pub enum Operation {
Insert,
Merge,
Add,
Remove,
Replace,
Test,
Auto,
}Variants§
Insert
Inserts the parsed structure into the provided JSON object.
Inserts the value into the provided JSON object at the path found during parsing. If the path already exists in the JSON object, the existing value at that path will be overwritten, potentially replacing entire objects or arrays.
Use this function when you want to set or replace a value exactly as specified, disregarding any existing data at that path.
§Arguments
json- The JSON object to insert into
§Returns
Returns Ok(())
§Examples
use serde_json::Value;
use jqesque::{Jqesque, Separator};
// Initial JSON object
let mut json_obj = serde_json::json!({
"settings": {
"theme": {
"color": "red",
"font": "Arial",
"size": 12
}
}
});
// Input string to parse
let input = "settings.theme={\"color\":\"blue\",\"font\":\"Helvetica\"}";
let separator = Separator::Dot;
let jqesque = Jqesque::from_str_with_separator(input, separator).unwrap();
// Using apply_to with no explicit operator will use the operator "Insert" and this will
// overwrite the existing "theme" object
jqesque.apply_to(&mut json_obj);
// The "theme" object is replaced entirely
let expected = serde_json::json!({
"settings": {
"theme": {
"color": "blue",
"font": "Helvetica"
}
}
});
assert_eq!(json_obj, expected);
// Note that the "size" key in the original "theme" object is removedIn this example, parse_and_insert replaces the entire "theme" object with the new value,
removing any existing keys not specified in the new value.
Merge
Merges the parsed structure into the JSON object.
This function merges the value into the provided JSON object at path found during parsing. If the path already exists in the JSON object, the existing value at that path will be merged with the new value, combining objects and arrays rather than overwriting them. Existing keys not specified in the new value are preserved.
Use this function when you want to update or extend the existing data without losing information, especially within nested objects or arrays.
§Arguments
json- The JSON object to merge into
§Returns
Returns Ok(())
§Examples
use serde_json::Value;
use jqesque::{Jqesque, Separator};
// Initial JSON object
let mut json_obj = serde_json::json!({
"settings": {
"theme": {
"color": "red",
"font": "Arial",
"size": 12
}
}
});
// Input string to parse
let input = "~settings.theme={\"color\":\"blue\",\"font\":\"Helvetica\"}";
let separator = Separator::Dot;
let jqesque = Jqesque::from_str_with_separator(input, separator).unwrap();
// Prefixing the query with the merge operator (~) will merge the new
// "theme" object with the existing one
jqesque.apply_to(&mut json_obj);
// The "theme" object is merged, updating existing keys and preserving others
let expected = serde_json::json!({
"settings": {
"theme": {
"color": "blue",
"font": "Helvetica",
"size": 12
}
}
});
assert_eq!(json_obj, expected);
// Note that the "size" key in the original "theme" object is preservedIn this example, parse_and_merge updates the "color" and "font" keys within the "theme" object,
while preserving the "size" key that was not specified in the new value.
Add
Remove
Replace
Test
Auto
Auto operation.
The Auto operation will attempt the following operations in order:
- Replace: If the path exists, replace the value.
- Add: If the path does not exist, add the value.
- Insert: If the path does not exist, insert the value.
This operation is useful when you want to update a value if it exists, or add or insert it if it does not.