pub struct JSONValue<'a> {
pub value_type: JSONValueType,
/* private fields */
}Fields§
§value_type: JSONValueTypeImplementations§
Source§impl<'a> JSONValue<'a>
impl<'a> JSONValue<'a>
Sourcepub fn load(contents: &'a str) -> JSONValue<'_>
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.
Sourcepub fn verify(&self) -> Result<(), JSONParsingError>
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());Sourcepub fn load_and_verify(
contents: &'a str,
) -> Result<JSONValue<'_>, JSONParsingError>
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.
Sourcepub fn read_boolean(&self) -> Result<bool, JSONParsingError>
pub fn read_boolean(&self) -> Result<bool, JSONParsingError>
Sourcepub fn read_integer(&self) -> Result<isize, JSONParsingError>
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));Sourcepub fn read_float(&self) -> Result<f32, JSONParsingError>
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));Sourcepub fn read_string(&self) -> Result<&'a str, JSONParsingError>
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"));Sourcepub fn iter_array(&self) -> Result<JSONArrayIterator<'a>, JSONParsingError>
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.
Sourcepub fn iter_string(&self) -> Result<EscapedStringIterator<'a>, JSONParsingError>
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 ⟻")Sourcepub fn iter_object(&self) -> Result<JSONObjectIterator<'a>, JSONParsingError>
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.
Sourcepub fn get_key_value(
&self,
key: &str,
) -> Result<JSONValue<'_>, JSONParsingError>
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.