Enum json::JsonValue[][src]

pub enum JsonValue {
    Null,
    Short(Short),
    String(String),
    Number(Number),
    Boolean(bool),
    Object(Object),
    Array(Vec<JsonValue>),
}

Variants

Null
Short(Short)

Tuple Fields of Short

0: Short
String(String)

Tuple Fields of String

0: String
Number(Number)

Tuple Fields of Number

0: Number
Boolean(bool)

Tuple Fields of Boolean

0: bool
Object(Object)

Tuple Fields of Object

0: Object
Array(Vec<JsonValue>)

Tuple Fields of Array

0: Vec<JsonValue>

Implementations

Create an empty JsonValue::Object instance. When creating an object with data, consider using the object! macro.

Create an empty JsonValue::Array instance. When creating array with data, consider using the array! macro.

Prints out the value as JSON string.

Pretty prints out the value as JSON string. Takes an argument that’s number of spaces to indent new blocks with.

👎 Deprecated since 0.10.2:

use JsonValue::write instead

Writes 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.

Writes the JSON as byte stream into an implementor of std::io::Write.

Writes the JSON as byte stream into an implementor of std::io::Write.

Checks whether the value is empty. Returns true for:

  • empty string ("")
  • number 0
  • boolean false
  • null
  • empty array (array![])
  • empty object (object!{})

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));

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));

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());

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());

Works on JsonValue::Array - pushes a new value to the array.

Works on JsonValue::Array - remove and return last element from an array. On failure returns a null.

Works on JsonValue::Array - checks if the array contains a value

Works on JsonValue::Object - checks if the object has a key

Returns length of array or object (number of keys), defaults to 0 for other types.

Works on JsonValue::Array - returns an iterator over members. Will return an empty iterator if called on non-array types.

Works on JsonValue::Array - returns a mutable iterator over members. Will return an empty iterator if called on non-array types.

Works on JsonValue::Object - returns an iterator over key value pairs. Will return an empty iterator if called on non-object types.

Works on JsonValue::Object - returns a mutable iterator over key value pairs. Will return an empty iterator if called on non-object types.

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.

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.

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.

When called on an array or an object, will wipe them clean. When called on a string will clear the string. Numbers and booleans become null.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Implements formatting

let data = json::parse(r#"{"url":"https://github.com/"}"#).unwrap();
println!("{}", data);
println!("{:#}", data);

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Implements indexing by &str to easily access object members:

Example

let object = object!{
    "foo" => "bar"
};

assert!(object["foo"] == "bar");

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Implements indexing by usize to easily access array members:

Example

let mut array = JsonValue::new_array();

array.push("foo");

assert!(array[0] == "foo");

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

Implements mutable indexing by &str to easily modify object members:

Example

let mut object = object!{};

object["foo"] = 42.into();

assert!(object["foo"] == 42);

Performs the mutable indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

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");

Performs the mutable indexing (container[index]) operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.