about summary refs log tree commit diff
path: root/src/libstd/json.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-26 15:26:05 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-27 07:04:12 -0700
commit478e4498b76178dc0031f88f0d5ee31c5f804d0b (patch)
treee21be7cccf5b3fa4c2d05f2719e6679936096ab0 /src/libstd/json.rs
parent4d995e66a274511d70eb02abdb80da43ccf70aee (diff)
downloadrust-478e4498b76178dc0031f88f0d5ee31c5f804d0b.tar.gz
rust-478e4498b76178dc0031f88f0d5ee31c5f804d0b.zip
std: add option type directly to serialize::{En,De}code
Diffstat (limited to 'src/libstd/json.rs')
-rw-r--r--src/libstd/json.rs38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/libstd/json.rs b/src/libstd/json.rs
index d69917c9aff..bb1102be9f7 100644
--- a/src/libstd/json.rs
+++ b/src/libstd/json.rs
@@ -120,24 +120,11 @@ impl serialize::Encoder for Encoder {
     }
 
     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 strings or vectors:
+        // enums are encoded as strings or vectors:
         // Bunny => "Bunny"
         // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
 
-        // 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
-        // those along.
-
-        if name == ~"Some" {
-            f();
-        } else if name == ~"None" {
-            self.wr.write_str(~"null");
-        } else if cnt == 0 {
+        if cnt == 0 {
             self.wr.write_str(escape_str(name));
         } else {
             self.wr.write_char('[');
@@ -193,6 +180,10 @@ impl serialize::Encoder for Encoder {
     fn emit_tup_elt(&self, idx: uint, f: &fn()) {
         self.emit_vec_elt(idx, f)
     }
+
+    fn emit_option(&self, f: &fn()) { f(); }
+    fn emit_option_none(&self) { self.emit_nil(); }
+    fn emit_option_some(&self, f: &fn()) { f(); }
 }
 
 pub struct PrettyEncoder {
@@ -245,11 +236,7 @@ impl serialize::Encoder for PrettyEncoder {
 
     fn emit_enum(&self, _name: &str, f: &fn()) { f() }
     fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) {
-        if name == ~"Some" {
-            f();
-        } else if name == ~"None" {
-            self.emit_nil();
-        } else if cnt == 0 {
+        if cnt == 0 {
             self.wr.write_str(escape_str(name));
         } else {
             self.wr.write_char('[');
@@ -335,6 +322,10 @@ impl serialize::Encoder for PrettyEncoder {
     fn emit_tup_elt(&self, idx: uint, f: &fn()) {
         self.emit_vec_elt(idx, f)
     }
+
+    fn emit_option(&self, f: &fn()) { f(); }
+    fn emit_option_none(&self) { self.emit_nil(); }
+    fn emit_option_some(&self, f: &fn()) { f(); }
 }
 
 impl<S:serialize::Encoder> serialize::Encodable<S> for Json {
@@ -966,6 +957,13 @@ impl<'self> serialize::Decoder for Decoder<'self> {
             _ => fail!(~"not a list")
         }
     }
+
+    fn read_option<T>(&self, f: &fn() -> T) -> Option<T> {
+        match *self.peek() {
+            Null => { self.pop(); None }
+            _ => Some(f()),
+        }
+    }
 }
 
 impl Eq for Json {