Skip to content

Commit 6b6673d

Browse files
committed
data.json : handle Nil and PersistentList
1 parent 4bd54cc commit 6b6673d

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

pixie/data/json.pxi

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@
88
(write-string [this]))
99

1010
(defn- write-map [m]
11-
(if (empty? m)
12-
"{}"
13-
(str "{ "
14-
(->> m
15-
(transduce (comp (map (fn [[k v]] (string/interp "$(write-string k)$: $(write-string v)$")))
16-
(interpose ", "))
17-
string-builder))
18-
" }")))
11+
(str "{"
12+
(->> m
13+
(transduce (comp (map (fn [[k v]] (string/interp "$(write-string k)$: $(write-string v)$")))
14+
(interpose ", "))
15+
string-builder))
16+
"}"))
1917

2018
(defn- write-sequential [xs]
21-
(if (empty? xs)
22-
"[]"
23-
(str "[ "
24-
(->> xs
25-
(transduce (comp (map write-string)
26-
(interpose ", "))
27-
string-builder))
28-
" ]")))
19+
(str "["
20+
(->> xs
21+
(transduce (comp (map write-string)
22+
(interpose ", "))
23+
string-builder))
24+
"]"))
2925

3026
(defn- write-str [s]
3127
(string/interp "\"$s$\""))
@@ -34,10 +30,12 @@
3430
Character (write-string [this] (write-str this))
3531
Cons (write-string [this] (write-sequential this))
3632
EmptyList (write-string [this] (write-sequential this))
33+
Nil (write-string [_] "null")
3734
Number (write-string [this] (str this))
3835
Keyword (write-string [this] (write-str (name this)))
3936
LazySeq (write-string [this] (write-sequential this))
4037
IMap (write-string [this] (write-map this))
4138
IVector (write-string [this] (write-sequential this))
39+
PersistentList (write-string [this] (write-sequential this))
4240
Ratio (write-string [this] (str (float this)))
4341
String (write-string [this] (write-str this)))

tests/pixie/tests/data/test-json.pxi

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,67 @@
1010
(assert= (json/read-string "{\"foo\": 42, \"bar\": [\"baz\"]}")
1111
{"foo" 42, "bar" ["baz"]}))
1212

13-
(deftest test-write-string
14-
(assert= (json/write-string {:strings {:a \a, :b "b", :c "", :d :d}
15-
:numbers {:one 1, :two 2.0, :three 3/1}
16-
:lists {:empty {:vector [], :list '(), :seq (take 0 (range))}
17-
:non-empty {:vector [1 2 3], :list '(1 2 3), :seq (take 3 (range))}}
18-
:maps {:empty {}, :non-empty {:this "is covered, right? ;)"}}})
19-
"{ \"numbers\": { \"three\": 3, \"two\": 2.000000, \"one\": 1 }, \"lists\": { \"empty\": { \"vector\": [], \"list\": [], \"seq\": [] }, \"non-empty\": { \"vector\": [ 1, 2, 3 ], \"list\": [ 1, 2, 3 ], \"seq\": [ 0, 1, 2 ] } }, \"maps\": { \"empty\": {}, \"non-empty\": { \"this\": \"is covered, right? ;)\" } }, \"strings\": { \"d\": \"d\", \"a\": \"a\", \"c\": \"\", \"b\": \"b\" } }"))
13+
(deftest test-strings
14+
(assert-table [x y] (assert= (json/write-string x) y)
15+
\a "\"a\""
16+
"foo" "\"foo\""
17+
:bar "\"bar\""
18+
"" "\"\""))
19+
20+
(deftest test-numbers
21+
(assert-table [x y] (assert= (json/write-string x) y)
22+
1 "1"
23+
1.0 "1.000000"
24+
0.1 "0.100000"
25+
1.1 "1.100000"
26+
1234.5678 "1234.567800"
27+
-1 "-1"
28+
-0.1 "-0.100000"
29+
-1.1 "-1.100000"
30+
-1234.5678 "-1234.567800"
31+
1e1 "10.000000"
32+
3/1 "3"))
33+
34+
(deftest test-vectors
35+
(assert-table [x y] (assert= (json/write-string x) y)
36+
[] "[]"
37+
[nil] "[null]"
38+
[1 2] "[1, 2]"
39+
[1 1.0 nil] "[1, 1.000000, null]"
40+
["foo" 42] "[\"foo\", 42]"))
41+
42+
(deftest test-seqs
43+
(assert-table [x y] (assert= (json/write-string x) y)
44+
(take 0 (range)) "[]"
45+
(take 1 (repeat nil)) "[null]"
46+
(map identity [1 2]) "[1, 2]"
47+
(reduce + [1 2 3]) "6"))
48+
49+
(deftest test-lists
50+
(assert-table [x y] (assert= (json/write-string x) y)
51+
'() "[]"
52+
'(nil) "[null]"
53+
'(1 2) "[1, 2]"
54+
'(1 1.0 nil) "[1, 1.000000, null]"
55+
'("foo" 42) "[\"foo\", 42]"
56+
(list) "[]"
57+
(list nil) "[null]"
58+
(list 1 2) "[1, 2]"
59+
(list 1 1.0 nil) "[1, 1.000000, null]"
60+
(list "foo" 42) "[\"foo\", 42]"))
61+
62+
(deftest test-maps
63+
(assert-table [x y] (assert= (json/write-string x) y)
64+
{} "{}"
65+
{:foo 42} "{\"foo\": 42}"
66+
{"foo" 42} "{\"foo\": 42}"
67+
{"foo" 42, "bar" nil} "{\"foo\": 42, \"bar\": null}"))
2068

2169
(deftest test-round-trip
2270
(let [data {"foo" 1, "bar" [2 3]}] ; won't work with keywords because the parser doesn't keywordise them (yet)
2371
(assert= (-> data json/write-string json/read-string)
2472
data))
2573

26-
(let [string "{ \"foo\": [ 1, 2, 3 ] }"]
74+
(let [string "{\"foo\": [1, 2, 3]}"]
2775
(assert= (-> string json/read-string json/write-string)
2876
string)))

0 commit comments

Comments
 (0)