i'm developing rest service in racket in educational purposes , facing problem json parsing. have post request following body
"{\"word\": \"a\", \"desc\": \"b\"}" also have request handler request such
(define (add-word-req req) (define post-data (request-post-data/raw req)) (display post-data) (newline) (define post-data-expr1 (bytes->jsexpr post-data)) (display post-data-expr1) (newline) (display (jsexpr? post-data-expr1)) (display (hash? post-data-expr1)) (newline) (define post-data-expr (string->jsexpr "{\"word\": \"a\", \"desc\": \"b\"}")) (display post-data-expr) (newline) (display (hash? post-data-expr)) (newline) (for (((key val) (in-hash post-data-expr))) (printf "~a = ~a~%" key val)) (cond [(jsexpr? post-data-expr) ;; conditional work ... ] [else ...] ) ) as can see, request body , hard-coded json same.
while processing request next output:
"{\"word\": \"a\", \"desc\": \"b\"}" {"word": "a", "desc": "b"} #t#f #hasheq((desc . b) (word . a)) #t desc = b word = body transmitted in 1 piece , transformed jsexpr, still not hash! , because of can't use hash methods post-data-expr1.
what best way hash such jsexpr? or should use method obtain key/values?
regards.
this program:
#lang racket (require json) (string->jsexpr "{\"word\": \"a\", \"desc\": \"b\"}") (bytes->jsexpr #"{\"word\": \"a\", \"desc\": \"b\"}") has output:
'#hasheq((desc . "b") (word . "a")) '#hasheq((desc . "b") (word . "a")) this means bytes->jsexpr ought work.
can change:
(define post-data (request-post-data/raw req)) (display post-data) (newline) to
(define post-data (request-post-data/raw req)) (write post-data) (newline) ?
i have feeling contents of byte string different 1 use later on.
note:
> (displayln "{\\\"word\\\": \\\"a\\\", \\\"desc\\\": \\\"b\\\"}") {\"word\": \"a\", \"desc\": \"b\"} > (displayln "{\"word\": \"a\", \"desc\": \"b\"}") {"word": "a", "desc": "b"} the output of (display post-data-expr1) matches first interaction above. guess therefore both backslashes , quotes escaped in input.
Comments
Post a Comment