]> git.lizzy.rs Git - rust.git/commitdiff
extra: add ToJson for TreeMap.
authorGraydon Hoare <graydon@mozilla.com>
Mon, 8 Jul 2013 23:21:09 +0000 (16:21 -0700)
committerGraydon Hoare <graydon@mozilla.com>
Thu, 11 Jul 2013 20:15:52 +0000 (13:15 -0700)
src/libextra/json.rs

index 06b6d0cb29e8af5d0ac159cb9901ca0cce3ec1b1..3567d5001bdf1c695241019b50f0f040f99affa1 100644 (file)
@@ -27,6 +27,7 @@
 use serialize::Encodable;
 use serialize;
 use sort::Sort;
+use treemap::TreeMap;
 
 /// Represents a json value
 pub enum Json {
@@ -1340,6 +1341,16 @@ fn to_json(&self) -> Json {
     }
 }
 
+impl<A:ToJson + Copy> ToJson for TreeMap<~str, A> {
+    fn to_json(&self) -> Json {
+        let mut d = HashMap::new();
+        for self.iter().advance |(key, value)| {
+            d.insert(copy *key, value.to_json());
+        }
+        Object(~d)
+    }
+}
+
 impl<A:ToJson> ToJson for Option<A> {
     fn to_json(&self) -> Json {
         match *self {