-
Notifications
You must be signed in to change notification settings - Fork 1
/
altjson.r3
195 lines (167 loc) · 4.81 KB
/
altjson.r3
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
REBOL [
Title: "JSON Parser for Rebol 3"
Author: "Christopher Ross-Gill"
Type: 'module
Date: 15-Jul-2011
Home: http://www.ross-gill.com/page/JSON_and_REBOL
File: %altjson.r
Version: 0.2.8
Rights: http://creativecommons.org/licenses/by-nc-sa/2.0/
Name: 'altjson
Exports: [load-json to-json]
Purpose: "Convert a Rebol block to a JSON string"
History: [
22-May-2005 0.1.0 "Original Version"
6-Aug-2010 0.2.2 "Issue! composed of digits encoded as integers"
28-Aug-2010 0.2.4 "Encodes tag! any-type! paired blocks as an object"
2-Dec-2010 0.2.5 "Support for time! added"
15-July-2011 0.2.6 "Flattens Flickr '_content' objects"
]
Notes: {
- Simple Escaping
- Converts date! to RFC 822 Date String ('to-idate)
}
]
load-json: use [
tree branch here val flat? emit new-child to-parent neaten
space comma number string block object _content value
][
branch: make block! 10
emit: func [val][here: insert/only here val]
new-child: [(insert/only branch insert/only here here: copy [])]
to-parent: [(here: take branch)]
neaten: [
(new-line/all head here true)
(new-line/all/skip head here true 2)
]
space: use [space][
space: charset " ^-^/^M"
[any space]
]
comma: [space #"," space]
number: use [dg ex nm as-num][
dg: charset "0123456789"
ex: [[#"e" | #"E"] opt [#"+" | #"-"] some dg]
nm: [opt #"-" some dg opt [#"." some dg] opt ex]
as-num: func [val][
either parse val [opt "-" some dg][
any [attempt [to integer! val] to issue! val]
][
to decimal! val
]
]
[copy val nm (val: as-num val)]
]
string: use [ch dq es hx mp decode][
ch: complement charset {\"}
es: charset {"\/bfnrt}
hx: charset "0123456789ABCDEFabcdef"
mp: [#"^"" "^"" #"\" "\" #"/" "/" #"b" "^H" #"f" "^L" #"r" "^M" #"n" "^/" #"t" "^-"]
decode: use [ch mk escape][
escape: [
mk: #"\" [
es (mk: change/part mk select mp mk/2 2)
| #"u" copy ch 4 hx (
mk: change/part mk to char! to integer! to issue! ch 6
)
] :mk
]
func [text [string! none!] /mk][
either none? text [copy ""][
all [parse/all text [any [to "\" escape] to end] text]
]
]
]
[#"^"" copy val [any [some ch | #"\" [#"u" 4 hx | es]]] #"^"" (val: decode val)]
]
block: use [list][
list: [space opt [value any [comma value]] space]
[#"[" new-child list #"]" neaten/1 to-parent]
]
_content: [#"{" space {"_content"} space #":" space value space "}"] ; Flickr
object: use [name list as-object][
name: [
string space #":" space
(emit to either flat? [tag!][set-word!] val)
]
list: [space opt [name value any [comma name value]] space]
as-object: [(unless flat? [here: change back here make object! pick back here 1])]
[#"{" new-child list #"}" neaten/2 to-parent as-object]
]
value: [
"null" (emit none)
| "true" (emit true)
| "false" (emit false)
| number (emit val)
| string (emit val)
| _content (emit val)
| object | block
]
func [
[catch] "Convert a json string to rebol data"
json [string! binary! file! url!] "JSON string"
/flat "Objects are imported as tag-value pairs"
][
flat?: :flat
tree: here: copy []
if any [file? json url? json][
if error? json: try [read (json)][
throw :json
]
]
unless parse/all json [space opt value space][
make error! "Not a valid JSON string"
]
pick tree 1
]
]
to-json: use [
json emit emits escape emit-issue
here comma block object value
][
emit: func [data][repend json data]
emits: func [data][emit {"} emit data emit {"}]
escape: use [mp ch es encode][
mp: [#"^/" "\n" #"^M" "\r" #"^-" "\t" #"^"" "\^"" #"\" "\\" #"/" "\/"]
ch: complement es: charset extract mp 2
encode: func [here][change/part here select mp here/1 1]
func [txt][
parse/all txt [any [txt: some ch | es (txt: encode txt) :txt]]
head txt
]
]
emit-issue: use [dg nm mk][
dg: charset "0123456789"
nm: [opt "-" some dg]
[(either parse next form here/1 [copy mk nm][emit mk][emits here/1])]
]
comma: [(if not tail? here [emit ","])]
block: [(emit "[") any [here: value here: comma] (emit "]")]
object: [
(emit "{")
any [
here: [tag! | set-word!]
(emit [{"} escape to string! either tag? here/1 [here/1][to word! here/1] {":}])
here: value here: comma
]
(emit "}")
]
value: [
number! (emit here/1)
| [logic! | 'true | 'false] (emit form here/1)
| [none! | 'none] (emit 'null)
| date! (emits to-idate here/1)
| issue! emit-issue
| [
any-string! | word! | lit-word! | tuple! | pair! | money! | time!
] (emits escape form here/1)
| into [some [tag! skip]] :here (change/only here copy first here) into object
| any-block! :here (change/only here copy first here) into block
| object! :here (change/only here body-of first here) into object
| any-type! (emits [type? here/1 "!"])
]
func [data][
json: make string! ""
if parse compose/only [(data)][here: value][json]
]
]