Git Product home page Git Product logo

serde_json_diff's Introduction

Create machine-readable JSON diffs

Usage

Library

let a = serde_json::json!({
  "list": [1, 2, 3],
  "object": {"a": "b"}
});

let b = serde_json::json!({
  "list": [1, 2, 3],
  "object": {"a": "b"}
});

assert!(serde_json_diff::values(a, b).is_none());

โš ๏ธ Warning

The above example is just for simplicity's sake. This library allows you to extract the difference between two serde_json::Value in detail.

If all you need is to check for equivalence, it's possible to just do an equality check: ==

serde_json_diff::objects and serde_json_diff::arrays are also exposed specifically for comparing serde_json::Map<String, serde_json::Value> and Vec<serde_json::Value>s respectively.

CLI

serde_json_diff my_json_file.json my_other_json_file.json

Tip: Since the command name serde_json_diff is a bit long, I personally have it aliased in my shell config:

alias jdiff="serde_json_diff"

Example

Comparing this file:

{
  "A": "a",
  "B": "a",
  "D": 1,
  "E": 1,
  "F": [],
  "G": ["a", "a"]
}

To this file:

{
  "A": "a",
  "C": "b",
  "D": 2,
  "E": "1",
  "F": [true],
  "G": ["a", "ab"]
}

Results in this diff (serialised as JSON):

{
  "B": {
    "entry_difference": "extra"
  },
  "D": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "scalar",
      "source": 1,
      "target": 2
    }
  },
  "E": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "type",
      "source_type": "number",
      "target_type": "string",
      "target_value": "1"
    }
  },
  "F": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "array",
      "array_difference": "longer",
      "different_pairs": null,
      "missing_elements": 1
    }
  },
  "G": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "array",
      "array_difference": "pairs_only",
      "different_pairs": {
        "1": {
          "difference_of": "scalar",
          "source": "a",
          "target": "ab"
        }
      }
    }
  },
  "C": {
    "entry_difference": "missing",
    "value": "b"
  }
}

serde_json_diff's People

Contributors

teajey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.