Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json"
version = "0.6.1"
version = "0.7.0"
authors = ["Maciej Hirsz <[email protected]>"]
description = "JSON implementation in Rust"
repository = "https://github.com/maciejhirsz/json-rust"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ let data = json::parse(r#"

"#).unwrap();

assert!(data["code"].is(200));
assert!(data["success"].is(true));
assert!(data["code"] == 200);
assert!(data["success"] == true));
assert!(data["payload"]["features"].is_array());
assert!(data["payload"]["features"][0].is("awesome"));
assert!(data["payload"]["features"][0] == "awesome");
assert!(data["payload"]["features"].contains("easyAPI"));

// Error resilient: non-existent values default to null
Expand Down
96 changes: 93 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
//!
//! "#).unwrap();
//!
//! assert!(data["code"].is(200));
//! assert!(data["success"].is(true));
//! assert!(data["code"] == 200);
//! assert!(data["success"] == true);
//! assert!(data["payload"]["features"].is_array());
//! assert!(data["payload"]["features"][0].is("awesome"));
//! assert!(data["payload"]["features"][0] == "awesome");
//! assert!(data["payload"]["features"].contains("easyAPI"));
//!
//! // Error resilient: non-existent values default to null
Expand Down Expand Up @@ -333,6 +333,33 @@ macro_rules! implement {
}
}

impl PartialEq<$from> for JsonValue {
fn eq(&self, other: &$from) -> bool {
match *self {
JsonValue::$to(ref value) => value == &(*other as $wanted),
_ => false
}
}
}

impl<'a> PartialEq<$from> for &'a JsonValue {
fn eq(&self, other: &$from) -> bool {
match **self {
JsonValue::$to(ref value) => value == &(*other as $wanted),
_ => false
}
}
}

impl PartialEq<JsonValue> for $from {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::$to(ref value) => value == &(*self as $wanted),
_ => false
}
}
}

implement_extras!($from);
};
($to:ident, $from:ty) => {
Expand All @@ -342,6 +369,33 @@ macro_rules! implement {
}
}

impl PartialEq<$from> for JsonValue {
fn eq(&self, other: &$from) -> bool {
match *self {
JsonValue::$to(ref value) => value == other,
_ => false
}
}
}

impl<'a> PartialEq<$from> for &'a JsonValue {
fn eq(&self, other: &$from) -> bool {
match **self {
JsonValue::$to(ref value) => value == other,
_ => false
}
}
}

impl PartialEq<JsonValue> for $from {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::$to(ref value) => value == self,
_ => false
}
}
}

implement_extras!($from);
}
}
Expand Down Expand Up @@ -391,6 +445,42 @@ impl From<Option<JsonValue>> for JsonValue {
}
}

impl<'a> PartialEq<&'a str> for JsonValue {
fn eq(&self, other: &&str) -> bool {
match *self {
JsonValue::String(ref value) => value == *other,
_ => false
}
}
}

impl<'a> PartialEq<JsonValue> for &'a str {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::String(ref value) => value == *self,
_ => false
}
}
}

impl PartialEq<str> for JsonValue {
fn eq(&self, other: &str) -> bool {
match *self {
JsonValue::String(ref value) => value == other,
_ => false
}
}
}

impl<'a> PartialEq<JsonValue> for str {
fn eq(&self, other: &JsonValue) -> bool {
match *other {
JsonValue::String(ref value) => value == self,
_ => false
}
}
}

implement!(String, String);
implement!(Number, isize as f64);
implement!(Number, usize as f64);
Expand Down
3 changes: 2 additions & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl JsonValue {
}

/// Checks if the value stored matches `other`.
#[deprecated(since="0.7.0", note="Use `value == other` instead")]
pub fn is<T>(&self, other: T) -> bool where T: Into<JsonValue> {
*self == other.into()
}
Expand Down Expand Up @@ -151,7 +152,7 @@ impl JsonValue {
}
}

