Skip to content

Commit 4d995e6

Browse files
committed
std: change default json enum encoder to use strings or a flat vec
1 parent b10b8c3 commit 4d995e6

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

src/libstd/json.rs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,15 @@ impl serialize::Encoder for Encoder {
119119
f()
120120
}
121121

122-
fn emit_enum_variant(&self, name: &str, _id: uint, _cnt: uint, f: &fn()) {
122+
fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) {
123123
// encoding of enums is special-cased for Option. Specifically:
124124
// Some(34) => 34
125125
// None => null
126126

127-
// other enums are encoded as vectors:
127+
// other enums are encoded as strings or vectors:
128+
// Bunny => "Bunny"
128129
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
129130

130-
// the default expansion for enums is more verbose than I'd like;
131-
// specifically, the inner pair of brackets seems superfluous,
132-
// BUT the design of the enumeration framework and the requirements
133-
// of the special-case for Option mean that a first argument must
134-
// be encoded "naked"--with no commas--and that the option name
135-
// can't be followed by just a comma, because there might not
136-
// be any elements in the tuple.
137-
138131
// FIXME #4872: this would be more precise and less frightening
139132
// with fully-qualified option names. To get that information,
140133
// we'd have to change the expansion of auto-encode to pass
@@ -144,14 +137,14 @@ impl serialize::Encoder for Encoder {
144137
f();
145138
} else if name == ~"None" {
146139
self.wr.write_str(~"null");
140+
} else if cnt == 0 {
141+
self.wr.write_str(escape_str(name));
147142
} else {
148143
self.wr.write_char('[');
149144
self.wr.write_str(escape_str(name));
150145
self.wr.write_char(',');
151-
self.wr.write_char('[');
152146
f();
153147
self.wr.write_char(']');
154-
self.wr.write_char(']');
155148
}
156149
}
157150

@@ -256,28 +249,19 @@ impl serialize::Encoder for PrettyEncoder {
256249
f();
257250
} else if name == ~"None" {
258251
self.emit_nil();
252+
} else if cnt == 0 {
253+
self.wr.write_str(escape_str(name));
259254
} else {
260255
self.wr.write_char('[');
261256
self.indent += 2;
262257
self.wr.write_char('\n');
263258
self.wr.write_str(spaces(self.indent));
264259
self.wr.write_str(escape_str(name));
265-
if cnt == 0 {
266-
self.wr.write_str(",\n");
267-
self.wr.write_str(spaces(self.indent));
268-
self.wr.write_str("[]\n");
269-
} else {
270-
self.wr.write_str(",\n");
271-
self.wr.write_str(spaces(self.indent));
272-
self.wr.write_str("[\n");
273-
self.indent += 2;
274-
f();
275-
self.wr.write_char('\n');
276-
self.indent -= 2;
277-
self.wr.write_str(spaces(self.indent));
278-
self.wr.write_str("]\n");
279-
}
260+
self.wr.write_str(",\n");
261+
f();
262+
self.wr.write_char('\n');
280263
self.indent -= 2;
264+
self.wr.write_str(spaces(self.indent));
281265
self.wr.write_char(']');
282266
}
283267
}
@@ -1406,7 +1390,7 @@ mod tests {
14061390
let encoder = Encoder(wr);
14071391
animal.encode(&encoder);
14081392
};
1409-
assert_eq!(s, ~"[\"Dog\",[]]");
1393+
assert_eq!(s, ~"\"Dog\"");
14101394
}
14111395

14121396
#[test]
@@ -1417,14 +1401,7 @@ mod tests {
14171401
let encoder = PrettyEncoder(wr);
14181402
animal.encode(&encoder);
14191403
};
1420-
assert_eq!(
1421-
s,
1422-
~"\
1423-
[\n \
1424-
\"Dog\",\n \
1425-
[]\n\
1426-
]"
1427-
);
1404+
assert_eq!(s, ~"\"Dog\"");
14281405
}
14291406

14301407
#[test]
@@ -1435,7 +1412,7 @@ mod tests {
14351412
let encoder = Encoder(wr);
14361413
animal.encode(&encoder);
14371414
};
1438-
assert_eq!(s, ~"[\"Frog\",[\"Henry\",349]]");
1415+
assert_eq!(s, ~"[\"Frog\",\"Henry\",349]");
14391416
}
14401417
14411418
#[test]
@@ -1451,10 +1428,8 @@ mod tests {
14511428
~"\
14521429
[\n \
14531430
\"Frog\",\n \
1454-
[\n \
1455-
\"Henry\",\n \
1456-
349\n \
1457-
]\n\
1431+
\"Henry\",\n \
1432+
349\n\
14581433
]"
14591434
);
14601435
}

src/libsyntax/parse/mod.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,25 +329,27 @@ mod test {
329329
assert_eq!(
330330
to_json_str(@tts),
331331
~"[\
332-
[\"tt_tok\",[null,[\"IDENT\",[\"fn\",false]]]],\
333-
[\"tt_tok\",[null,[\"IDENT\",[\"foo\",false]]]],\
334-
[\"tt_delim\",[\
332+
[\"tt_tok\",null,[\"IDENT\",\"fn\",false]],\
333+
[\"tt_tok\",null,[\"IDENT\",\"foo\",false]],\
334+
[\
335+
\"tt_delim\",\
335336
[\
336-
[\"tt_tok\",[null,[\"LPAREN\",[]]]],\
337-
[\"tt_tok\",[null,[\"IDENT\",[\"x\",false]]]],\
338-
[\"tt_tok\",[null,[\"COLON\",[]]]],\
339-
[\"tt_tok\",[null,[\"IDENT\",[\"int\",false]]]],\
340-
[\"tt_tok\",[null,[\"RPAREN\",[]]]]\
337+
[\"tt_tok\",null,\"LPAREN\"],\
338+
[\"tt_tok\",null,[\"IDENT\",\"x\",false]],\
339+
[\"tt_tok\",null,\"COLON\"],\
340+
[\"tt_tok\",null,[\"IDENT\",\"int\",false]],\
341+
[\"tt_tok\",null,\"RPAREN\"]\
341342
]\
342-
]],\
343-
[\"tt_delim\",[\
343+
],\
344+
[\
345+
\"tt_delim\",\
344346
[\
345-
[\"tt_tok\",[null,[\"LBRACE\",[]]]],\
346-
[\"tt_tok\",[null,[\"IDENT\",[\"x\",false]]]],\
347-
[\"tt_tok\",[null,[\"SEMI\",[]]]],\
348-
[\"tt_tok\",[null,[\"RBRACE\",[]]]]\
347+
[\"tt_tok\",null,\"LBRACE\"],\
348+
[\"tt_tok\",null,[\"IDENT\",\"x\",false]],\
349+
[\"tt_tok\",null,\"SEMI\"],\
350+
[\"tt_tok\",null,\"RBRACE\"]\
349351
]\
350-
]]\
352+
]\
351353
]"
352354
);
353355
let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts)

0 commit comments

Comments
 (0)