Enum sj::Value

source ·
pub enum Value {
    String(String),
    Number(Number),
    Boolean(bool),
    Null,
    Object(Object),
    Array(Array),
}
Expand description

A value

Formatting

Formatting as JSON string

  • To format as compacted JSON string, you can use format() or format_as_bytes().
  • To format as a nice JSON string, you can use format_nicely().
  • Currently the order of keys in input objects will not be preserved. See Object for more details.

Writing as JSON string to Write

Can be done via write() or write_nicely().

Converting Rust types to Value and vice versa

There are some implementations:

impl From<...> for Value;
impl TryFrom<&Value> for ...;
impl TryFrom<Value> for ...;

About TryFrom implementations:

  • For primitives, since they’re cheap, they have implementations on either a borrowed or an owned value.
  • For collections such as String, Object, Array…, they only have implementations on an owned value. So data is moved, not copied.

Shortcuts

A root JSON value can be either an object or an array. For your convenience, there are some shortcuts, like below examples.

  • Object:

    
    let mut object = sj::object();
    object.insert("first", true)?;
    object.insert("second", <Option<u8>>::None)?;
    object.insert(String::from("third"), "...")?;
    
    assert!(bool::try_from(object.by("first")?)?);
    assert!(object.take_by("second")?.try_into_or(true)?);
    assert!(
        [r#"{"first":true,"third":"..."}"#, r#"{"third":"...","first":true}"#]
            .contains(&object.format()?.as_str())
    );
  • Array:

    
    let mut array = sj::array();
    array.push(false)?;
    array.push("a string")?;
    array.push(Some(sj::object()))?;
    
    assert!(bool::try_from(array.at(0)?)? == false);
    assert_eq!(array.format()?, r#"[false,"a string",{}]"#);

Variants

String(String)

Number(Number)

Boolean(bool)

Null

Object(Object)

Array(Array)

Implementations

If the value is an array, pushes new item into it

Returns an error if the value is not an array.

Gets an immutable item from this array and its sub arrays

The function returns an error on one of these conditions:

  • Indexes are empty or invalid.
  • The value or any of its sub items is not an array.
Examples
let mut array = sj::array();
array.push("first")?;
array.push(vec![false.into(), "second".into()])?;

assert_eq!(array.at(0)?.as_str()?, "first");
assert_eq!(array.at([1, 1])?.as_str()?, "second");

assert!(array.at(&[][..]).is_err());
assert!(array.at([0, 1]).is_err());
assert!(array.at([1, 2]).is_err());
assert!(array.at([1, 0, 0]).is_err());
assert!(array.at([1, 1, 2]).is_err());
Gets a mutable item from this array and its sub arrays

The function returns an error on one of these conditions:

  • Indexes are empty or invalid.
  • The value or any of its sub items is not an array.
Takes an item from this array and its sub arrays

The function returns an error on one of these conditions:

  • Indexes are empty or invalid.
  • The value or any of its sub items is not an array.
Examples
let mut array = sj::array();
array.push("earth")?;
array.push(vec![false.into(), "moon".into()])?;

assert_eq!(array.take_at(0)?.as_str()?, "earth");
assert_eq!(array.take_at([0, 1])?.as_str()?, "moon");

assert!(array.take_at(&[][..]).is_err());
assert!(array.take_at([0, 1]).is_err());
If the value is an array, returns an immutable reference of it

Returns an error if the value is not an array.

If the value is an array, returns a mutable reference of it

Returns an error if the value is not an array.

Tries to convert this value into something

If this is Null, returns default.

If your default value would be a result of a function call, you should use try_into_or_else().

Examples
let mut value = sj::parse_bytes("[null]")?;
assert!(value.take_at(0)?.try_into_or(true)?);
Tries to convert this value into something

If this is Null, calls default() and returns its result.

Tries to convert a reference of this value into something

If this is Null, returns default.

If your default value would be a result of a function call, you should use try_ref_into_or_else().

Examples
let value = sj::parse_bytes("[null]")?;
assert_eq!(value.at(0)?.try_ref_into_or(0)?, 0);
Tries to convert a reference of this value into something

If this is Null, calls default() and returns its result.

If the value is an object, inserts new item into it

On success, returns previous value (if it existed).

Returns an error if the value is not an object.

Gets an immutable item from this object and its sub objects

The function returns an error on one of these conditions:

  • Keys are empty.
  • The value or any of its sub items is not an object.
Examples
let mut object = sj::object();
object.insert("first", true)?;
object.insert("second", {
    let mut map = sj::Object::new();
    sj::insert(&mut map, "zero", 0);
    map
})?;

assert_eq!(bool::try_from(object.by("first")?)?, true);
assert_eq!(u8::try_from(object.by(["second", "zero"])?)?, 0);

assert!(object.by("something").is_err());
assert!(object.maybe_by("something")?.is_none());

assert!(object.by(&[][..]).is_err());
assert!(object.by(["first", "last"]).is_err());
assert!(object.by(["second", "third", "fourth"]).is_err());
Gets an optional immutable item from this object and its sub objects

The function returns an error on one of these conditions:

  • Keys are empty.
  • The value or any of its sub items is not an object.
Gets a mutable item from this object and its sub objects

The function returns an error on one of these conditions:

  • Keys are empty.
  • The value or any of its sub items is not an object.
Gets an optional mutable item from this object and its sub objects

The function returns an error on one of these conditions:

  • Keys are empty.
  • The value or any of its sub items is not an object.
Takes an item from this object and its sub objects

The function returns an error on one of these conditions:

  • Keys are empty.
  • The value or any of its sub items is not an object.
Examples
let mut object = sj::object();
object.insert("earth", "moon")?;
object.insert("solar-system", {
    let mut map = sj::Object::new();
    sj::insert(&mut map, "sun", "earth");
    map
})?;

assert_eq!(object.take_by("earth")?.as_str()?, "moon");
assert_eq!(object.take_by(["solar-system", "sun"])?.as_str()?, "earth");

assert!(object.take_by("milky-way").is_err());
assert!(object.maybe_take_by("milky-way")?.is_none());
assert!(object.maybe_take_by(["solar-system", "mars"])?.is_none());

assert!(object.take_by(&[][..]).is_err());
assert!(object.take_by(["jupiter", "venus"]).is_err());
Takes an optional item from this object and its sub objects

The function returns an error on one of these conditions:

  • Keys are empty.
  • The value or any of its sub items is not an object.
If the value is an object, returns an immutable reference of it

Returns an error if the value is not an object.

If the value is an object, returns a mutable reference of it

Returns an error if the value is not an object.

If the value is a string, returns an immutable reference of it

Returns an error if the value is not a string.

If the value is a string, returns a mutable reference of it

Returns an error if the value is not a string.

Nicely formats this value as JSON string

If you don’t provide tab size, default (4) will be used.

Nicely formats this value as JSON string

If you don’t provide tab size, default (4) will be used.

Writes this value as compacted JSON string to a stream
Notes
  • The stream is used as-is. You might want to consider using BufWriter.
  • This function does not flush the stream when done.
Writes this value as nicely formatted JSON string to a stream
Notes
  • If you don’t provide tab size, default (4) will be used.
  • The stream is used as-is. You might want to consider using BufWriter.
  • This function does not flush the stream when done.

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
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. 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.