application/octet-stream
•
1.04 KB
•
27 lines
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Contains subroutines for (de-)serializing JSON
(defn tree->json (v)
"Converts a tree into a JSON encoded string, you should prefer VAL->JSON"
(cat "{"
(join (map (:keys v)
(fn (k)
(cat "\"" (:string k) "\": "
(val->json (ref v k)))))
",\n")
"}"))
(defn val->json (v)
:export-as serialize
"Return V as a JSON encoded string"
(case (:type-name v)
(:nil "null")
((:int :float) (:string v))
(:bool (if v "true" "false"))
((:array :pair) (cat "[" (join (map v val->json) ",") "]"))
(:string (string/write v))
((:symbol :keyword) (cat "\"" (:string v) "\""))
(:tree (tree->json v))
(otherwise (throw (list :type-error "Can't encode the value into JSON" v (current-lambda))))))