pub enum JsonValue {
Null,
Short(Short),
String(String),
Number(Number),
Boolean(bool),
Object(Object),
Array(Vec<JsonValue>),
}
Variants§
Implementations§
Source§impl JsonValue
impl JsonValue
Sourcepub fn new_object() -> JsonValue
pub fn new_object() -> JsonValue
Create an empty JsonValue::Object
instance.
When creating an object with data, consider using the object!
macro.
Sourcepub fn new_array() -> JsonValue
pub fn new_array() -> JsonValue
Create an empty JsonValue::Array
instance.
When creating array with data, consider using the array!
macro.
Sourcepub fn pretty(&self, spaces: u16) -> String
pub fn pretty(&self, spaces: u16) -> String
Pretty prints out the value as JSON string. Takes an argument that’s number of spaces to indent new blocks with.
Sourcepub fn to_writer<W: Write>(&self, writer: &mut W)
👎Deprecated since 0.10.2: use JsonValue::write
instead
pub fn to_writer<W: Write>(&self, writer: &mut W)
JsonValue::write
insteadWrites the JSON as byte stream into an implementor of std::io::Write
.
This method is deprecated as it will panic on io errors, use write
instead.
Sourcepub fn write<W: Write>(&self, writer: &mut W) -> Result<()>
pub fn write<W: Write>(&self, writer: &mut W) -> Result<()>
Writes the JSON as byte stream into an implementor of std::io::Write
.
Sourcepub fn write_pretty<W: Write>(&self, writer: &mut W, spaces: u16) -> Result<()>
pub fn write_pretty<W: Write>(&self, writer: &mut W, spaces: u16) -> Result<()>
Writes the JSON as byte stream into an implementor of std::io::Write
.
pub fn is_string(&self) -> bool
pub fn is_number(&self) -> bool
pub fn is_boolean(&self) -> bool
pub fn is_null(&self) -> bool
pub fn is_object(&self) -> bool
pub fn is_array(&self) -> bool
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks whether the value is empty. Returns true for:
- empty string (
""
) - number
0
- boolean
false
- null
- empty array (
array![]
) - empty object (
object!{}
)
pub fn as_str(&self) -> Option<&str>
pub fn as_number(&self) -> Option<Number>
pub fn as_f64(&self) -> Option<f64>
pub fn as_f32(&self) -> Option<f32>
pub fn as_u64(&self) -> Option<u64>
pub fn as_u32(&self) -> Option<u32>
pub fn as_u16(&self) -> Option<u16>
pub fn as_u8(&self) -> Option<u8>
pub fn as_usize(&self) -> Option<usize>
pub fn as_i64(&self) -> Option<i64>
pub fn as_i32(&self) -> Option<i32>
pub fn as_i16(&self) -> Option<i16>
pub fn as_i8(&self) -> Option<i8>
pub fn as_isize(&self) -> Option<isize>
pub fn as_bool(&self) -> Option<bool>
pub fn as_object(&self) -> Option<&Object>
pub fn as_object_mut(&mut self) -> Option<&mut Object>
pub fn as_array(&self) -> Option<&Vec<JsonValue>>
pub fn as_array_mut(&mut self) -> Option<&mut Vec<JsonValue>>
Sourcepub fn get(&self, key: &str) -> Option<&JsonValue>
pub fn get(&self, key: &str) -> Option<&JsonValue>
If self
is a JsonValue::Object
, then this looks up an entry by its key
in the object.
If self
is not a JsonValue::Object
, then this method returns None
.
Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut JsonValue>
pub fn get_mut(&mut self, key: &str) -> Option<&mut JsonValue>
If self
is a JsonValue::Object
, then this looks up an entry by its key
in the object.
If self
is not a JsonValue::Object
, then this method returns None
.
Sourcepub fn as_fixed_point_u64(&self, point: u16) -> Option<u64>
pub fn as_fixed_point_u64(&self, point: u16) -> Option<u64>
Obtain an integer at a fixed decimal point. This is useful for converting monetary values and doing arithmetic on them without rounding errors introduced by floating point operations.
Will return None
if Number
called on a value that’s not a number,
or if the number is negative or a NaN.
let price_a = JsonValue::from(5.99);
let price_b = JsonValue::from(7);
let price_c = JsonValue::from(10.2);
assert_eq!(price_a.as_fixed_point_u64(2), Some(599));
assert_eq!(price_b.as_fixed_point_u64(2), Some(700));
assert_eq!(price_c.as_fixed_point_u64(2), Some(1020));
Sourcepub fn as_fixed_point_i64(&self, point: u16) -> Option<i64>
pub fn as_fixed_point_i64(&self, point: u16) -> Option<i64>
Analog to as_fixed_point_u64
, except returning a signed
i64
, properly handling negative numbers.
let balance_a = JsonValue::from(-1.49);
let balance_b = JsonValue::from(42);
assert_eq!(balance_a.as_fixed_point_i64(2), Some(-149));
assert_eq!(balance_b.as_fixed_point_i64(2), Some(4200));
Sourcepub fn take(&mut self) -> JsonValue
pub fn take(&mut self) -> JsonValue
Take over the ownership of the value, leaving Null
in it’s place.
§Example
let mut data = array!["Foo", 42];
let first = data[0].take();
let second = data[1].take();
assert!(first == "Foo");
assert!(second == 42);
assert!(data[0].is_null());
assert!(data[1].is_null());
Sourcepub fn take_string(&mut self) -> Option<String>
pub fn take_string(&mut self) -> Option<String>
Checks that self is a string, returns an owned Rust String
, leaving
Null
in it’s place.
-
If the contained string is already a heap allocated
String
, then the ownership is moved without any heap allocation. -
If the contained string is a
Short
, this will perform a heap allocation to convert the types for you.
§Example
let mut data = array!["Hello", "World"];
let owned = data[0].take_string().expect("Should be a string");
assert_eq!(owned, "Hello");
assert!(data[0].is_null());
Sourcepub fn push<T>(&mut self, value: T) -> Result<()>
pub fn push<T>(&mut self, value: T) -> Result<()>
Works on JsonValue::Array
- pushes a new value to the array.
Sourcepub fn pop(&mut self) -> JsonValue
pub fn pop(&mut self) -> JsonValue
Works on JsonValue::Array
- remove and return last element from
an array. On failure returns a null.
Sourcepub fn contains<T>(&self, item: T) -> bool
pub fn contains<T>(&self, item: T) -> bool
Works on JsonValue::Array
- checks if the array contains a value
Sourcepub fn has_key(&self, key: &str) -> bool
pub fn has_key(&self, key: &str) -> bool
Works on JsonValue::Object
- checks if the object has a key
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns length of array or object (number of keys), defaults to 0
for
other types.
Sourcepub fn members(&self) -> Members<'_>
pub fn members(&self) -> Members<'_>
Works on JsonValue::Array
- returns an iterator over members.
Will return an empty iterator if called on non-array types.
§Example
let animals = array!["Cat", "Dog", "Snail"];
let mut animals_with_letter_a = Vec::new();
for animal in animals.members() {
if animal.as_str().unwrap().contains('a') {
animals_with_letter_a.push(animal);
}
}
assert_eq!(animals_with_letter_a, vec!["Cat", "Snail"]);
Sourcepub fn members_mut(&mut self) -> MembersMut<'_>
pub fn members_mut(&mut self) -> MembersMut<'_>
Works on JsonValue::Array
- returns a mutable iterator over members.
Will return an empty iterator if called on non-array types.
Sourcepub fn entries(&self) -> Entries<'_>
pub fn entries(&self) -> Entries<'_>
Works on JsonValue::Object
- returns an iterator over key value pairs.
Will return an empty iterator if called on non-object types.
§Example
let heights = jzon::parse(r#"
{
"Alice": 1.42,
"Bob": 1.6,
"Carlos": 2.1
}
"#).unwrap();
let mut total_height = 0.0;
let mut names_with_o: Vec<&str> = Vec::new();
for (name, height) in heights.entries() {
total_height += height.as_f64().unwrap();
if name.contains('o') {
names_with_o.push(name);
}
}
assert_eq!(total_height, 5.12);
assert_eq!(names_with_o, vec!["Bob", "Carlos"]);
Sourcepub fn entries_mut(&mut self) -> EntriesMut<'_>
pub fn entries_mut(&mut self) -> EntriesMut<'_>
Works on JsonValue::Object
- returns a mutable iterator over
key value pairs.
Will return an empty iterator if called on non-object types.
Sourcepub fn insert<T>(&mut self, key: &str, value: T) -> Result<()>
pub fn insert<T>(&mut self, key: &str, value: T) -> Result<()>
Works on JsonValue::Object
- inserts a new entry, or override an existing
one into the object. Note that key
has to be a &str
slice and not an owned
String
. The internals of Object
will handle the heap allocation of the key
if needed for better performance.
Sourcepub fn remove(&mut self, key: &str) -> JsonValue
pub fn remove(&mut self, key: &str) -> JsonValue
Works on JsonValue::Object
- remove a key and return the value it held.
If the key was not present, the method is called on anything but an
object, it will return a null.
Sourcepub fn array_remove(&mut self, index: usize) -> JsonValue
pub fn array_remove(&mut self, index: usize) -> JsonValue
Works on JsonValue::Array
- remove an entry and return the value it held.
If the method is called on anything but an object or if the index is out of bounds, it
will return JsonValue::Null
.
Trait Implementations§
Source§impl Display for JsonValue
Implements formatting
impl Display for JsonValue
Implements formatting
let data = jzon::parse(r#"{"url":"https://github.com/"}"#).unwrap();
println!("{}", data);
println!("{:#}", data);
Source§impl<'a> Index<&'a str> for JsonValue
Implements indexing by &str
to easily access object members:
impl<'a> Index<&'a str> for JsonValue
Implements indexing by &str
to easily access object members:
§Example
let object = object!{
foo: "bar"
};
assert!(object["foo"] == "bar");
Source§impl Index<usize> for JsonValue
Implements indexing by usize
to easily access array members:
impl Index<usize> for JsonValue
Implements indexing by usize
to easily access array members:
§Example
let mut array = JsonValue::new_array();
array.push("foo");
assert!(array[0] == "foo");
Source§impl<'a> IndexMut<&'a str> for JsonValue
Implements mutable indexing by &str
to easily modify object members:
impl<'a> IndexMut<&'a str> for JsonValue
Implements mutable indexing by &str
to easily modify object members:
§Example
let mut object = object!{};
object["foo"] = 42.into();
assert!(object["foo"] == 42);
Source§impl IndexMut<usize> for JsonValue
Implements mutable indexing by usize
to easily modify array members:
impl IndexMut<usize> for JsonValue
Implements mutable indexing by usize
to easily modify array members:
§Example
let mut array = array!["foo", 3.14];
array[1] = "bar".into();
assert!(array[1] == "bar");