annotate modules/json.tp @ 365:3d36d69aab7f

Fix JSON decoder to align with language changes
author Michael Pavone <pavone@retrodev.com>
date Fri, 07 Aug 2015 21:40:08 -0700
parents eef8a5cea812
children e2c1309ab750
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 startArr <- "[" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 endArr <- "]" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 startObj <- "{" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 endObj <- "}" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 quote <- "\"" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 esc <- "\\" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 zero <- "0" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 nine <- "9" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 neg <- "-" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 space <- " " byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 comma <- "," byte: 0
169
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
13 period <- "." byte: 0
167
5a6a55592c45 Fix some JSON parser bugs
Mike Pavone <pavone@retrodev.com>
parents: 166
diff changeset
14 tab <- " " byte: 0
166
e7642715d575 Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents: 165
diff changeset
15 nl <- "\n" byte: 0
e7642715d575 Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents: 165
diff changeset
16 cr <- "\r" byte: 0
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 colon <- ":" byte: 0
165
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
18 t <- "t" byte: 0
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
19 f <- "f" byte: 0
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20
365
3d36d69aab7f Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents: 329
diff changeset
21 parseNumAt <- :str at {
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 num <- 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 l <- str length
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 minus <- false
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 aft <- -1
169
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
26 ignore <- false
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 while: { at < l } do: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 b <- str byte: at
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 if: b = neg {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 minus <- true
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 } else: {
169
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
32 if: b = period {
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
33 ignore <- true
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 } else: {
169
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
35 if: b >= zero && b <= nine {
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
36 if: (not: ignore) {
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
37 num <- num * 10 + (str byte: at) - zero
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
38 }
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
39 } else: {
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
40 aft <- at
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
41 at <- l
9d8ae39e8e67 Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 167
diff changeset
42 }
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 at <- at + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 if: aft < 0 {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 aft <- at
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 if: minus {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 num <- 0 - num
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 value <- num
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 after <- aft
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58
365
3d36d69aab7f Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents: 329
diff changeset
59 parseStrAt <- :src at {
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 //TODO: Deal with escaped characters
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 end <- src find: "\"" startingAt: at + 1 else: { src length }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 value <- src from: (at + 1) withLength: (end - at - 1)
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 after <- end + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 _decode:at <- :text :cur {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 ret <- false
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 b <- text byte: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 if: b = neg || b >= zero && b <= nine {
365
3d36d69aab7f Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents: 329
diff changeset
72 parseNumAt: text cur
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 if: b = quote {
365
3d36d69aab7f Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents: 329
diff changeset
75 parseStrAt: text cur
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 if: b = startArr {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 len <- text length
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 val <- #[]
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 aft <- -1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 while: { cur < len } do: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 b <- text byte: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 if: b = endArr {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 aft <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 cur <- len
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 } else: {
166
e7642715d575 Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents: 165
diff changeset
88 if: b = comma || b = space || b = tab || b = nl || b = cr {
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 el <- _decode: text at: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 cur <- el after
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 val append: (el value)
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 value <- val
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 after <- aft
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 if: b = startObj {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 len <- text length
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 val <- dict linear
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 aft <- -1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 expectKey <- true
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 key <- ""
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 while: { cur < len } do: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 b <- text byte: cur
166
e7642715d575 Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents: 165
diff changeset
111 if: b = comma || b = space || b = tab || b = colon || b = nl || b = cr {
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 if: expectKey {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 if: b = endObj {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 aft <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 cur <- len
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 kd <- _decode: text at: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 key <- kd value
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 cur <- kd after
167
5a6a55592c45 Fix some JSON parser bugs
Mike Pavone <pavone@retrodev.com>
parents: 166
diff changeset
122
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 expectKey <- false
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 el <- _decode: text at: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 val set: key (el value)
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 cur <- el after
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 expectKey <- true
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 after <- aft
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 value <- val
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 } else: {
165
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
138 if: b = t && (text from: cur withLength: 4) = "true" {
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
139 #{
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
140 value <- true
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
141 after <- cur + 4
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
142 }
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
143 } else: {
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
144 if: b = f && (text from: cur withLength: 5) = "false" {
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
145 #{
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
146 value <- false
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
147 after <- cur + 5
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
148 }
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
149 } else: {
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
150 #{
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
151 value <- "foobar"
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
152 after <- (text length)
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
153 }
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
154 }
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 }
165
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
156
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 decode <- :text {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 (_decode: text at: 0) value
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 }
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
166
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
167 encode <- :value {
329
eef8a5cea812 Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents: 286
diff changeset
168 jsonEncoder encode: value
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
169 }
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
170
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
171 main <- {
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
172 o <- #{
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
173 foo <- "bar"
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
174 baz <- ["fizz" "buzz" "buzzzz"]
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
175 qux <- ((dict hash) set: "fo" "shizzle") set: "my" "nizzle"
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
176 arr <- #["pirate" "booty"]
286
ddf38b66b2e2 Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents: 272
diff changeset
177 numbers <- [1 2 3 42 (0-1337) 3.14159]
272
bb2b4613fdc8 Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
178 booleans <- [
286
ddf38b66b2e2 Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents: 272
diff changeset
179 true
272
bb2b4613fdc8 Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
180 false
bb2b4613fdc8 Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
181 ]
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
182 }
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
183 print: (encode: o) . "\n"
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
184 0
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
185 }
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187 }