Coder Social home page Coder Social logo

json-patch's People

Contributors

21pages avatar asayers avatar asmello avatar baszalmstra avatar idubrov avatar jayvdb avatar stefan-vatov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-patch's Issues

Diff generates invalid patch if an object becomes an array

There seems to be an issue with the json_patch::diff function. The path op Add is returned before Remove. Added failing test case as an example:

    async fn failing_test() {
        let left = json!({ "style": { "ref": "name" } });
        let right = json!({ "style": [{ "ref": "hello" }]});

        let patch = json_patch::diff(&left, &right);

        let mut initial = left.clone();
        json_patch::patch(&mut initial, &patch);

        assert_eq!(
            serde_json::to_string(&right).unwrap(),
            serde_json::to_string(&initial).unwrap()
        );
    }

gives output

assertion `left == right` failed
  left: "{\"style\":[{\"ref\":\"hello\"}]}"
 right: "{\"style\":{\"0\":{\"ref\":\"hello\"}}}"

If I try this same with some other diff tool eg (https://json-patch-builder-online.github.io/) it gives the patches in right order eg.:

[
    {
        "op": "remove",
        "path": "/style/ref"
    },
    {
        "op": "add",
        "path": "/style/0",
        "value": {
            "ref": "hello"
        }
    }
]

while this lib gives the Add op first.

treediff-rs dependency

Please see Byron/treediff-rs#11. Apparently treediff-rs is in maintenance mode, quoting the maintainer:

It looks like the main consumer of this library is json-patch, and I suggest to open an issue them so they contribute a fix, take over this crate, or cut the dependency to this crate in some other way.

Breaking api change between minor versions

The v1.3.0 release changed the type of a number of public fields in public structs to use Pointer instead of String. This caused my docker builds to start failing with errors that didn't show up when compiling outside of docker, since I had v1.2.0 cached locally. Please consider either reversing this change or bumping the major version

Revert Patch

Hey,
Is there a way to revert a Patch?
e.g:

fn test(doc: mut Value, p: Patch) {
	let original = doc.clone();
	json_patch::patch(&mut doc, &p).unwrap();
	json_patch::revert(&mut doc, &p).unwrap();

	assert_eq(doc, original);
} 

Adding Merge to PatchOperation

I would like to add a MergeOperation to the PatchOperation type, but I wanted to check here first to see if there was a good reason that hadn't already been done. Thanks!

Strings added via an add patch are wrapped in '"'

Code:

let mut doc = json!({ "first_name": "Andrew" });

let p = from_str(r#"[
        { "op": "test", "path": "/first_name", "value": "Andrew" },
        { "op": "add", "path": "/last_name", "value": "Marx" }
    ]"#).unwrap();

patch(&mut doc, &p).unwrap();

printf!("{}", doc.as_object().unwap().get("last_name").unwrap().as_str());

Expected Output:

Marx

Actual Output:

"Marx"

Crates:

serde: 1.0.136 (latest)
serde_derive: 1.0.136 (latest)
serde_json: 1.0.79 (latest)
json-patch: 0.2.6 (latest)

error[E0554]: #![feature] may not be used on the stable release channel

I get the following error when running cargo build after adding the json-patch dependency to the Cargo.toml file

error[E0554]: #![feature] may not be used on the stable release channel
--> /home/harindaka/.cargo/registry/src/github.com-1ecc6299db9ec823/json-patch-0.2.0/src/lib.rs:84:1
|
84 | #![feature(test)]
| ^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile json-patch.

What can I do to make it work with Rust stable? Wouldn't being unable to compile with stable be detrimental to the adoption of this awesome library?

diff() has issues with slashes in object keys

Great library, exactly what I needed! The only problem is I can't diff objects with keys containing slashes. The generated patch is invalid and can't be applied.

extern crate serde_json;
extern crate json_patch;
use serde_json::json;
use json_patch::{diff, patch};

fn main() {
	let mut json_a = json!({
		"/foo/bar": 1,
	});
	
	let json_b = json!({
		"/foo/bar": 2,
	});
	
	let p = diff(&json_a, &json_b);
	dbg!(&p);
	
	patch(&mut json_a, &p).unwrap();
	dbg!(&json_a);
}
[src/main.rs:16] &p = Patch(
    [
        Replace(
            ReplaceOperation {
                path: "//foo/bar",
                value: Number(
                    2,
                ),
            },
        ),
    ],
)
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidPointer', src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

diff() can produce ops in the wrong order

There are two possible patches which will take you from [1,2] to []:

[{"op":"remove","path":"/1"},{"op":"remove","path":"/0"}]
[{"op":"remove","path":"/0"},{"op":"remove","path":"/0"}]

The diff() function gives the following (invalid) patch:

[{"op":"remove","path":"/0"},{"op":"remove","path":"/1"}]

`diff()` produces invalid patches for root replacements

The following will fail with an invalid pointer error:

let mut a = serde_json::Value::from(0);
let b = serde_json::Value::from(1);
let patch = json_patch::diff(&a, &b);
json_patch::patch(&mut a, &patch).unwrap();

The problem is that diff() generates a replacement with / as the path, which then can't be applied.

Provide better errors

Currently, errors don't contain any context information and are pretty much useless. Need to provide the location in the document, what was missing, etc.

Memory usage tests?

Just for fun, would be interesting to see how many allocations / deallocations happen when applying patches. Not that it matters that much, but why not? There are some minor potential improvements like when we replace ~0 and ~1 in paths -- most paths don't have these!

Consider adding `Eq`

The types Patch, PatchOperation, and all of their used types are marked as PartialEq only. Although all used types (like Value) also do support Eq.

I think it would be great add Eq as well, so that people using Patch, can go for Eq in their own data-structures too.

I can create a PR if you like.

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.