pub enum Value<'a> {
Number(JsonNumber<'a>),
String(JsonString<'a>),
Boolean(bool),
Object(Object<'a>),
Array(Vec<Value<'a>>),
Null,
}
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>
impl<'a> Value<'a>
sourcepub fn from_json(json: &'a str) -> Result<Self, Error>
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.
sourcepub fn from_json_with_config(
json: &'a str,
config: ParseConfig
) -> Result<Self, Error>
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.
sourcepub fn from_json_bytes(json: &'a [u8]) -> Result<Self, Error>
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.
sourcepub fn from_json_bytes_with_config(
json: &'a [u8],
config: ParseConfig
) -> Result<Self, Error>
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.
sourcepub const fn as_object(&self) -> Option<&Object<'a>>
pub const fn as_object(&self) -> Option<&Object<'a>>
Returns the Object
inside of this value, if this is a
Value::Object
.
sourcepub fn as_object_mut(&mut self) -> Option<&mut Object<'a>>
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
.
sourcepub fn get(&self, key: &str) -> Option<&Value<'a>>
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.
sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut Value<'a>>
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.
sourcepub const fn as_string(&self) -> Option<&JsonString<'a>>
pub const fn as_string(&self) -> Option<&JsonString<'a>>
Returns the JsonString
inside of this value, if this is a
Value::String
.
sourcepub fn as_str(&self) -> Option<&str>
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.
sourcepub const fn as_number(&self) -> Option<&JsonNumber<'a>>
pub const fn as_number(&self) -> Option<&JsonNumber<'a>>
Returns the JsonNumber
inside of this value, if this is a
Value::Number
.
sourcepub fn as_f32(&self) -> Option<f32>
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.
sourcepub fn as_f64(&self) -> Option<f64>
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.
sourcepub const fn as_bool(&self) -> Option<bool>
pub const fn as_bool(&self) -> Option<bool>
Returns the bool
inside of this value, if this is a
Value::Boolean
.
sourcepub fn as_array(&self) -> Option<&[Self]>
pub fn as_array(&self) -> Option<&[Self]>
Returns the slice of values inside of this value, if this is a
Value::Array
.
sourcepub fn as_array_mut(&mut self) -> Option<&mut Vec<Self>>
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
.
sourcepub fn get_index(&self, index: usize) -> Option<&Value<'a>>
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.
sourcepub fn get_index_mut(&mut self, index: usize) -> Option<&mut Value<'a>>
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.
sourcepub const fn is_null(&self) -> bool
pub const fn is_null(&self) -> bool
Returns true if this value is null
/Value::Null
.
sourcepub fn to_json_pretty(&self) -> String
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.
sourcepub fn to_json_pretty_custom(
&self,
indentation: &str,
line_ending: &str
) -> String
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.
sourcepub fn to_json(&self) -> String
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.
sourcepub fn write_json_to<W: Write>(&self, destination: W) -> Result
pub fn write_json_to<W: Write>(&self, destination: W) -> Result
Writes this value’s JSON representation to destination
, with no extraneous
whitespace.
sourcepub fn pretty_write_json_to<W: Write>(&self, destination: W) -> Result
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.
Trait Implementations§
source§impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>where
Backing: NodeCollection<'a>,
impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>where
Backing: NodeCollection<'a>,
source§fn from(doc: GenericDocument<'a, Backing>) -> Self
fn from(doc: GenericDocument<'a, Backing>) -> Self
source§impl<'a> From<JsonString<'a>> for Value<'a>
impl<'a> From<JsonString<'a>> for Value<'a>
source§fn from(value: JsonString<'a>) -> Self
fn from(value: JsonString<'a>) -> Self
source§impl<'b, 'a> Index<&'b str> for Value<'a>
impl<'b, 'a> Index<&'b str> for Value<'a>
source§fn index(&self, index: &'b str) -> &Self::Output
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.
source§impl<'b, 'a> IndexMut<&'b str> for Value<'a>
impl<'b, 'a> IndexMut<&'b str> for Value<'a>
source§fn index_mut(&mut self, index: &'b str) -> &mut Self::Output
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>
impl<'a> IndexMut<usize> for Value<'a>
source§fn index_mut(&mut self, index: usize) -> &mut Self::Output
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.