RawJsonMember

Struct RawJsonMember 

Source
pub struct RawJsonMember<'text, 'raw, 'a> { /* private fields */ }
Expand description

Represents a member access result for a JSON object.

This struct is returned by RawJsonValue::to_member() and allows you to handle both present and missing object members. It wraps an optional value that is Some if the member exists and None if it doesn’t.

§Examples

let json = RawJson::parse(r#"{"name": "Alice", "age": 30}"#)?;
let obj = json.value();

// Access an existing member
let name_member = obj.to_member("name")?;
let name: String = name_member.required()?.try_into()?;
assert_eq!(name, "Alice");

// Access a missing member
let city_member = obj.to_member("city")?;
let city: Option<String> = city_member.try_into()?;
assert_eq!(city, None);

Implementations§

Source§

impl<'text, 'raw, 'a> RawJsonMember<'text, 'raw, 'a>

Source

pub fn required(self) -> Result<RawJsonValue<'text, 'raw>, JsonParseError>

Returns the member value if it exists, or an error if it’s missing.

This method is useful when you need to ensure that a required member is present in the JSON object.

§Examples
let json = RawJson::parse(r#"{"name": "Alice"}"#)?;
let obj = json.value();

// Required member exists
let name = obj.to_member("name")?.required()?;
assert_eq!(name.to_unquoted_string_str()?, "Alice");

// Required member missing - returns error
let age_result = obj.to_member("age")?.required();
assert!(age_result.is_err());
Source

pub fn get(self) -> Option<RawJsonValue<'text, 'raw>>

Returns the inner raw JSON value as an Option.

This method provides direct access to the underlying Option<RawJsonValue>, allowing you to handle the presence or absence of the member yourself.

§Examples
let json = RawJson::parse(r#"{"name": "Alice", "age": 30}"#)?;
let obj = json.value();

// Existing member
let name_member = obj.to_member("name")?;
if let Some(name_value) = name_member.get() {
    assert_eq!(name_value.to_unquoted_string_str()?, "Alice");
}

// Missing member
let city_member = obj.to_member("city")?;
assert!(city_member.get().is_none());

// Using with pattern matching
match obj.to_member("age")?.get() {
    Some(age_value) => {
        let age: i32 = age_value.as_integer_str()?.parse()
            .map_err(|e| age_value.invalid(e))?;
        assert_eq!(age, 30);
    }
    None => println!("Age not provided"),
}
Source

pub fn map<F, T>(self, f: F) -> Result<Option<T>, JsonParseError>
where F: FnOnce(RawJsonValue<'text, 'raw>) -> Result<T, JsonParseError>,

Applies a transformation function to the member value if it exists.

This method is similar to Option::map, but designed for transformations that can fail with a JsonParseError. If the member exists, the function is applied to its value. If the member doesn’t exist, Ok(None) is returned.

§Examples
let json = RawJson::parse(r#"{"name": "Alice", "age": "30"}"#)?;
let obj = json.value();

// Transform existing member
let age_member = obj.to_member("age")?;
let age: Option<i32> = age_member.map(|v| {
    v.to_unquoted_string_str()?.parse().map_err(|e| v.invalid(e))
})?;
assert_eq!(age, Some(30));

// Transform missing member
let city_member = obj.to_member("city")?;
let city: Option<String> = city_member.map(|v| v.try_into())?;
assert_eq!(city, None);

This is particularly useful when you need to perform parsing or validation on optional members without having to handle the Option separately:

let json = RawJson::parse(r#"{"score": "95.5"}"#)?;
let obj = json.value();

// Parse optional numeric string
let score: Option<f64> = obj.to_member("score")?.map(|v| {
    v.to_unquoted_string_str()?.parse().map_err(|e| v.invalid(e))
})?;
assert_eq!(score, Some(95.5));

Trait Implementations§

Source§

impl<'text, 'raw, 'a> Clone for RawJsonMember<'text, 'raw, 'a>

Source§

fn clone(&self) -> RawJsonMember<'text, 'raw, 'a>

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<'text, 'raw, 'a> Debug for RawJsonMember<'text, 'raw, 'a>

Source§

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

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

impl<'text, 'raw, 'a> Hash for RawJsonMember<'text, 'raw, 'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'text, 'raw, 'a> Ord for RawJsonMember<'text, 'raw, 'a>

Source§

fn cmp(&self, other: &RawJsonMember<'text, 'raw, 'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'text, 'raw, 'a> PartialEq for RawJsonMember<'text, 'raw, 'a>

Source§

fn eq(&self, other: &RawJsonMember<'text, 'raw, 'a>) -> 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<'text, 'raw, 'a> PartialOrd for RawJsonMember<'text, 'raw, 'a>

Source§

fn partial_cmp( &self, other: &RawJsonMember<'text, 'raw, 'a>, ) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'text, 'raw, 'a, T> TryFrom<RawJsonMember<'text, 'raw, 'a>> for Option<T>
where T: TryFrom<RawJsonValue<'text, 'raw>, Error = JsonParseError>,

Source§

type Error = JsonParseError

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

fn try_from(value: RawJsonMember<'text, 'raw, 'a>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'text, 'raw, 'a> Copy for RawJsonMember<'text, 'raw, 'a>

Source§

impl<'text, 'raw, 'a> Eq for RawJsonMember<'text, 'raw, 'a>

Source§

impl<'text, 'raw, 'a> StructuralPartialEq for RawJsonMember<'text, 'raw, 'a>

Auto Trait Implementations§

§

impl<'text, 'raw, 'a> Freeze for RawJsonMember<'text, 'raw, 'a>

§

impl<'text, 'raw, 'a> RefUnwindSafe for RawJsonMember<'text, 'raw, 'a>

§

impl<'text, 'raw, 'a> Send for RawJsonMember<'text, 'raw, 'a>

§

impl<'text, 'raw, 'a> Sync for RawJsonMember<'text, 'raw, 'a>

§

impl<'text, 'raw, 'a> Unpin for RawJsonMember<'text, 'raw, 'a>

§

impl<'text, 'raw, 'a> UnwindSafe for RawJsonMember<'text, 'raw, 'a>

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, 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.