std: change default json enum encoder to use strings or a flat vec
This commit is contained in:
parent
b10b8c3ee4
commit
4d995e66a2
2 changed files with 33 additions and 56 deletions
|
@ -119,22 +119,15 @@ impl serialize::Encoder for Encoder {
|
|||
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:
|
||||
// Some(34) => 34
|
||||
// None => null
|
||||
|
||||
// other enums are encoded as vectors:
|
||||
// other enums are encoded as strings or vectors:
|
||||
// Bunny => "Bunny"
|
||||
// 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
|
||||
// with fully-qualified option names. To get that information,
|
||||
// we'd have to change the expansion of auto-encode to pass
|
||||
|
@ -144,14 +137,14 @@ impl serialize::Encoder for Encoder {
|
|||
f();
|
||||
} else if name == ~"None" {
|
||||
self.wr.write_str(~"null");
|
||||
} else if cnt == 0 {
|
||||
self.wr.write_str(escape_str(name));
|
||||
} else {
|
||||
self.wr.write_char('[');
|
||||
self.wr.write_str(escape_str(name));
|
||||
self.wr.write_char(',');
|
||||
self.wr.write_char('[');
|
||||
f();
|
||||
self.wr.write_char(']');
|
||||
self.wr.write_char(']');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -256,28 +249,19 @@ impl serialize::Encoder for PrettyEncoder {
|
|||
f();
|
||||
} else if name == ~"None" {
|
||||
self.emit_nil();
|
||||
} else if cnt == 0 {
|
||||
self.wr.write_str(escape_str(name));
|
||||
} else {
|
||||
self.wr.write_char('[');
|
||||
self.indent += 2;
|
||||
self.wr.write_char('\n');
|
||||
self.wr.write_str(spaces(self.indent));
|
||||
self.wr.write_str(escape_str(name));
|
||||
if cnt == 0 {
|
||||
self.wr.write_str(",\n");
|
||||
self.wr.write_str(spaces(self.indent));
|
||||
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.wr.write_str(",\n");
|
||||
f();
|
||||
self.wr.write_char('\n');
|
||||
self.indent -= 2;
|
||||
self.wr.write_str(spaces(self.indent));
|
||||
self.wr.write_char(']');
|
||||
}
|
||||
}
|
||||
|
@ -1406,7 +1390,7 @@ mod tests {
|
|||
let encoder = Encoder(wr);
|
||||
animal.encode(&encoder);
|
||||
};
|
||||
assert_eq!(s, ~"[\"Dog\",[]]");
|
||||
assert_eq!(s, ~"\"Dog\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1417,14 +1401,7 @@ mod tests {
|
|||
let encoder = PrettyEncoder(wr);
|
||||
animal.encode(&encoder);
|
||||
};
|
||||
assert_eq!(
|
||||
s,
|
||||
~"\
|
||||
[\n \
|
||||
\"Dog\",\n \
|
||||
[]\n\
|
||||
]"
|
||||
);
|
||||
assert_eq!(s, ~"\"Dog\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1435,7 +1412,7 @@ mod tests {
|
|||
let encoder = Encoder(wr);
|
||||
animal.encode(&encoder);
|
||||
};
|
||||
assert_eq!(s, ~"[\"Frog\",[\"Henry\",349]]");
|
||||
assert_eq!(s, ~"[\"Frog\",\"Henry\",349]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1451,10 +1428,8 @@ mod tests {
|
|||
~"\
|
||||
[\n \
|
||||
\"Frog\",\n \
|
||||
[\n \
|
||||
\"Henry\",\n \
|
||||
349\n \
|
||||
]\n\
|
||||
\"Henry\",\n \
|
||||
349\n\
|
||||
]"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -329,25 +329,27 @@ mod test {
|
|||
assert_eq!(
|
||||
to_json_str(@tts),
|
||||
~"[\
|
||||
[\"tt_tok\",[null,[\"IDENT\",[\"fn\",false]]]],\
|
||||
[\"tt_tok\",[null,[\"IDENT\",[\"foo\",false]]]],\
|
||||
[\"tt_delim\",[\
|
||||
[\"tt_tok\",null,[\"IDENT\",\"fn\",false]],\
|
||||
[\"tt_tok\",null,[\"IDENT\",\"foo\",false]],\
|
||||
[\
|
||||
\"tt_delim\",\
|
||||
[\
|
||||
[\"tt_tok\",[null,[\"LPAREN\",[]]]],\
|
||||
[\"tt_tok\",[null,[\"IDENT\",[\"x\",false]]]],\
|
||||
[\"tt_tok\",[null,[\"COLON\",[]]]],\
|
||||
[\"tt_tok\",[null,[\"IDENT\",[\"int\",false]]]],\
|
||||
[\"tt_tok\",[null,[\"RPAREN\",[]]]]\
|
||||
[\"tt_tok\",null,\"LPAREN\"],\
|
||||
[\"tt_tok\",null,[\"IDENT\",\"x\",false]],\
|
||||
[\"tt_tok\",null,\"COLON\"],\
|
||||
[\"tt_tok\",null,[\"IDENT\",\"int\",false]],\
|
||||
[\"tt_tok\",null,\"RPAREN\"]\
|
||||
]\
|
||||
]],\
|
||||
[\"tt_delim\",[\
|
||||
],\
|
||||
[\
|
||||
\"tt_delim\",\
|
||||
[\
|
||||
[\"tt_tok\",[null,[\"LBRACE\",[]]]],\
|
||||
[\"tt_tok\",[null,[\"IDENT\",[\"x\",false]]]],\
|
||||
[\"tt_tok\",[null,[\"SEMI\",[]]]],\
|
||||
[\"tt_tok\",[null,[\"RBRACE\",[]]]]\
|
||||
[\"tt_tok\",null,\"LBRACE\"],\
|
||||
[\"tt_tok\",null,[\"IDENT\",\"x\",false]],\
|
||||
[\"tt_tok\",null,\"SEMI\"],\
|
||||
[\"tt_tok\",null,\"RBRACE\"]\
|
||||
]\
|
||||
]]\
|
||||
]\
|
||||
]"
|
||||
);
|
||||
let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue