1
- #[ link( name = "json" ,
2
- vers = "0.1" ,
3
- uuid = "09d7f2fc-1fad-48b2-9f9d-a65512342f16" ,
4
- url = "http://www.leptoquark.net/~elly/rust/" ) ] ;
5
- #[ copyright = "Google Inc. 2011" ] ;
6
- #[ comment = "JSON serialization format library" ] ;
7
- #[ license = "BSD" ] ;
1
+ // Rust JSON serialization library
2
+ // Copyright (c) 2011 Google Inc.
8
3
9
4
import float;
10
5
import map;
@@ -14,18 +9,39 @@ import str;
14
9
import vec;
15
10
16
11
export json;
17
- export tostr ;
18
- export fromstr ;
12
+ export to_str ;
13
+ export from_str ;
19
14
15
+ export num;
16
+ export string;
17
+ export boolean;
18
+ export list;
19
+ export dict;
20
+
21
+ /*
22
+ Tag: json
23
+
24
+ Represents a json value.
25
+ */
20
26
tag json {
27
+ /* Variant: num */
21
28
num( float) ;
29
+ /* Variant: string */
22
30
string ( str) ;
31
+ /* Variant: boolean */
23
32
boolean ( bool) ;
33
+ /* Variant: list */
24
34
list ( @[ json] ) ;
35
+ /* Variant: dict */
25
36
dict ( map:: hashmap<str, json>) ;
26
37
}
27
38
28
- fn tostr ( j : json ) -> str {
39
+ /*
40
+ Function: to_str
41
+
42
+ Serializes a json value into a string.
43
+ */
44
+ fn to_str ( j : json ) -> str {
29
45
alt j {
30
46
num( f) { float:: to_str ( f, 6 u) }
31
47
string ( s) { #fmt[ "\" %s\" " , s] } // XXX: escape
@@ -34,15 +50,15 @@ fn tostr(j: json) -> str {
34
50
list ( @js) {
35
51
str:: concat ( [ "[" ,
36
52
str:: connect (
37
- vec:: map :: < json , str > ( { |e| tostr ( e) } , js) ,
53
+ vec:: map :: < json , str > ( { |e| to_str ( e) } , js) ,
38
54
", " ) ,
39
55
"]" ] )
40
56
}
41
57
dict ( m) {
42
58
let parts = [ ] ;
43
59
m. items ( { |k, v|
44
60
vec:: grow ( parts, 1 u,
45
- str:: concat ( [ "\" " , k, "\" : " , tostr ( v) ] )
61
+ str:: concat ( [ "\" " , k, "\" : " , to_str ( v) ] )
46
62
)
47
63
} ) ;
48
64
str:: concat ( [ "{ " , str:: connect ( parts, ", " ) , " }" ] )
@@ -51,10 +67,11 @@ fn tostr(j: json) -> str {
51
67
}
52
68
53
69
fn rest ( s : str ) -> str {
70
+ assert ( str:: char_len ( s) >= 1 u) ;
54
71
str:: char_slice ( s, 1 u, str:: char_len ( s) )
55
72
}
56
73
57
- fn fromstr_str ( s : str ) -> ( option:: t < json > , str ) {
74
+ fn from_str_str ( s : str ) -> ( option:: t < json > , str ) {
58
75
let pos = 0 u;
59
76
let len = str:: byte_len ( s) ;
60
77
let escape = false ;
@@ -86,15 +103,15 @@ fn fromstr_str(s: str) -> (option::t<json>, str) {
86
103
ret ( none, s) ;
87
104
}
88
105
89
- fn fromstr_list ( s : str ) -> ( option:: t < json > , str ) {
106
+ fn from_str_list ( s : str ) -> ( option:: t < json > , str ) {
90
107
if str:: char_at ( s, 0 u) != '[' { ret ( none, s) ; }
91
108
let s0 = str:: trim_left ( rest ( s) ) ;
92
109
let vals = [ ] ;
93
110
if str:: is_empty ( s0) { ret ( none, s0) ; }
94
111
if str:: char_at ( s0, 0 u) == ']' { ret ( some ( list ( @[ ] ) ) , rest ( s0) ) ; }
95
112
while str:: is_not_empty ( s0) {
96
113
s0 = str:: trim_left ( s0) ;
97
- let ( next, s1) = fromstr_helper ( s0) ;
114
+ let ( next, s1) = from_str_helper ( s0) ;
98
115
s0 = s1;
99
116
alt next {
100
117
some( j) { vec:: grow ( vals, 1 u, j) ; }
@@ -112,15 +129,15 @@ fn fromstr_list(s: str) -> (option::t<json>, str) {
112
129
ret ( none, s0) ;
113
130
}
114
131
115
- fn fromstr_dict ( s : str ) -> ( option:: t < json > , str ) {
132
+ fn from_str_dict ( s : str ) -> ( option:: t < json > , str ) {
116
133
if str:: char_at ( s, 0 u) != '{' { ret ( none, s) ; }
117
134
let s0 = str:: trim_left ( rest ( s) ) ;
118
135
let vals = map:: new_str_hash :: < json > ( ) ;
119
136
if str:: is_empty ( s0) { ret ( none, s0) ; }
120
137
if str:: char_at ( s0, 0 u) == '}' { ret ( some ( dict ( vals) ) , rest ( s0) ) ; }
121
138
while str:: is_not_empty ( s0) {
122
139
s0 = str:: trim_left ( s0) ;
123
- let ( next, s1) = fromstr_helper ( s0) ; // key
140
+ let ( next, s1) = from_str_helper ( s0) ; // key
124
141
let key = "" ;
125
142
s0 = s1;
126
143
alt next {
@@ -131,7 +148,7 @@ fn fromstr_dict(s: str) -> (option::t<json>, str) {
131
148
if str:: is_empty ( s0) { ret ( none, s0) ; }
132
149
if str:: char_at ( s0, 0 u) != ':' { ret ( none, s0) ; }
133
150
s0 = str:: trim_left ( rest ( s0) ) ;
134
- let ( next, s1) = fromstr_helper ( s0) ; // value
151
+ let ( next, s1) = from_str_helper ( s0) ; // value
135
152
s0 = s1;
136
153
alt next {
137
154
some( j) { vals. insert ( key, j) ; }
@@ -149,7 +166,7 @@ fn fromstr_dict(s: str) -> (option::t<json>, str) {
149
166
( none, s)
150
167
}
151
168
152
- fn fromstr_float ( s : str ) -> ( option:: t < json > , str ) {
169
+ fn from_str_float ( s : str ) -> ( option:: t < json > , str ) {
153
170
let pos = 0 u;
154
171
let len = str:: byte_len ( s) ;
155
172
let res = 0 f;
@@ -205,7 +222,7 @@ fn fromstr_float(s: str) -> (option::t<json>, str) {
205
222
ret ( some ( num ( neg * res) ) , str:: char_slice ( s, pos, str:: char_len ( s) ) ) ;
206
223
}
207
224
208
- fn fromstr_bool ( s : str ) -> ( option:: t < json > , str ) {
225
+ fn from_str_bool ( s : str ) -> ( option:: t < json > , str ) {
209
226
if ( str:: starts_with ( s, "true" ) ) {
210
227
( some ( boolean ( true ) ) , str:: slice ( s, 4 u, str:: byte_len ( s) ) )
211
228
} else if ( str:: starts_with ( s, "false" ) ) {
@@ -215,79 +232,26 @@ fn fromstr_bool(s: str) -> (option::t<json>, str) {
215
232
}
216
233
}
217
234
218
- fn fromstr_helper ( s : str ) -> ( option:: t < json > , str ) {
235
+ fn from_str_helper ( s : str ) -> ( option:: t < json > , str ) {
219
236
let s = str:: trim_left ( s) ;
220
237
if str:: is_empty ( s) { ret ( none, s) ; }
221
238
let start = str:: char_at ( s, 0 u) ;
222
239
alt start {
223
- '"' { fromstr_str ( s) }
224
- '[' { fromstr_list ( s) }
225
- '{' { fromstr_dict ( s) }
226
- '0' to '9' | '-' | '+' | '.' { fromstr_float ( s) }
227
- 't' | 'f' { fromstr_bool ( s) }
240
+ '"' { from_str_str ( s) }
241
+ '[' { from_str_list ( s) }
242
+ '{' { from_str_dict ( s) }
243
+ '0' to '9' | '-' | '+' | '.' { from_str_float ( s) }
244
+ 't' | 'f' { from_str_bool ( s) }
228
245
_ { ret ( none, s) ; }
229
246
}
230
247
}
231
248
232
- fn fromstr ( s : str ) -> option:: t < json > {
233
- let ( j, _) = fromstr_helper ( s) ;
234
- j
235
- }
236
-
237
- fn main ( ) {
238
- let j = fromstr ( "{ \" foo\" : [ 4, 5 ], \" bar\" : { \" baz\" : true}}" ) ;
239
- alt j {
240
- some( j0) {
241
- log tostr ( j0) ;
242
- }
243
- _ { }
244
- }
245
- }
246
-
247
- #[ cfg( test) ]
248
- mod tests {
249
- #[ test]
250
- fn test_fromstr_num ( ) {
251
- assert ( fromstr ( "3" ) == some ( num ( 3 f) ) ) ;
252
- assert ( fromstr ( "3.1" ) == some ( num ( 3.1 f) ) ) ;
253
- assert ( fromstr ( "-1.2" ) == some ( num ( -1.2 f) ) ) ;
254
- assert ( fromstr ( ".4" ) == some ( num ( 0.4 f) ) ) ;
255
- }
256
-
257
- #[ test]
258
- fn test_fromstr_str ( ) {
259
- assert ( fromstr ( "\" foo\" " ) == some ( string ( "foo" ) ) ) ;
260
- assert ( fromstr ( "\" \\ \" \" " ) == some ( string ( "\" " ) ) ) ;
261
- assert ( fromstr ( "\" lol" ) == none) ;
262
- }
263
-
264
- #[ test]
265
- fn test_fromstr_bool ( ) {
266
- assert ( fromstr ( "true" ) == some ( boolean ( true ) ) ) ;
267
- assert ( fromstr ( "false" ) == some ( boolean ( false ) ) ) ;
268
- assert ( fromstr ( "truz" ) == none) ;
269
- }
270
-
271
- #[ test]
272
- fn test_fromstr_list ( ) {
273
- assert ( fromstr ( "[]" ) == some ( list ( @[ ] ) ) ) ;
274
- assert ( fromstr ( "[true]" ) == some ( list ( @[ boolean ( true ) ] ) ) ) ;
275
- assert ( fromstr ( "[3, 1]" ) == some ( list ( @[ num ( 3 f) , num ( 1 f) ] ) ) ) ;
276
- assert ( fromstr ( "[2, [4, 1]]" ) ==
277
- some ( list ( @[ num ( 2 f) , list ( @[ num ( 4 f) , num ( 1 f) ] ) ] ) ) ) ;
278
- assert ( fromstr ( "[2, ]" ) == none) ;
279
- assert ( fromstr ( "[5, " ) == none) ;
280
- assert ( fromstr ( "[6 7]" ) == none) ;
281
- assert ( fromstr ( "[3" ) == none) ;
282
- }
249
+ /*
250
+ Function: from_str
283
251
284
- #[ test]
285
- fn test_fromstr_dict ( ) {
286
- assert ( fromstr ( "{}" ) != none) ;
287
- assert ( fromstr ( "{\" a\" : 3}" ) != none) ;
288
- assert ( fromstr ( "{\" a\" : }" ) == none) ;
289
- assert ( fromstr ( "{\" a\" }" ) == none) ;
290
- assert ( fromstr ( "{\" a\" " ) == none) ;
291
- assert ( fromstr ( "{" ) == none) ;
292
- }
252
+ Deserializes a json value from a string.
253
+ */
254
+ fn from_str ( s : str ) -> option:: t < json > {
255
+ let ( j, _) = from_str_helper ( s) ;
256
+ j
293
257
}
0 commit comments