Struct JSONValue

Source
pub struct JSONValue<'a> {
    pub value_type: JSONValueType,
    /* private fields */
}

Fields§

§value_type: JSONValueType

Implementations§

Source§

impl<'a> JSONValue<'a>

Source

pub fn load(contents: &'a str) -> JSONValue<'_>

Create a new JSONValue from an input string

This is the primary method of constructing a JSONValue. It cannot fail, although the value might have type JSONValueType::Error. However, a malformed payload may have a type that is not JSONValueType::Error.

If you want to load the payload and verify that it is valid JSON, use JSONValue::load_and_verify.

Source

pub fn verify(&self) -> Result<(), JSONParsingError>

Confirm that this JSONValue is proper JSON

This will scan through the entire JSON and confirm that it is properly formatted. See also JSONValue::load_and_verify.

§Example
let value = JSONValue::load("[1,{},\"foo\"]");
assert!(value.verify().is_ok());

let value = JSONValue::load("[,,{\"");
assert!(value.verify().is_err());
Source

pub fn load_and_verify( contents: &'a str, ) -> Result<JSONValue<'_>, JSONParsingError>

Load a JSON value from a payload and verify that it is valid JSON.

This is equivalent to calling JSONValue::load followed by JSONValue::verify.

Source

pub fn is_null(&self) -> bool

Checks if the JSONValue is null

If the value is malformed, returns false.

§Example
let value = JSONValue::load("null");
assert_eq!(value.is_null(), true);

let value = JSONValue::load("1293");
assert_eq!(value.is_null(), false);
Source

pub fn read_boolean(&self) -> Result<bool, JSONParsingError>

Reads the JSONValue as a boolean

If the type is not a [JSONValueType::Boolean], returns an Err.

§Example
let value = JSONValue::load("true");
assert_eq!(value.read_boolean(), Ok(true));

let value = JSONValue::load("f");
assert_eq!(value.read_boolean(), Err(JSONParsingError::CannotParseBoolean));
Source

pub fn read_integer(&self) -> Result<isize, JSONParsingError>

Reads the JSONValue as an integer

If the type is not a JSONValueType::Number, returns an Err.

§Example
let value = JSONValue::load("-24");
assert_eq!(value.read_integer(), Ok(-24));

let value = JSONValue::load("5pi");
assert_eq!(value.read_integer(), Err(JSONParsingError::CannotParseInteger));
Source

pub fn read_float(&self) -> Result<f32, JSONParsingError>

Reads the JSONValue as a float

If the type is not a JSONValueType::Number, returns an Err.

§Example
let value = JSONValue::load("2.4");
assert_eq!(value.read_float(), Ok(2.4));

let value = JSONValue::load("5pi");
assert_eq!(value.read_float(), Err(JSONParsingError::CannotParseFloat));
Source

pub fn read_string(&self) -> Result<&'a str, JSONParsingError>

Read the JSONValue as a string

This returns an unescaped string (actually a slice into the underlying bytes). If you need escape sequences to be handled, use JSONValue::iter_string.

§Example
let value = JSONValue::load("\"this is a string\"");
assert_eq!(value.read_string(), Ok("this is a string"));
Source

pub fn iter_array(&self) -> Result<JSONArrayIterator<'a>, JSONParsingError>

Constructs an iterator over this array value

If the value is not an JSONValueType::Array, returns an error.

Source

pub fn iter_string(&self) -> Result<EscapedStringIterator<'a>, JSONParsingError>

Constructs an iterator over this string

If the value is not an JSONValueType::String, returns an error.

The iterator returns Result<char, JSONParsingError>s and handles escape sequences. You can convert this into a Result<String, _> using collect.

§Example
let value = JSONValue::load(r#" "\u27FC This is a string with unicode \u27FB""#);
let string : Result<String, _> = value.iter_string().unwrap().collect::<Result<String, _>>();
assert_eq!(string.unwrap(), "⟼ This is a string with unicode ⟻")
Source

pub fn iter_object(&self) -> Result<JSONObjectIterator<'a>, JSONParsingError>

Constructs an iterator over this object

If the value is not an JSONValueType::Object, returns an error.

Source

pub fn get_key_value( &self, key: &str, ) -> Result<JSONValue<'_>, JSONParsingError>

Searches this object for a key and returns it’s value

Like the function Iterator::nth, this searches linearly through all the keys in the object to find the desired one. If parsing the entire object in an arbitrary order, then, prefer using JSONValue::iter_object.

Will return Err(JSONParsingError::KeyNotFound) if the key is not present.

Trait Implementations§

Source§

impl<'a> Clone for JSONValue<'a>

Source§

fn clone(&self) -> JSONValue<'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<'a> Debug for JSONValue<'a>

Source§

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

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

impl<'a> Copy for JSONValue<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for JSONValue<'a>

§

impl<'a> RefUnwindSafe for JSONValue<'a>

§

impl<'a> Send for JSONValue<'a>

§

impl<'a> Sync for JSONValue<'a>

§

impl<'a> Unpin for JSONValue<'a>

§

impl<'a> UnwindSafe for JSONValue<'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, 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.