Enum justjson::Value

source ·
pub enum Value<'a> {
    Number(JsonNumber<'a>),
    String(JsonString<'a>),
    Boolean(bool),
    Object(Object<'a>),
    Array(Vec<Value<'a>>),
    Null,
}
Available on crate feature alloc only.
Expand description

A JSON value.

The Backing generic is the storage mechanism used by JsonNumber and JsonString. This is generally &str or Cow<str>.

Variants§

§

Number(JsonNumber<'a>)

A JSON number.

§

String(JsonString<'a>)

A JSON string.

§

Boolean(bool)

A boolean value.

§

Object(Object<'a>)

A JSON object (key/value pairs).

§

Array(Vec<Value<'a>>)

A JSON array (list of values).

§

Null

A null value.

Implementations§

source§

impl<'a> Value<'a>

source

pub fn from_json(json: &'a str) -> Result<Self, Error>

Parses a JSON value from json, returning a Value<&str> that borrows data from json.

Because the str type guarantees that json is valid UTF-8, no additional unicode checks are performed on unescaped unicode sequences.

Examples found in repository?
examples/borrowed-value.rs (line 4)
3
4
5
6
7
8
9
fn main() {
    let value = Value::from_json(r#"{"hello":"world"}"#).expect("error parsing json");
    let obj = value.as_object().expect("json contains an object");
    assert_eq!(obj[0].key, "hello");
    let value = obj[0].value.as_string().expect("value is a string");
    assert_eq!(value, "world");
}
source

pub fn from_json_with_config( json: &'a str, config: ParseConfig ) -> Result<Self, Error>

Parses a JSON value from json using the settings fromconfig, returning a Value<&str> that borrows data from json.

Because the str type guarantees that json is valid UTF-8, no additional unicode checks are performed on unescaped unicode sequences.

source

pub fn from_json_bytes(json: &'a [u8]) -> Result<Self, Error>

Parses a JSON value from json, returning a Value<&str> that borrows data from json.

This function verifies that json is valid UTF-8 while parsing the JSON.

source

pub fn from_json_bytes_with_config( json: &'a [u8], config: ParseConfig ) -> Result<Self, Error>

Parses a JSON value from json using the settings fromconfig, returning a Value<&str> that borrows data from json.

This function verifies that json is valid UTF-8 while parsing the JSON.

source

pub const fn as_object(&self) -> Option<&Object<'a>>

Returns the Object inside of this value, if this is a Value::Object.

Examples found in repository?
examples/borrowed-value.rs (line 5)
3
4
5
6
7
8
9
fn main() {
    let value = Value::from_json(r#"{"hello":"world"}"#).expect("error parsing json");
    let obj = value.as_object().expect("json contains an object");
    assert_eq!(obj[0].key, "hello");
    let value = obj[0].value.as_string().expect("value is a string");
    assert_eq!(value, "world");
}
source

pub fn as_object_mut(&mut self) -> Option<&mut Object<'a>>

Returns a mutable reference to the Object inside of this value, if this is a Value::Object.

source

pub fn get(&self, key: &str) -> Option<&Value<'a>>

Returns the contained value associated with key, if this is a Value::Object. Returns None if the value is not an object or if the key is not found.

Performance

Object uses a Vec of Entry types to store its entries. If the operation being performed can be done with a single iteration over the value’s contents instead of multiple random accesses, the iteration should be preferred. Additional options to make random access faster in environments that can support it are being considered for future releases.

source

pub fn get_mut(&mut self, key: &str) -> Option<&mut Value<'a>>

Returns a mutable reference to the contained value associated with key, if this is a Value::Object. Returns None if the value is not an object or if the key is not found.

Performance

Object uses a Vec of Entry types to store its entries. If the operation being performed can be done with a single iteration over the value’s contents instead of multiple random accesses, the iteration should be preferred. Additional options to make random access faster in environments that can support it are being considered for future releases.

source

pub const fn as_string(&self) -> Option<&JsonString<'a>>

Returns the JsonString inside of this value, if this is a Value::String.

Examples found in repository?
examples/borrowed-value.rs (line 7)
3
4
5
6
7
8
9
fn main() {
    let value = Value::from_json(r#"{"hello":"world"}"#).expect("error parsing json");
    let obj = value.as_object().expect("json contains an object");
    assert_eq!(obj[0].key, "hello");
    let value = obj[0].value.as_string().expect("value is a string");
    assert_eq!(value, "world");
}
source

pub fn as_str(&self) -> Option<&str>

Returns a reference to the contents of this value if it is a Value::String, and it does not have any escape sequences that need to be decoded.

source

pub const fn as_number(&self) -> Option<&JsonNumber<'a>>

Returns the JsonNumber inside of this value, if this is a Value::Number.

source

pub fn as_f32(&self) -> Option<f32>

Parses the contained value as an f32, if it is a number.

The JSON parser only validates that the number takes a correct form. If a number cannot be parsed by the underlying routine due to having too many digits, it this function can return None.

source

pub fn as_f64(&self) -> Option<f64>

Parses the contained value as an f64, if it is a number.

The JSON parser only validates that the number takes a correct form. If a number cannot be parsed by the underlying routine due to having too many digits, it this function can return None.

source

pub const fn as_bool(&self) -> Option<bool>

Returns the bool inside of this value, if this is a Value::Boolean.

source

pub fn as_array(&self) -> Option<&[Self]>

Returns the slice of values inside of this value, if this is a Value::Array.

source

pub fn as_array_mut(&mut self) -> Option<&mut Vec<Self>>

Returns a mutable reference to the Vec of values inside of this value, if this is a Value::Array.

source

pub fn get_index(&self, index: usize) -> Option<&Value<'a>>

Returns the contained value at index, if this is a Value::Array. Returns None if the value is not an array or if index is beyond the bounds of the array.

source

pub fn get_index_mut(&mut self, index: usize) -> Option<&mut Value<'a>>

Returns a mutable reference to the contained value at index, if this is a Value::Array. Returns None if the value is not an array or if index is beyond the bounds of the array.

source

pub const fn is_null(&self) -> bool

Returns true if this value is null/Value::Null.

source

pub fn to_json_pretty(&self) -> String

Converts this value to its JSON representation, with extra whitespace to make it easier for a human to read.

This uses two spaces for indentation, and \n for end of lines. Use to_json_pretty_custom() to customize the formatting behavior.

Panics

This function will panic if there is not enough memory to format the JSON.

source

pub fn to_json_pretty_custom( &self, indentation: &str, line_ending: &str ) -> String

Converts this value to its JSON representation, with extra whitespace to make it easier for a human to read.

Panics

This function will panic if there is not enough memory to format the JSON.

source

pub fn to_json(&self) -> String

Converts this value to its JSON representation, with no extraneous whitespace.

Panics

This function will panic if there is not enough memory to format the JSON.

source

pub fn write_json_to<W: Write>(&self, destination: W) -> Result

Writes this value’s JSON representation to destination, with no extraneous whitespace.

source

pub fn pretty_write_json_to<W: Write>(&self, destination: W) -> Result

Writes this value’s JSON representation to destination, with extra whitespace to make it easier for a human to read.

This uses two spaces for indentation, and \n for end of lines. Use to_json_pretty_custom() to customize the formatting behavior.

source

pub fn pretty_write_json_to_custom<W: Write>( &self, indentation: &str, line_ending: &str, destination: W ) -> Result

Writes this value’s JSON representation to destination, with extra whitespace to make it easier for a human to read.

source§

impl Value<'_>

source

pub fn as_u8(&self) -> Option<u8>

Parses the contained value as an u8 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_u16(&self) -> Option<u16>

Parses the contained value as an u16 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_u32(&self) -> Option<u32>

Parses the contained value as an u32 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_u64(&self) -> Option<u64>

Parses the contained value as an u64 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_u128(&self) -> Option<u128>

Parses the contained value as an u128 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_usize(&self) -> Option<usize>

Parses the contained value as an usize if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_i8(&self) -> Option<i8>

Parses the contained value as an i8 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_i16(&self) -> Option<i16>

Parses the contained value as an i16 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_i32(&self) -> Option<i32>

Parses the contained value as an i32 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_i64(&self) -> Option<i64>

Parses the contained value as an i64 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_i128(&self) -> Option<i128>

Parses the contained value as an i128 if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

source§

impl Value<'_>

source

pub fn as_isize(&self) -> Option<isize>

Parses the contained value as an isize if possible.

If the source number is a floating point number or has a negative sign, this will always return None.

Trait Implementations§

source§

impl<'a> Debug for Value<'a>

source§

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

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

impl<'a> Display for Value<'a>

source§

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

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

impl<'a> From<&'a str> for Value<'a>

source§

fn from(value: &'a str) -> Self

Converts to this type from the input type.
source§

impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>
where Backing: NodeCollection<'a>,

source§

fn from(doc: GenericDocument<'a, Backing>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<JsonString<'a>> for Value<'a>

source§

fn from(value: JsonString<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<Object<'a>> for Value<'a>

source§

fn from(value: Object<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<String> for Value<'a>

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

impl<'a> From<Vec<Value<'a>>> for Value<'a>

source§

fn from(value: Vec<Value<'a>>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<bool> for Value<'a>

source§

fn from(value: bool) -> Self

Converts to this type from the input type.
source§

impl<'b, 'a> Index<&'b str> for Value<'a>

source§

fn index(&self, index: &'b str) -> &Self::Output

Returns the contained value associated with key, if this is a Value::Object.

Panics

This function panics if this value is not a Value::Object or if the key is not found.

Performance

Object uses a Vec of Entry types to store its entries. If the operation being performed can be done with a single iteration over the value’s contents instead of multiple random accesses, the iteration should be preferred. Additional options to make random access faster in environments that can support it are being considered for future releases.

§

type Output = Value<'a>

The returned type after indexing.
source§

impl<'a> Index<usize> for Value<'a>

source§

fn index(&self, index: usize) -> &Self::Output

Returns the contained value at index, if this is a Value::Array.

Panics

This function panics if the value is not a Value::Array or if the index is out of bounds of the array.

§

type Output = Value<'a>

The returned type after indexing.
source§

impl<'b, 'a> IndexMut<&'b str> for Value<'a>

source§

fn index_mut(&mut self, index: &'b str) -> &mut Self::Output

Returns a mutable reference to the contained value associated with key, if this is a Value::Object.

Panics

This function panics if this value is not a Value::Object or if the key is not found.

Performance

Object uses a Vec of Entry types to store its entries. If the operation being performed can be done with a single iteration over the value’s contents instead of multiple random accesses, the iteration should be preferred. Additional options to make random access faster in environments that can support it are being considered for future releases.

source§

impl<'a> IndexMut<usize> for Value<'a>

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Returns a mutable reference to the contained value at index, if this is a Value::Array.

Panics

This function panics if the value is not a Value::Array or if the index is out of bounds of the array.

source§

impl<'a> PartialEq for Value<'a>

source§

fn eq(&self, other: &Value<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for Value<'a>

source§

impl<'a> StructuralEq for Value<'a>

source§

impl<'a> StructuralPartialEq for Value<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> Send for Value<'a>

§

impl<'a> Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'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> 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> ToString for T
where T: Display + ?Sized,

source§

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

§

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

§

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.