Skip to content

Commit

Permalink
Added diff support for serde_json Value
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Feb 5, 2017
1 parent 1070cd4 commit cf1934a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ rust:
- beta
- nightly
script:
- cargo test --features=with-rustc-serialize
- cargo test --all-features
- cargo doc

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ version = "0.1.0"

[dependencies]
rustc-serialize = {version = "0.3.22", optional = true}
serde_json = {version = "0.9", optional = true}

[features]
with-rustc-serialize = ["rustc-serialize"]
with-serde-json = ["serde_json"]

2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! See what's different in arbitrary data structures.
#[cfg(feature = "with-rustc-serialize")]
extern crate rustc_serialize;
#[cfg(feature = "with-serde-json")]
extern crate serde_json;


mod traitdef;
Expand Down
6 changes: 6 additions & 0 deletions src/value/json/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

mod shared;
pub use self::shared::*;

#[cfg(feature = "with-rustc-serialize")]
pub mod rustc_serialize;

#[cfg(feature = "with-serde-json")]
pub mod serde_json;
7 changes: 1 addition & 6 deletions src/value/json/rustc_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ use rustc_serialize::json::{Object, Json};
use merger::Mergeable;
use std::mem;
use std::collections::btree_map::Entry::*;

#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub enum JsonKey {
Index(usize),
String(String),
}
use super::JsonKey;

impl Value for Json {
type Item = Json;
Expand Down
19 changes: 19 additions & 0 deletions src/value/json/serde_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use traitdef::Value;
use super::JsonKey;
use serde_json::Value as Json;

impl Value for Json {
type Item = Json;
type Key = JsonKey;
fn items<'a>(&'a self) -> Option<Box<Iterator<Item = (Self::Key, &'a Self::Item)> + 'a>> {
match *self {
Json::String(_) | Json::Number(_) | Json::Bool(_) | Json::Null => None,
Json::Array(ref inner) => {
Some(Box::new(inner.iter().enumerate().map(|(i, v)| (JsonKey::Index(i), v))))
}
Json::Object(ref inner) => {
Some(Box::new(inner.iter().map(|(s, v)| (JsonKey::String(s.clone()), v))))
}
}
}
}
5 changes: 5 additions & 0 deletions src/value/json/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub enum JsonKey {
Index(usize),
String(String),
}
2 changes: 1 addition & 1 deletion tests/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate treediff;
mod diff {
extern crate rustc_serialize;
use treediff::{diff, Recorder};
use treediff::value::json::rustc_serialize::JsonKey;
use treediff::value::json::JsonKey;
use self::rustc_serialize::json::Json;
use treediff::ChangeType::*;

Expand Down
31 changes: 21 additions & 10 deletions tests/json_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
extern crate treediff;

macro_rules! make_suite {
($make:ident, $index:tt, $null:expr, $bool:expr) => {
($make:ident, $null:expr, $bool:expr) => {
use treediff::Value;
use treediff::value::json::JsonKey;

#[test]
fn scalar_values() {
for v in &["null", "true", "1.23", "-1234456", "1234456", "\"string\""] {
Expand All @@ -14,16 +17,16 @@ macro_rules! make_suite {
fn array() {
let j = $make(r#"[null, true]"#);
assert_eq!(j.items().unwrap().collect::<Vec<_>>(),
vec![($index::Index(0), &$null),
($index::Index(1), &$bool(true))]);
vec![(JsonKey::Index(0), &$null),
(JsonKey::Index(1), &$bool(true))]);
}

#[test]
fn object() {
let j = $make(r#"{"a": null, "b": true}"#);
assert_eq!(j.items().unwrap().collect::<Vec<_>>(),
vec![($index::String("a".into()), &$null),
($index::String("b".into()), &$bool(true))]);
vec![(JsonKey::String("a".into()), &$null),
(JsonKey::String("b".into()), &$bool(true))]);
}

#[test]
Expand All @@ -35,18 +38,26 @@ macro_rules! make_suite {
}


#[cfg(feature = "with-serde-json")]
mod serde_json {
extern crate serde_json;
use self::serde_json::Value as Json;

fn make_json(v: &str) -> Json {
v.parse().unwrap()
}

make_suite!(make_json, Json::Null, Json::Bool);
}

#[cfg(feature = "with-rustc-serialize")]
mod rustc_serialize {
extern crate rustc_serialize;
use treediff::value::json::rustc_serialize::JsonKey;
use self::rustc_serialize::json::Json;
use treediff::Value;

fn make_json(v: &str) -> Json {
v.parse().unwrap()
}

make_suite!(make_json, JsonKey, Json::Null, Json::Boolean);


make_suite!(make_json, Json::Null, Json::Boolean);
}

0 comments on commit cf1934a

Please sign in to comment.