#[deprecated(since="0.6.1", note="Unnecessary, use `as_bool` instead")]
#[deprecated(since="0.6.1", note="Use `as_bool` instead")]
pub fn as_boolean(&self) -> JsonResult<&bool> {
match *self {
JsonValue::Boolean(ref value) => Ok(value),
Expand Down
62 changes: 31 additions & 31 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ fn is_as_boolean() {
fn is_true() {
let boolean = JsonValue::Boolean(true);

assert!(boolean.is(true));
assert_eq!(boolean, true);
}

#[test]
fn is_false() {
let boolean = JsonValue::Boolean(false);

assert!(boolean.is(false));
assert_eq!(boolean, false);
}

#[test]
Expand Down Expand Up @@ -268,12 +268,12 @@ fn object_dump_pretty() {

#[test]
fn parse_true() {
assert!(parse("true").unwrap().is(true));
assert_eq!(parse("true").unwrap(), true);
}

#[test]
fn parse_false() {
assert!(parse("false").unwrap().is(false));
assert_eq!(parse("false").unwrap(), false);
}

#[test]
Expand All @@ -283,35 +283,35 @@ fn parse_null() {

#[test]
fn parse_number() {
assert!(parse("3.14").unwrap().is(3.14))
assert_eq!(parse("3.14").unwrap(), 3.14);
}

#[test]
fn parse_integer() {
assert!(parse("42").unwrap().is(42));
assert_eq!(parse("42").unwrap(), 42);
}

#[test]
fn parse_negative_integer() {
assert!(parse("-42").unwrap().is(-42));
assert_eq!(parse("-42").unwrap(), -42);
}

#[test]
fn parse_number_with_e() {
assert!(parse("5e2").unwrap().is(500));
assert!(parse("5E2").unwrap().is(500));
assert_eq!(parse("5e2").unwrap(), 500);
assert_eq!(parse("5E2").unwrap(), 500);
}

#[test]
fn parse_number_with_positive_e() {
assert!(parse("5e+2").unwrap().is(500));
assert!(parse("5E+2").unwrap().is(500));
assert_eq!(parse("5e+2").unwrap(), 500);
assert_eq!(parse("5E+2").unwrap(), 500);
}

#[test]
fn parse_number_with_negative_e() {
assert!(parse("5e-2").unwrap().is(0.05));
assert!(parse("5E-2").unwrap().is(0.05));
assert_eq!(parse("5e-2").unwrap(), 0.05);
assert_eq!(parse("5E-2").unwrap(), 0.05);
}

#[test]
Expand Down Expand Up @@ -382,7 +382,7 @@ fn parse_and_index_from_object() {
let data = parse("{ \"pi\": 3.14 }").unwrap();
let ref pi = data["pi"];

assert!(pi.is(3.14));
assert_eq!(pi, 3.14);
}

#[test]
Expand All @@ -395,11 +395,11 @@ fn parse_and_index_mut_from_object() {

"#).unwrap();

assert!(data["foo"].is(100));
assert_eq!(data["foo"], 100);

data["foo"] = 200.into();

assert!(data["foo"].is(200));
assert_eq!(data["foo"], 200);
}

#[test]
Expand All @@ -414,7 +414,7 @@ fn parse_and_index_mut_from_null() {
data["foo"]["bar"] = 100.into();

assert!(data.is_object());
assert!(data["foo"]["bar"].is(100));
assert_eq!(data["foo"]["bar"], 100);

assert_eq!(data.dump(), r#"{"foo":{"bar":100}}"#);
}
Expand All @@ -423,12 +423,12 @@ fn parse_and_index_mut_from_null() {
fn parse_and_index_from_array() {
let data = parse(r#"[100, 200, false, null, "foo"]"#).unwrap();

assert!(data[0].is(100));
assert!(data[1].is(200));
assert!(data[2].is(false));
assert!(data[3].is_null());
assert!(data[4].is("foo"));
assert!(data[5].is_null());
assert_eq!(data[0], 100);
assert_eq!(data[1], 200);
assert_eq!(data[2], false);
assert_eq!(data[3], Null);
assert_eq!(data[4], "foo");
assert_eq!(data[5], Null);
}

#[test]
Expand All @@ -441,8 +441,8 @@ fn parse_and_index_mut_from_array() {
data[3] = "modified".into();
data[5] = "implicid push".into();

assert!(data[3].is("modified"));
assert!(data[5].is("implicid push"));
assert_eq!(data[3], "modified");
assert_eq!(data[5], "implicid push");
}

#[test]
Expand All @@ -454,7 +454,7 @@ fn parse_escaped_characters() {
"#).unwrap();

assert!(data.is_string());
assert!(data.is("\r\n\t\u{8}\u{c}\\/\""));
assert_eq!(data, "\r\n\t\u{8}\u{c}\\/\"");
}

#[test]
Expand All @@ -465,7 +465,7 @@ fn parse_escaped_unicode() {

"#).unwrap();

assert!(data.is("❤️"));
assert_eq!(data, "❤️");
}

#[test]
Expand Down Expand Up @@ -526,11 +526,11 @@ fn iter_entries() {

let (key, value) = entries.next().unwrap();
assert_eq!(key, "a");
assert!(value.is(1));
assert_eq!(value, 1);

let (key, value) = entries.next().unwrap();
assert_eq!(key, "b");
assert!(value.is("foo"));
assert_eq!(value, "foo");

assert!(entries.next().is_none());
}
Expand Down Expand Up @@ -563,8 +563,8 @@ fn iter_members() {

let mut members = data.members();

assert!(members.next().unwrap().is(1));
assert!(members.next().unwrap().is("foo"));
assert_eq!(members.next().unwrap(), 1);
assert_eq!(members.next().unwrap(), "foo");
assert!(members.next().is_none());
}

Expand Down