forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.pxi
More file actions
41 lines (35 loc) · 1.3 KB
/
json.pxi
File metadata and controls
41 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(ns pixie.data.json
(:require [pixie.parser.json]
[pixie.string :as string]))
(def read-string pixie.parser.json/read-string)
(defprotocol IToJSON
(write-string [this]))
(defn- write-map [m]
(str "{"
(->> m
(transduce (comp (map (fn [[k v]] (string/interp "$(write-string k)$: $(write-string v)$")))
(interpose ", "))
string-builder))
"}"))
(defn- write-sequential [xs]
(str "["
(->> xs
(transduce (comp (map write-string)
(interpose ", "))
string-builder))
"]"))
(defn- write-str [s]
(string/interp "\"$s$\""))
(extend-protocol IToJSON
Character (write-string [this] (write-str this))
Cons (write-string [this] (write-sequential this))
EmptyList (write-string [this] (write-sequential this))
Nil (write-string [_] "null")
Number (write-string [this] (str this))
Keyword (write-string [this] (write-str (name this)))
LazySeq (write-string [this] (write-sequential this))
IMap (write-string [this] (write-map this))
IVector (write-string [this] (write-sequential this))
PersistentList (write-string [this] (write-sequential this))
Ratio (write-string [this] (str (float this)))
String (write-string [this] (write-str this)))