[][src]Enum sj::Value

pub enum Value {
    String(String),
    Number(Number),
    Boolean(bool),
    Null,
    Object(Object),
    Array(Array),
}

A value

Formatting

Formatting as JSON string

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:

This example is not tested
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:

    use core::convert::TryFrom;
    
    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_eq!(object.format()?, r#"{"first":true,"third":"..."}"#);
  • Array:

    use core::convert::TryFrom;
    
    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)

Methods

impl Value[src]

pub fn format_as_bytes(&self) -> IoResult<Vec<u8>>[src]

pub fn format_nicely_as_bytes(&self, tab: Option<u8>) -> IoResult<Vec<u8>>[src]

Nicely formats this value as JSON string

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

pub fn format(&self) -> IoResult<String>[src]

pub fn format_nicely(&self, tab: Option<u8>) -> IoResult<String>[src]

Nicely formats this value as JSON string

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

pub fn write<W>(&self, stream: &mut W) -> IoResult<()> where
    W: Write
[src]

Writes this value as compacted JSON string to a stream

Notes

  • The stream is used as-is. For better performance, you should wrap your stream inside a BufWriter.
  • This function does not flush the stream when done.

pub fn write_nicely<W>(&self, tab: Option<u8>, stream: &mut W) -> IoResult<()> where
    W: Write
[src]

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. For better performance, you should wrap your stream inside a BufWriter.
  • This function does not flush the stream when done.

pub fn is_null(&self) -> bool[src]

pub fn try_into_or<T>(self, default: T) -> Result<T> where
    T: TryFrom<Self, Error = Error>, 
[src]

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

pub fn try_into_or_else<T, F>(self, default: F) -> Result<T> where
    T: TryFrom<Self, Error = Error>,
    F: FnOnce() -> T, 
[src]

Tries to convert this value into something

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

See also

try_into_or()

pub fn try_ref_into_or<'a, T>(&'a self, default: T) -> Result<T> where
    T: TryFrom<&'a Self, Error = Error>, 
[src]

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

pub fn try_ref_into_or_else<'a, T, F>(&'a self, default: F) -> Result<T> where
    T: TryFrom<&'a Self, Error = Error>,
    F: FnOnce() -> T, 
[src]

pub fn as_str(&self) -> Result<&str>[src]

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

Returns an error if the value is not a string.

pub fn push<T>(&mut self, value: T) -> Result<()> where
    T: Into<Self>, 
[src]

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

Returns an error if the value is not an array.

pub fn at(&self, indexes: &[usize]) -> Result<&Self>[src]

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

pub fn mut_at(&mut self, indexes: &[usize]) -> Result<&mut Self>[src]

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.

See also

at()

pub fn take_at(&mut self, indexes: &[usize]) -> Result<Self>[src]

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

pub fn as_array(&self) -> Result<&Array>[src]

If the value is an array, returns an immutable reference of it

Returns an error if the value is not an array.

pub fn as_mut_array(&mut self) -> Result<&mut Array>[src]

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

Returns an error if the value is not an array.

pub fn insert<S, T>(&mut self, key: S, value: T) -> Result<Option<Self>> where
    S: Into<String>,
    T: Into<Self>, 
[src]

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.

pub fn by(&self, keys: &[&str]) -> Result<&Self>[src]

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

use core::convert::TryFrom;

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

pub fn maybe_by(&self, keys: &[&str]) -> Result<Option<&Self>>[src]

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.

pub fn mut_by(&mut self, keys: &[&str]) -> Result<&mut Self>[src]

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.

pub fn maybe_mut_by(&mut self, keys: &[&str]) -> Result<Option<&mut Self>>[src]

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.

pub fn take_by(&mut self, keys: &[&str]) -> Result<Self>[src]

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

pub fn maybe_take_by(&mut self, keys: &[&str]) -> Result<Option<Self>>[src]

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.

pub fn as_object(&self) -> Result<&Object>[src]

If the value is an object, returns an immutable reference of it

Returns an error if the value is not an object.

pub fn as_mut_object(&mut self) -> Result<&mut Object>[src]

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

Returns an error if the value is not an object.

Trait Implementations

impl Clone for Value[src]

impl Debug for Value[src]

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

impl From<BTreeMap<String, Value>> for Value[src]

impl From<Number> for Value[src]

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

impl From<String> for Value[src]

impl From<Vec<Value>> for Value[src]

impl From<bool> for Value[src]

impl From<f32> for Value[src]

impl From<f64> for Value[src]

impl From<i128> for Value[src]

impl From<i16> for Value[src]

impl From<i32> for Value[src]

impl From<i64> for Value[src]

impl From<i8> for Value[src]

impl From<isize> for Value[src]

impl From<u128> for Value[src]

impl From<u16> for Value[src]

impl From<u32> for Value[src]

impl From<u64> for Value[src]

impl From<u8> for Value[src]

impl From<usize> for Value[src]

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

impl FromIterator<Value> for Value[src]

impl FromStr for Value[src]

type Err = Error

The associated error which can be returned from parsing.

impl<'_> TryFrom<&'_ Value> for bool[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for i8[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for u64[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for u128[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for usize[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for f32[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for f64[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for i16[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for i32[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for i64[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for i128[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for isize[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for u8[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for u16[src]

type Error = Error

The type returned in the event of a conversion error.

impl<'_> TryFrom<&'_ Value> for u32[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for String[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for bool[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for u8[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for u16[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for u32[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for u64[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for u128[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for usize[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for f32[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for f64[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for Object[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for Array[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for i8[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for i16[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for i32[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for i64[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for i128[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Value> for isize[src]

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<Vec<u8>> for Value[src]

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations

impl RefUnwindSafe for Value

impl Send for Value

impl Sync for Value

impl Unpin for Value

impl UnwindSafe for Value

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

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

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.