Skip to content

Commit

Permalink
extra: switch json from hashmaps to treemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Jul 23, 2013
1 parent 9e4ebdb commit fa8553e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum Json {
}

pub type List = ~[Json];
pub type Object = HashMap<~str, Json>;
pub type Object = TreeMap<~str, Json>;

#[deriving(Eq)]
/// If an error occurs while parsing some JSON, this is the structure which is
Expand Down Expand Up @@ -809,7 +809,7 @@ impl<T : iterator::Iterator<char>> Parser<T> {
self.bump();
self.parse_whitespace();

let mut values = ~HashMap::new();
let mut values = ~TreeMap::new();

if self.ch == '}' {
self.bump();
Expand Down Expand Up @@ -1087,7 +1087,7 @@ impl serialize::Decoder for Decoder {
let len = match self.stack.pop() {
Object(obj) => {
let len = obj.len();
for obj.consume().advance |(key, value)| {
for obj.consume_iter().advance |(key, value)| {
self.stack.push(value);
self.stack.push(String(key));
}
Expand Down Expand Up @@ -1294,19 +1294,19 @@ impl<A:ToJson> ToJson for ~[A] {
fn to_json(&self) -> Json { List(self.map(|elt| elt.to_json())) }
}

impl<A:ToJson> ToJson for HashMap<~str, A> {
impl<A:ToJson> ToJson for TreeMap<~str, A> {
fn to_json(&self) -> Json {
let mut d = HashMap::new();
let mut d = TreeMap::new();
for self.iter().advance |(key, value)| {
d.insert((*key).clone(), value.to_json());
}
Object(~d)
}
}

impl<A:ToJson> ToJson for TreeMap<~str, A> {
impl<A:ToJson> ToJson for HashMap<~str, A> {
fn to_json(&self) -> Json {
let mut d = HashMap::new();
let mut d = TreeMap::new();
for self.iter().advance |(key, value)| {
d.insert((*key).clone(), value.to_json());
}
Expand Down Expand Up @@ -1338,11 +1338,11 @@ mod tests {

use super::*;

use std::hashmap::HashMap;
use std::io;
use std::result;

use extra::serialize::Decodable;
use serialize::Decodable;
use treemap::TreeMap;

#[deriving(Eq, Encodable, Decodable)]
enum Animal {
Expand All @@ -1363,7 +1363,7 @@ mod tests {
}

fn mk_object(items: &[(~str, Json)]) -> Json {
let mut d = ~HashMap::new();
let mut d = ~TreeMap::new();

for items.iter().advance |item| {
match *item {
Expand Down Expand Up @@ -1954,7 +1954,7 @@ mod tests {
fn test_decode_map() {
let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}";
let mut decoder = Decoder(from_str(s).unwrap());
let mut map: HashMap<~str, Animal> = Decodable::decode(&mut decoder);
let mut map: TreeMap<~str, Animal> = Decodable::decode(&mut decoder);
assert_eq!(map.pop(&~"a"), Some(Dog));
assert_eq!(map.pop(&~"b"), Some(Frog(~"Henry", 349)));
Expand Down
3 changes: 1 addition & 2 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use std::task;
use std::to_str::ToStr;
use std::u64;
use std::f64;
use std::hashmap::HashMap;
use std::os;


Expand Down Expand Up @@ -852,7 +851,7 @@ fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {

impl ToJson for Metric {
fn to_json(&self) -> json::Json {
let mut map = ~HashMap::new();
let mut map = ~TreeMap::new();
map.insert(~"value", json::Number(self.value as float));
map.insert(~"noise", json::Number(self.noise as float));
json::Object(map)
Expand Down
3 changes: 1 addition & 2 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use treemap::TreeMap;
use std::cell::Cell;
use std::comm::{PortOne, oneshot, send_one, recv_one};
use std::either::{Either, Left, Right};
use std::hashmap::HashMap;
use std::io;
use std::result;
use std::run;
Expand Down Expand Up @@ -381,7 +380,7 @@ fn test() {
}

let cx = Context::new(RWARC(Database::new(Path("db.json"))),
Logger::new(), HashMap::new());
Logger::new(), TreeMap::new());

let s = do cx.with_prep("test1") |prep| {
prep.declare_input("file", pth.to_str(), digest_file(&pth));
Expand Down

0 comments on commit fa8553e

Please sign in to comment.