Enum json_api::Value [] [src]

pub enum Value {
    Null,
    Array(Vec<Value>),
    Bool(bool),
    Number(Number),
    Object(Map),
    String(String),
}

Represents any valid JSON API value.

Like serde_json::Value, but with spec compliance baked into the type system.

Variants

A null value.

An array of values.

A boolean value.

An integer or floating point value.

A JSON object as a hash table with consistent order. Keys are guarenteed to be a valid member name.

A string value.

Methods

impl Value
[src]

[src]

Optionally get the underlying vector as a slice. Returns None if the Value is not an array.

Example

let data = vec![true.into(), false.into()];
let array = Value::Array(data.clone());
let boolean = Value::Bool(true);

assert_eq!(array.as_array(), Some(data.as_slice()));
assert_eq!(boolean.as_array(), None);

[src]

Optionally get the underlying vector as a mutable slice. Returns None if the Value is not an array.

Example

let mut data = vec![true.into(), false.into()];
let mut array = Value::Array(data.clone());
let mut boolean = Value::Bool(true);

assert_eq!(array.as_array_mut(), Some(data.as_mut_slice()));
assert_eq!(boolean.as_array_mut(), None);

[src]

Optionally get the inner boolean value. Returns None if the Value is not a boolean.

Example

let boolean = Value::Bool(true);
let number = Value::from(3.14);

assert_eq!(boolean.as_bool(), Some(true));
assert_eq!(number.as_bool(), None);

[src]

Returns Some(()) if the Value is null.

Example

let null = Value::Null;
let text = Value::String("Hello, World!".to_owned());

assert_eq!(null.as_null(), Some(()));
assert_eq!(text.as_null(), None);

[src]

Optionally get a reference to the inner map. Returns None if the Value is not an object.

Example

let data = Map::new();
let object = Value::Object(data.clone());
let number = Value::from(3.14);

assert_eq!(object.as_object(), Some(&data));
assert_eq!(number.as_object(), None);

[src]

Optionally get a mutable reference to the inner map. Returns None if the Value is not an object.

Example

let mut data = Map::new();
let mut object = Value::Object(data.clone());
let mut number = Value::from(3.14);

assert_eq!(object.as_object_mut(), Some(&mut data));
assert_eq!(number.as_object_mut(), None);

[src]

Optionally get the underlying string as a string slice. Returns None if the Value is not a string.

Example

let data = "Hello, World!";
let string = Value::String(data.to_owned());
let number = Value::from(3.14);

assert_eq!(string.as_str(), Some(data));
assert_eq!(number.as_str(), None);

[src]

Optionally get the underlying number as an f64. Returns None if the Value cannot be represented as an f64.

Example

let number = Value::from(3.14);
let string = Value::String("Hello, World!".to_owned());

assert_eq!(number.as_f64(), Some(3.14));
assert_eq!(string.as_f64(), None);

[src]

Optionally get the underlying number as an i64. Returns None if the Value cannot be represented as an i64.

Example

let integer = Value::from(10);
let float = Value::from(3.14);

assert_eq!(integer.as_i64(), Some(10));
assert_eq!(float.as_i64(), None);

[src]

Optionally get the underlying number as an u64. Returns None if the Value cannot be represented as an u64.

Example

let positive = Value::from(10);
let negative = Value::from(-10);

assert_eq!(positive.as_u64(), Some(10));
assert_eq!(negative.as_u64(), None);

[src]

Returns true if the Value is an array.

For any Value on which is_array returns true, as_array and as_array_mut are guaranteed to return a reference to the vector representing the array.

Example

let mut value = Value::from(vec![1, 2, 3]);

assert!(value.is_array());

value.as_array().unwrap();
value.as_array_mut().unwrap();

[src]

Returns true if the Value is a boolean.

For any Value on which is_boolean returns true, as_bool is guaranteed to return the boolean value.

Example

let value = Value::Bool(true);

assert!(value.is_boolean());
value.as_bool().unwrap();

[src]

Returns true if the Value is null.

For any Value on which is_null returns true, as_null is guaranteed to return Some(()).

Example

let value = Value::Null;

assert!(value.is_null());
value.as_null().unwrap();

[src]

Returns true if the Value is a number.

Example

assert!(Value::from(3.14).is_number());

[src]

Returns true if the Value is an object.

