Skip to content

Instantly share code, notes, and snippets.

@Isan-Rivkin
Created August 19, 2018 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Isan-Rivkin/58332977fa8e7c9a4f3169bcc0379ed3 to your computer and use it in GitHub Desktop.
Save Isan-Rivkin/58332977fa8e7c9a4f3169bcc0379ed3 to your computer and use it in GitHub Desktop.
[package]
name = "state_initial"
version = "0.1.0"
authors = ["Isan-Rivkin <isanrivkin@gmail.com>"]
[dependencies]
json-patch = "0.2.2"
serde_json = "1.0.24"
rmpv="0.4.0"
rmp-serde = "0.13.7"
serde="1.0.70"
serde_derive = "1.0.70"
[dependencies.rmp]
rmp = "^0.8"
#[macro_use]
extern crate serde_json;
extern crate json_patch;
#[macro_use]
extern crate serde_derive;
extern crate rmp;
extern crate rmpv;
extern crate serde;
extern crate rmp_serde as rmps;
// msgpck
use rmpv::Value;
use serde::{Deserialize, Serialize};
use rmps::{Deserializer, Serializer};
// jsonpatch
use json_patch::{patch, diff, from_value};
use serde_json::from_str;
use json_patch::merge;
fn create_and_patch_document_using_json_patch(){
let mut doc = json!([
{ "name": "Andrew" },
{ "name": "Maxim" }
]);
let p = from_str(r#"[
{ "op": "test", "path": "/0/name", "value": "Andrew" },
{ "op": "add", "path": "/0/happy", "value": true }
]"#).unwrap();
patch(&mut doc, &p).unwrap();
assert_eq!(doc, json!([
{ "name": "Andrew", "happy": true },
{ "name": "Maxim" }
]));
}
fn create_and_patch_document_using_json_merge_patch(){
let mut doc = json!({
"title": "Goodbye!",
"author" : {
"givenName" : "John",
"familyName" : "Doe"
},
"tags":[ "example", "sample" ],
"content": "This will be unchanged"
});
let patch = json!({
"title": "Hello!", // replace
"phoneNumber": "+01-123-456-7890", // add new
"author": { // remove familyName param completley
"familyName": null
},
"tags": [ "example" ] //remove sample
});
merge(&mut doc, &patch);
assert_eq!(doc, json!({
"title": "Hello!",
"author" : {
"givenName" : "John"
},
"tags": [ "example" ],
"content": "This will be unchanged",
"phoneNumber": "+01-123-456-7890"
}));
}
fn diff_2_jsons(){
let left = json!({
"title": "Goodbye!",
"author" : {
"givenName" : "John",
"familyName" : "Doe"
},
"tags":[ "example", "sample" ],
"content": "This will be unchanged"
});
let right = json!({
"title": "Hello!",
"author" : {
"givenName" : "John"
},
"tags": [ "example" ],
"content": "This will be unchanged",
"phoneNumber": "+01-123-456-7890"
});
let p = diff(&left, &right);
assert_eq!(p, from_value(json!([
{ "op": "remove", "path": "/author/familyName" },
{ "op": "remove", "path": "/tags/1" },
{ "op": "replace", "path": "/title", "value": "Hello!" },
{ "op": "add", "path": "/phoneNumber", "value": "+01-123-456-7890" },
])).unwrap());
let mut doc = left.clone();
patch(&mut doc, &p).unwrap();
assert_eq!(doc, right);
}
fn msgpack_encode_decode(){
let mut doc = json!([
{ "name": "Andrew" },
{ "name": "Maxim" }
]);
// serialize
let mut buf = Vec::new();
doc.serialize(&mut Serializer::new(&mut buf)).unwrap();
println!("{:?}",buf );
// deserialize
let mut pre_de = Deserializer::new(&buf[..]);
let json : serde_json::Value = Deserialize::deserialize(&mut pre_de).unwrap();
println!("{}",json[0]["name"] );
assert_eq!(doc, json);
}
fn main() {
create_and_patch_document_using_json_patch();
create_and_patch_document_using_json_merge_patch();
diff_2_jsons();
msgpack_encode_decode();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment