idubrov / json-patch Goto Github PK
View Code? Open in Web Editor NEWRFC 6902 (JSON Patch) / RFC 7396 (JSON Merge Patch) implementation for Rust
License: Apache License 2.0
RFC 6902 (JSON Patch) / RFC 7396 (JSON Merge Patch) implementation for Rust
License: Apache License 2.0
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.
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.
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
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);
}
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!
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)
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?
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.
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"}]
How would you diff a left and right hand side json and create patches?
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.
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.
Avoid cloning values from patch operations like "add"/"replace". Instead, accept patch operations as values and reuse their values.
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!
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.