Skip to content

Commit

Permalink
Order keys when writing JSON output.
Browse files Browse the repository at this point in the history
More significant on Racket CS because ordering is often different
than creation order.

Closes racket#3537.
  • Loading branch information
samth committed Dec 23, 2020
1 parent 3577cb0 commit 6369646
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions racket/collects/json/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,20 @@
[(hash? x)
(write-bytes #"{" o)
(define first? #t)
(for ([(k v) (in-hash x)])
(unless (symbol? k)
(raise-type-error who "legal JSON key value" k))
(if first? (set! first? #f) (write-bytes #"," o))
;; use a string encoding so we get the same deal with
;; `rx-to-encode'
(write-json-string (symbol->string k))
(write-bytes #":" o)
(loop v))
(write-bytes #"}" o)]
(hash-for-each
x
(lambda (k v)
(unless (symbol? k)
(raise-type-error who "legal JSON key value" k))
(if first? (set! first? #f) (write-bytes #"," o))
;; use a string encoding so we get the same deal with
;; `rx-to-encode'
(write-json-string (symbol->string k))
(write-bytes #":" o)
(loop v))
;; order output
#true)
(write-bytes #"}" o)]
[else (raise-type-error who "legal JSON value" x)]))
(void))

Expand Down

0 comments on commit 6369646

Please sign in to comment.