For any Value on which is_array returns true, as_object and as_object_mut are guaranteed to return a reference to the map representing the object.

Example

let mut value = Value::Object(Default::default());

assert!(value.is_object());

value.as_object().unwrap();
value.as_object_mut().unwrap();

[src]

Returns true if the Value is a string.

For any Value on which is_string returns true, as_str is guaranteed to return the string slice.

Example

let value = Value::String("Hello, world!".to_owned());

assert!(value.is_string());
value.as_str().unwrap();

[src]

Returns true if the Value is a number that can be represented as an f64.

For any Value on which is_f64 returns true, as_f64 is guaranteed to return the floating point value.

Currently this function returns true if and only if both is_i64 and is_u64 return false. This behavior is not a guarantee in the future.

Example

let value = Value::from(3.14);

assert!(value.is_f64());
value.as_f64().unwrap();

[src]

Returns true if the Value is an integer between i64::MIN and i64::MAX.

For any Value on which is_i64 returns true, as_i64 is guaranteed to return the integer value.

Example

let pos = Value::from(3);
let neg = Value::from(-3);

assert!(pos.is_i64());
assert!(neg.is_i64());

pos.as_i64().unwrap();
neg.as_i64().unwrap();

[src]

Returns true if the Value is an integer between 0 and u64::MAX.

For any Value on which is_u64 returns true, as_u64 is guaranteed to return the integer value.

Example

let value = Value::from(3);

assert!(value.is_u64());
value.as_u64().unwrap();

Trait Implementations

impl Clone for Value
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Debug for Value
[src]

[src]

Formats the value using the given formatter.

impl PartialEq for Value
[src]

[src]

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

[src]

This method tests for !=.

impl Default for Value
[src]

Returns the Value::Null. This allows for better composition with Option types.

Example

const MSG: &'static str = "Hello, World!";

let opt = None;
let value = opt.map(Value::String).unwrap_or_default();
assert_eq!(value, Value::Null);

let opt = Some(MSG.to_owned());
let value = opt.map(Value::String).unwrap_or_default();
assert_eq!(value, Value::String(MSG.to_owned()));

[src]

Returns the "default value" for a type. Read more

impl From<bool> for Value
[src]

[src]

Performs the conversion.

impl From<f32> for Value
[src]

[src]

Performs the conversion.

impl From<f64> for Value
[src]

[src]

Performs the conversion.

impl From<i8> for Value
[src]

[src]

Performs the conversion.

impl From<i16> for Value
[src]

[src]

Performs the conversion.

impl From<i32> for Value
[src]

[src]

Performs the conversion.

impl From<i64> for Value
[src]

[src]

Performs the conversion.

impl From<u8> for Value
[src]

[src]

Performs the conversion.

impl From<u16> for Value
[src]

[src]

Performs the conversion.

impl From<u32> for Value
[src]

[src]

Performs the conversion.

impl From<u64> for Value
[src]

[src]

Performs the conversion.

impl From<String> for Value
[src]

[src]

Performs the conversion.

impl From<Map> for Value
[src]

[src]

Performs the conversion.

impl<T> From<Option<T>> for Value where
    T: Into<Value>, 
[src]

[src]

Performs the conversion.

impl<T> From<Vec<T>> for Value where
    T: Into<Value>, 
[src]

[src]

Performs the conversion.

impl<'a> From<&'a str> for Value
[src]

[src]

Performs the conversion.

impl<'a, T> From<&'a [T]> for Value where
    T: Clone + Into<Value>, 
[src]

[src]

Performs the conversion.

impl<T> FromIterator<T> for Value where
    T: Into<Value>, 
[src]

[src]

Creates a value from an iterator. Read more

impl FromIterator<(Key, Value)> for Value
[src]

[src]

Creates a value from an iterator. Read more

impl FromStr for Value
[src]

The associated error which can be returned from parsing.

[src]

Parses a string s to return a value of this type. Read more

impl PartialEq<bool> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<f32> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<f64> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<i8> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<i16> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<i32> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<i64> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<isize> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<u8> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<u16> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<u32> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<u64> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<usize> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl PartialEq<str> for Value
[src]

[src]

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

1.0.0
[src]

This method tests for !=.

impl<'de> Deserialize<'de> for Value
[src]

[src]

Deserialize this value from the given Serde deserializer. Read more

impl Serialize for Value
[src]

[src]

Serialize this value into the given Serde serializer. Read more