From ed5af70a3634915004f24c391a33cbebb8beb7bd Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 28 May 2012 12:10:32 -0700 Subject: [PATCH] std: add json::to_str and json::to_json iface. --- src/libstd/json.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) 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 of to_json for (A, B) { + fn to_json() -> json { + let (a, b) = self; + list([a.to_json(), b.to_json()]) + } +} + +impl + 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 of to_json for [A] { + fn to_json() -> json { list(self.map { |elt| elt.to_json() }) } +} + +impl of to_json for hashmap { + fn to_json() -> json { + let d = map::str_hash(); + for self.each() { |key, value| + d.insert(key, value.to_json()); + } + dict(d) + } +} + +impl of to_json for option { + 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 {