Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra::json: use a different encoding for enums. #8437

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@ impl serialize::Encoder for Encoder {
_id: uint,
cnt: uint,
f: &fn(&mut Encoder)) {
// enums are encoded as strings or vectors:
// enums are encoded as strings or objects
// Bunny => "Bunny"
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
// Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}

if cnt == 0 {
self.wr.write_str(escape_str(name));
} else {
self.wr.write_char('[');
self.wr.write_char('{');
self.wr.write_str("\"variant\"");
self.wr.write_char(':');
self.wr.write_str(escape_str(name));
self.wr.write_char(',');
self.wr.write_str("\"fields\"");
self.wr.write_str(":[");
f(self);
self.wr.write_char(']');
self.wr.write_str("]}");
}
}

Expand Down Expand Up @@ -947,14 +951,21 @@ impl serialize::Decoder for Decoder {
debug!("read_enum_variant(names=%?)", names);
let name = match self.stack.pop() {
String(s) => s,
List(list) => {
for v in list.consume_rev_iter() {
self.stack.push(v);
}
match self.stack.pop() {
String(s) => s,
value => fail!("invalid variant name: %?", value),
Object(o) => {
let n = match o.find(&~"variant").expect("invalidly encoded json") {
&String(ref s) => s.clone(),
_ => fail!("invalidly encoded json"),
};
match o.find(&~"fields").expect("invalidly encoded json") {
&List(ref l) => {
for field in l.iter() {
self.stack.push(field.clone());
}
},
_ => fail!("invalidly encoded json")
}
self.stack.push(Object(o));
n
}
ref json => fail!("invalid variant: %?", *json),
};
Expand Down Expand Up @@ -1522,7 +1533,7 @@ mod tests {
let mut encoder = Encoder(wr);
animal.encode(&mut encoder);
},
~"[\"Frog\",\"Henry\",349]"
~"{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"
);
assert_eq!(
do io::with_str_writer |wr| {
Expand Down Expand Up @@ -1926,14 +1937,14 @@ mod tests {
assert_eq!(value, Dog);

let mut decoder =
Decoder(from_str("[\"Frog\",\"Henry\",349]").unwrap());
Decoder(from_str("{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}").unwrap());
let value: Animal = Decodable::decode(&mut decoder);
assert_eq!(value, Frog(~"Henry", 349));
}

#[test]
fn test_decode_map() {
let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}";
let s = ~"{\"a\": \"Dog\", \"b\": {\"variant\":\"Frog\",\"fields\":[\"Henry\", 349]}}";
let mut decoder = Decoder(from_str(s).unwrap());
let mut map: TreeMap<~str, Animal> = Decodable::decode(&mut decoder);

Expand Down