Created
December 12, 2024 23:38
-
-
Save samdphillips/3721d9788594d77e2f7f4d4781341685 to your computer and use it in GitHub Desktop.
Racket JSON customization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- racket/collects/json/main.rkt 2024-11-05 16:35:40 | |
+++ racket/collects/json/exp.rkt 2024-12-12 07:43:11 | |
@@ -65,12 +65,16 @@ | |
any)] ;; jsexpr? | |
)) | |
+(module* for-extension #f | |
+ (provide read-json*)) | |
+ | |
;; ----------------------------------------------------------------------------- | |
;; CUSTOMIZATION | |
;; The default translation for a JSON `null' value | |
(define json-null (make-parameter 'null)) | |
+ | |
;; ----------------------------------------------------------------------------- | |
;; PREDICATE | |
@@ -206,9 +210,14 @@ | |
;; PARSING (from JSON to Racket) | |
(define (read-json [i (current-input-port)] #:null [jsnull (json-null)]) | |
- (read-json* 'read-json i jsnull)) | |
+ (read-json* 'read-json i jsnull make-immutable-hasheq values string->symbol values)) | |
-(define (read-json* who i jsnull) | |
+(define (read-json* who i | |
+ jsnull | |
+ make-json-object | |
+ make-json-list | |
+ make-json-key | |
+ make-json-string) | |
;; Follows the specification (eg, at json.org) -- no extensions. | |
;; | |
(define (err fmt . args) | |
@@ -363,9 +372,8 @@ | |
(unless (char=? #\: ch) | |
(err "error while parsing a json object pair")) | |
(read-byte i) | |
- (cons (string->symbol k) (read-json))) | |
- (for/hasheq ([p (in-list (read-list 'object #\} read-pair))]) | |
- (values (car p) (cdr p)))) | |
+ (cons (make-json-key k) (read-json))) | |
+ (make-json-object (read-list 'object #\} read-pair))) | |
;; | |
(define (read-literal bstr) | |
(define len (bytes-length bstr)) | |
@@ -524,9 +532,10 @@ | |
(eqv? ch #\-)) | |
(read-number ch)] | |
[(eqv? ch #\") (read-byte i) | |
- (read-a-string)] | |
+ (make-json-string (read-a-string))] | |
[(eqv? ch #\[) (read-byte i) | |
- (read-list 'array #\] read-json)] | |
+ (make-json-list | |
+ (read-list 'array #\] read-json))] | |
[(eqv? ch #\{) (read-byte i) | |
(read-hash)] | |
[else (bad-input)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment