Hi Mathew, 

Thanks for the code. I somehow missed your update. Sorry for that. What I 
had in mind, I was able to do it in YAML although it was a cumbersome 
solution but it worked. 

Thanks again for the help. 

Siddharth

On Thursday, 15 March 2018 17:11:30 UTC+5:30, Matthew Butterick wrote:
>
>
> On Mar 14, 2018, at 11:38 PM, siddhar...@gmail.com <javascript:> wrote:
>
> I tried converting the S-expressions into nested hash-table by checking if 
> it car(list) is a list or not and making it a key with value a blank 
> dictionary. 
> This turned to be more difficult than I anticipated. 
>
>
> If you just want to store the X-expression in JSON, you can always just 
> serialize it as a string:
>
> #lang racket
> (require json)
> (define x '(root (p "Hello world" 42) (p "Goodbye")))
> (define jsx (format "~s" x))
> (define json-str (jsexpr->string jsx))
> (read (open-input-string (read-json (open-input-string json-str))))
>
>
> If you want a more detailed mapping, you'd want something like this that 
> descends through the X-expression and encodes incompatible datatypes (like 
> symbols) as JS-expression approximations. (The below example is 
> illustrative, not exhaustive — it doesn't handle everything that coule 
> appear in an X-expression)
>
> #lang racket
> (require txexpr json)
> (define x '(root (p ((foo "bar")) "Hello world" 42) (p "Goodbye")))
>
> (define/contract (xexpr->jsexpr x)
>   (xexpr? . -> . jsexpr?)
>   (cond
>     [(txexpr? x) (list* (hasheq 'tag (symbol->string (get-tag x)))
>                         (attrs->hash (get-attrs x))
>                         (map xexpr->jsexpr (get-elements x)))]
>     [else x]))
>
> (define jsx (xexpr->jsexpr x))
>
> (define/contract (jsexpr->xexpr x)
>   (jsexpr? . -> . xexpr?)
>   (cond
>     [(and (pair? x) (hash? (car x)) (hash-has-key? (car x) 'tag))
>      (apply txexpr* (string->symbol (hash-ref (car x) 'tag)) (map 
> jsexpr->xexpr (cdr x)))]
>     [(hash? x) (hash->attrs x)]
>     [else x]))
>
> (define x2 (jsexpr->xexpr jsx))
>
> (equal? x x2)
>

-- 
You received this message because you are subscribed to the Google Groups 
"Pollen" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pollenpub+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to