1
Fork 0

std: change default json enum encoder to use strings or a flat vec

This commit is contained in:
Erick Tryzelaar 2013-03-27 00:14:52 -07:00
parent b10b8c3ee4
commit 4d995e66a2
2 changed files with 33 additions and 56 deletions

View file

@ -119,22 +119,15 @@ impl serialize::Encoder for Encoder {
f() f()
} }
fn emit_enum_variant(&self, name: &str, _id: uint, _cnt: uint, f: &fn()) { fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) {
// encoding of enums is special-cased for Option. Specifically: // encoding of enums is special-cased for Option. Specifically:
// Some(34) => 34 // Some(34) => 34
// None => null // None => null
// other enums are encoded as vectors: // other enums are encoded as strings or vectors:
// Bunny => "Bunny"
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]] // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
// the default expansion for enums is more verbose than I'd like;
// specifically, the inner pair of brackets seems superfluous,
// BUT the design of the enumeration framework and the requirements
// of the special-case for Option mean that a first argument must
// be encoded "naked"--with no commas--and that the option name
// can't be followed by just a comma, because there might not
// be any elements in the tuple.
// FIXME #4872: this would be more precise and less frightening // FIXME #4872: this would be more precise and less frightening
// with fully-qualified option names. To get that information, // with fully-qualified option names. To get that information,
// we'd have to change the expansion of auto-encode to pass // we'd have to change the expansion of auto-encode to pass
@ -144,14 +137,14 @@ impl serialize::Encoder for Encoder {
f(); f();
} else if name == ~"None" { } else if name == ~"None" {
self.wr.write_str(~"null"); self.wr.write_str(~"null");
} else if cnt == 0 {
self.wr.write_str(escape_str(name));
} else { } else {
self.wr.write_char('['); self.wr.write_char('[');
self.wr.write_str(escape_str(name)); self.wr.write_str(escape_str(name));
self.wr.write_char(','); self.wr.write_char(',');
self.wr.write_char('[');
f(); f();
self.wr.write_char(']'); self.wr.write_char(']');
self.wr.write_char(']');
} }
} }
@ -256,28 +249,19 @@ impl serialize::Encoder for PrettyEncoder {
f(); f();
} else if name == ~"None" { } else if name == ~"None" {
self.emit_nil(); self.emit_nil();
} else if cnt == 0 {
self.wr.write_str(escape_str(name));
} else { } else {
self.wr.write_char('['); self.wr.write_char('[');
self.indent += 2; self.indent += 2;
self.wr.write_char('\n'); self.wr.write_char('\n');
self.wr.write_str(spaces(self.indent)); self.wr.write_str(spaces(self.indent));
self.wr.write_str(escape_str(name)); self.wr.write_str(escape_str(name));
if cnt == 0 { self.wr.write_str(",\n");
self.wr.write_str(",\n"); f();
self.wr.write_str(spaces(self.indent)); self.wr.write_char('\n');
self.wr.write_str("[]\n");
} else {
self.wr.write_str(",\n");
self.wr.write_str(spaces(self.indent));
self.wr.write_str("[\n");
self.indent += 2;
f();
self.wr.write_char('\n');
self.indent -= 2;
self.wr.write_str(spaces(self.indent));
self.wr.write_str("]\n");
}
self.indent -= 2; self.indent -= 2;
self.wr.write_str(spaces(self.indent));
self.wr.write_char(']'); self.wr.write_char(']');
} }
} }
@ -1406,7 +1390,7 @@ mod tests {
let encoder = Encoder(wr); let encoder = Encoder(wr);
animal.encode(&encoder); animal.encode(&encoder);
}; };
assert_eq!(s, ~"[\"Dog\",[]]"); assert_eq!(s, ~"\"Dog\"");
} }
#[test] #[test]
@ -1417,14 +1401,7 @@ mod tests {
let encoder = PrettyEncoder(wr); let encoder = PrettyEncoder(wr);
animal.encode(&encoder); animal.encode(&encoder);
}; };
assert_eq!( assert_eq!(s, ~"\"Dog\"");
s,
~"\
[\n \
\"Dog\",\n \
[]\n\
]"
);
} }
#[test] #[test]
@ -1435,7 +1412,7 @@ mod tests {
let encoder = Encoder(wr); let encoder = Encoder(wr);
animal.encode(&encoder); animal.encode(&encoder);
}; };
assert_eq!(s, ~"[\"Frog\",[\"Henry\",349]]"); assert_eq!(s, ~"[\"Frog\",\"Henry\",349]");
} }
#[test] #[test]
@ -1451,10 +1428,8 @@ mod tests {
~"\ ~"\
[\n \ [\n \
\"Frog\",\n \ \"Frog\",\n \
[\n \ \"Henry\",\n \
\"Henry\",\n \ 349\n\
349\n \
]\n\
]" ]"
); );
} }

View file

@ -329,25 +329,27 @@ mod test {
assert_eq!( assert_eq!(
to_json_str(@tts), to_json_str(@tts),
~"[\ ~"[\
[\"tt_tok\",[null,[\"IDENT\",[\"fn\",false]]]],\ [\"tt_tok\",null,[\"IDENT\",\"fn\",false]],\
[\"tt_tok\",[null,[\"IDENT\",[\"foo\",false]]]],\ [\"tt_tok\",null,[\"IDENT\",\"foo\",false]],\
[\"tt_delim\",[\ [\
\"tt_delim\",\
[\ [\
[\"tt_tok\",[null,[\"LPAREN\",[]]]],\ [\"tt_tok\",null,\"LPAREN\"],\
[\"tt_tok\",[null,[\"IDENT\",[\"x\",false]]]],\ [\"tt_tok\",null,[\"IDENT\",\"x\",false]],\
[\"tt_tok\",[null,[\"COLON\",[]]]],\ [\"tt_tok\",null,\"COLON\"],\
[\"tt_tok\",[null,[\"IDENT\",[\"int\",false]]]],\ [\"tt_tok\",null,[\"IDENT\",\"int\",false]],\
[\"tt_tok\",[null,[\"RPAREN\",[]]]]\ [\"tt_tok\",null,\"RPAREN\"]\
]\ ]\
]],\ ],\
[\"tt_delim\",[\ [\
\"tt_delim\",\
[\ [\
[\"tt_tok\",[null,[\"LBRACE\",[]]]],\ [\"tt_tok\",null,\"LBRACE\"],\
[\"tt_tok\",[null,[\"IDENT\",[\"x\",false]]]],\ [\"tt_tok\",null,[\"IDENT\",\"x\",false]],\
[\"tt_tok\",[null,[\"SEMI\",[]]]],\ [\"tt_tok\",null,\"SEMI\"],\
[\"tt_tok\",[null,[\"RBRACE\",[]]]]\ [\"tt_tok\",null,\"RBRACE\"]\
]\ ]\
]]\ ]\
]" ]"
); );
let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts) let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts)