[][src]Enum sj::Value

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

A value

Usage

Formatting as JSON string

  • To format as compacted JSON string, you can use to_string() or implementation of From<Value> for Vec<u8>, or into_bytes().

  • To format with default tab width (4), you can use # (via Formatter):

    format!(
        "{:#}",
        sj::parse(&mut &br#"["test"]"#[..]).unwrap(),
    );
  • You can set tab width (required) and tab level (optional):

    format!(
        "{:width$.level$}",
        sj::parse(&mut &br#"["test"]"#[..]).unwrap(),
        width=4, level=0,
    );

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, Vec..., 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.

let mut object = sj::object();
object.insert("first", true)?;
object.insert("second", Some(9))?;
object.insert(String::from("third"), "...")?;
assert!(object.as_mut_object()?.remove("second").is_some());
assert_eq!(object.to_string(), r#"{"first":true,"third":"..."}"#);

let mut array = sj::array();
array.push(false)?;
array.push("a string")?;
array.push(Some(sj::object()))?;
assert_eq!(array.to_string(), r#"[false,"a string",{}]"#);

Variants

String(String)
Number(Number)
Boolean(bool)
Null
Object(Object)
Array(Array)

Methods

impl Value[src]

pub fn into_bytes(self) -> Vec<u8>[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 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 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.

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<usize>,
    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.

Trait Implementations

impl From<String> for Value[src]

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

impl From<Number> for Value[src]

impl From<bool> for Value[src]

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

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

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

impl From<i8> for Value[src]

impl From<i16> for Value[src]

impl From<i32> for Value[src]

impl From<i64> for Value[src]

impl From<i128> for Value[src]

impl From<isize> for Value[src]

impl From<u8> for Value[src]

impl From<u16> for Value[src]

impl From<u32> for Value[src]

impl From<u64> for Value[src]

impl From<u128> for Value[src]

impl From<usize> for Value[src]

impl From<f32> for Value[src]

impl From<f64> for Value[src]

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

impl Debug for Value[src]

impl Display for Value[src]

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 bool[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 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 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 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 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 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 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 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 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 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 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 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 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 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 f64[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.

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

impl FromIterator<Value> for Value[src]

Auto Trait Implementations

impl Unpin for Value

impl Send for Value

impl Sync for Value

impl UnwindSafe for Value

impl RefUnwindSafe for Value

Blanket Implementations

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

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> Into<U> for T where
    U: From<T>, 
[src]

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.

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

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

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

impl<T> ToString for T where
    T: Display + ?Sized
[src]