Operation

Enum Operation 

Source
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 removed

In 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 preserved

In 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:

  1. Replace: If the path exists, replace the value.
  2. Add: If the path does not exist, add the value.
  3. 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.

Implementations§

Source§

impl Operation

Source

pub fn operators() -> &'static [char]

Source

pub fn from_operator(op: char) -> Option<Self>

Source

pub fn to_operator(&self) -> Option<char>

Trait Implementations§

Source§

impl Clone for Operation

Source§

fn clone(&self) -> Operation

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Operation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Operation

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Operation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Operation

Source§

fn eq(&self, other: &Operation) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Operation

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Operation

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,