Skip to content
Easily compare two JSON values and get great output
Rust Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Support both exact and inclusive matching Nov 16, 2018
src
tests accept trailing comma Feb 15, 2019
.gitignore Initial commit Oct 17, 2018
.travis.yml Add travis ci config Nov 12, 2018
CHANGELOG.md Simplify internal diffing algorithm Jan 25, 2020
Cargo.toml Simplify internal diffing algorithm Jan 25, 2020
LICENSE Add LICENSE file (#5) Oct 24, 2019
README.md Bump version to 1.0.1 Oct 24, 2019

README.md

Build Status

assert-json-diff

This crate includes macros for comparing two JSON values. It is designed to give much more helpful error messages than the standard assert_eq!. It basically does a diff of the two objects and tells you the exact differences. This is useful when asserting that two large JSON objects are the same.

It uses the serde_json::Value type to represent JSON.

Install

[dependencies]
assert-json-diff = "1"

Partial matching

If you want to assert that one JSON value is "included" in another use assert_json_include:

#[macro_use]
extern crate assert_json_diff;
#[macro_use]
extern crate serde_json;

fn main() {
    let a = json!({
        "data": {
            "users": [
                {
                    "id": 1,
                    "country": {
                        "name": "Denmark"
                    }
                },
                {
                    "id": 24,
                    "country": {
                        "name": "Denmark"
                    }
                }
            ]
        }
    });

    let b = json!({
        "data": {
            "users": [
                {
                    "id": 1,
                    "country": {
                        "name": "Sweden"
                    }
                },
                {
                    "id": 2,
                    "country": {
                        "name": "Denmark"
                    }
                }
            ]
        }
    });

    assert_json_include!(actual: a, expected: b)
}

This will panic with the error message:

json atoms at path ".data.users[0].country.name" are not equal:
    expected:
        "Sweden"
    actual:
        "Denmark"

json atoms at path ".data.users[1].id" are not equal:
    expected:
        2
    actual:
        24

assert_json_include allows extra data in actual but not in expected. That is so you can verify just a part of the JSON without having to specify the whole thing. For example this test passes:

#[macro_use]
extern crate assert_json_diff;
#[macro_use]
extern crate serde_json;

fn main() {
    assert_json_include!(
        actual: json!({
            "a": { "b": 1 },
        }),
        expected: json!({
            "a": {},
        })
    )
}

However expected cannot contain additional data so this test fails:

#[macro_use]
extern crate assert_json_diff;
#[macro_use]
extern crate serde_json;

fn main() {
    assert_json_include!(
        actual: json!({
            "a": {},
        }),
        expected: json!({
            "a": { "b": 1 },
        })
    )
}

That will print

json atom at path ".a.b" is missing from actual

Exact matching

If you want to ensure two JSON values are exactly the same, use assert_json_eq.

#[macro_use]
extern crate assert_json_diff;
#[macro_use]
extern crate serde_json;

fn main() {
    assert_json_eq!(
        json!({ "a": { "b": 1 } }),
        json!({ "a": {} })
    )
}

This will panic with the error message:

json atom at path ".a.b" is missing from lhs

License: MIT

You can’t perform that action at this time.