Skip to content

Commit 7659614

Browse files
committed
fix typos in sample code, add enum to json encoder, add test case
1 parent 19dfec2 commit 7659614

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

src/libstd/json.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,15 @@ pub impl Encoder: serialize::Encoder {
122122
fn emit_managed(&self, f: fn()) { f() }
123123

124124
fn emit_enum(&self, name: &str, f: fn()) {
125-
if name != "option" { die!(~"only supports option enum") }
126-
f()
125+
// emitting enums as arrays where the first
126+
// element provides the enum variant name
127+
self.wr.write_char('[');
128+
self.wr.write_str(name);
129+
self.wr.write_char(',');
130+
f();
131+
self.wr.write_char(']');
127132
}
133+
128134
fn emit_enum_variant(&self, _name: &str, id: uint, _cnt: uint, f: fn()) {
129135
if id == 0 {
130136
self.emit_nil();

src/libsyntax/ext/auto_encode.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ For example, a type like:
2323
2424
would generate two implementations like:
2525
26-
impl<S: Encoder> node_id: Encodable<S> {
27-
fn encode(s: &S) {
28-
do s.emit_struct("Node", 1) {
29-
s.emit_field("id", 0, || s.emit_uint(self))
30-
}
26+
impl<S: std::serialize::Encoder> Node: Encodable<S> {
27+
fn encode(&self, s: &S) {
28+
do s.emit_struct("Node", 1) {
29+
s.emit_field("id", 0, || s.emit_uint(self.id))
3130
}
3231
}
32+
}
3333
34-
impl<D: Decoder> node_id: Decodable {
35-
static fn decode(d: &D) -> Node {
36-
do d.read_struct("Node", 1) {
37-
Node {
38-
id: d.read_field(~"x", 0, || decode(d))
39-
}
34+
impl<D: Decoder> node_id: Decodable {
35+
static fn decode(d: &D) -> Node {
36+
do d.read_struct("Node", 1) {
37+
Node {
38+
id: d.read_field(~"x", 0, || decode(d))
4039
}
4140
}
4241
}
42+
}
4343
4444
Other interesting scenarios are whe the item has type parameters or
4545
references other non-built-in types. A type definition like:
@@ -1150,3 +1150,42 @@ fn mk_enum_deser_body(
11501150
]
11511151
)
11521152
}
1153+
1154+
1155+
#[cfg(test)]
1156+
mod test {
1157+
use std::serialize::Encodable;
1158+
use core::dvec::*;
1159+
use util::testing::*;
1160+
use core::io;
1161+
use core::str;
1162+
use std;
1163+
1164+
1165+
#[auto_decode]
1166+
#[auto_encode]
1167+
struct Node {id: uint}
1168+
1169+
fn to_json_str (val: Encodable<std::json::Encoder>) -> ~str{
1170+
let bw = @io::BytesWriter {bytes: DVec(), pos: 0};
1171+
val.encode(~std::json::Encoder(bw as io::Writer));
1172+
str::from_bytes(bw.bytes.data)
1173+
}
1174+
1175+
#[test] fn encode_test () {
1176+
check_equal (to_json_str(Node{id:34} as Encodable::<std::json::Encoder>),~"{\"id\":34}");
1177+
}
1178+
1179+
#[auto_encode]
1180+
enum written {
1181+
Book(int),
1182+
Magazine(~str)
1183+
}
1184+
1185+
#[test] fn json_enum_encode_test () {
1186+
check_equal (to_json_str(Book(9) as Encodable::<std::json::Encoder>),
1187+
~"[\"Book\",9]");
1188+
check_equal (to_json_str(Magazine(~"Paris Match") as Encodable::<std::json::Encoder>),
1189+
~"[\"Magazine\",\"Paris Match\"]");
1190+
}
1191+
}

0 commit comments

Comments
 (0)