about summary refs log tree commit diff
path: root/src/libstd/json.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2012-05-28 12:10:32 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2012-05-28 12:14:41 -0700
commited5af70a3634915004f24c391a33cbebb8beb7bd (patch)
tree596330cb0940f3b9ccb46a8748914d5b368a81de /src/libstd/json.rs
parente45ed323c99cf105b807ca79c5561570ec5c93c8 (diff)
std: add json::to_str and json::to_json iface.
Diffstat (limited to 'src/libstd/json.rs')
-rw-r--r--src/libstd/json.rs105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/libstd/json.rs b/src/libstd/json.rs
index 82a8b5d9e8a..5d9427c72b8 100644
--- a/src/libstd/json.rs
+++ b/src/libstd/json.rs
@@ -16,6 +16,7 @@ export to_str;
 export from_reader;
 export from_str;
 export eq;
+export to_json;
 
 export num;
 export string;
@@ -498,6 +499,110 @@ fn eq(value0: json, value1: json) -> bool {
     }
 }
 
+iface to_json { fn to_json() -> json; }
+
+impl of to_json for json {
+    fn to_json() -> json { self }
+}
+
+impl of to_json for i8 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for i16 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for i32 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for i64 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for u8 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for u16 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for u32 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for u64 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for float {
+    fn to_json() -> json { num(self) }
+}
+
+impl of to_json for f32 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for f64 {
+    fn to_json() -> json { num(self as float) }
+}
+
+impl of to_json for () {
+    fn to_json() -> json { null }
+}
+
+impl of to_json for bool {
+    fn to_json() -> json { boolean(self) }
+}
+
+impl of to_json for str {
+    fn to_json() -> json { string(self) }
+}
+
+impl <A: to_json copy, B: to_json copy> of to_json for (A, B) {
+    fn to_json() -> json {
+        let (a, b) = self;
+        list([a.to_json(), b.to_json()])
+    }
+}
+
+impl <A: to_json copy, B: to_json copy, C: to_json copy>
+  of to_json for (A, B, C) {
+    fn to_json() -> json {
+        let (a, b, c) = self;
+        list([a.to_json(), b.to_json(), c.to_json()])
+    }
+}
+
+impl <A: to_json> of to_json for [A] {
+    fn to_json() -> json { list(self.map { |elt| elt.to_json() }) }
+}
+
+impl <A: to_json copy> of to_json for hashmap<str, A> {
+    fn to_json() -> json {
+        let d = map::str_hash();
+        for self.each() { |key, value|
+            d.insert(key, value.to_json());
+        }
+        dict(d)
+    }
+}
+
+impl <A: to_json> of to_json for option<A> {
+    fn to_json() -> json {
+        alt self {
+          none { null }
+          some(value) { value.to_json() }
+        }
+    }
+}
+
+impl of to_str::to_str for json {
+    fn to_str() -> str { to_str(self) }
+}
+
 #[cfg(test)]
 mod tests {
     fn mk_dict(items: [(str, json)]) -> json {