pub enum Json {
String(String),
Number(Number),
Boolean(bool),
Null,
Object(Object),
Array(Array),
}
Expand description
§Json
§Formatting
§Formatting as JSON string
- To format as compacted JSON string, you can use
format()
orformat_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 Json
and vice versa
There are some implementations:
impl From<...> for Json;
impl TryFrom<&Json> for ...;
impl TryFrom<Json> 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")?.map_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§
Implementations§
source§impl Json
impl Json
sourcepub fn push<T>(&mut self, value: T) -> Result<()>where
T: Into<Self>,
pub fn push<T>(&mut self, value: T) -> Result<()>where
T: Into<Self>,
§If the value is an array, pushes new item into it
Returns an error if the value is not an array.
sourcepub fn at<'a, I, const N: usize>(&self, indexes: I) -> Result<&Self>where
I: Into<ArrayIndexes<'a, N>>,
pub fn at<'a, I, const N: usize>(&self, indexes: I) -> Result<&Self>where
I: Into<ArrayIndexes<'a, N>>,
§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());
sourcepub fn mut_at<'a, I, const N: usize>(&mut self, indexes: I) -> Result<&mut Self>where
I: Into<ArrayIndexes<'a, N>>,
pub fn mut_at<'a, I, const N: usize>(&mut self, indexes: I) -> Result<&mut Self>where
I: Into<ArrayIndexes<'a, N>>,
§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.
sourcepub fn take_at<'a, I, const N: usize>(&mut self, indexes: I) -> Result<Self>where
I: Into<ArrayIndexes<'a, N>>,
pub fn take_at<'a, I, const N: usize>(&mut self, indexes: I) -> Result<Self>where
I: Into<ArrayIndexes<'a, N>>,
§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());
sourcepub fn as_array(&self) -> Result<&Array>
pub fn as_array(&self) -> Result<&Array>
§If the value is an array, returns an immutable reference of it
Returns an error if the value is not an array.
sourcepub fn as_mut_array(&mut self) -> Result<&mut Array>
pub fn as_mut_array(&mut self) -> Result<&mut Array>
§If the value is an array, returns a mutable reference of it
Returns an error if the value is not an array.
source§impl Json
impl Json
sourcepub fn map_or<T, E>(self, default: T) -> Result<T, E>where
T: TryFrom<Self, Error = E>,
pub fn map_or<T, E>(self, default: T) -> Result<T, E>where
T: TryFrom<Self, Error = E>,
§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 map_or_else()
.
§Examples
let mut value = sj::parse_bytes("[null]")?;
assert!(value.take_at(0)?.map_or(true)?);
sourcepub fn map_or_else<T, E, F>(self, default: F) -> Result<T, E>
pub fn map_or_else<T, E, F>(self, default: F) -> Result<T, E>
sourcepub fn map_ref_or<'a, T, E>(&'a self, default: T) -> Result<T, E>
pub fn map_ref_or<'a, T, E>(&'a self, default: T) -> Result<T, E>
§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 map_ref_or_else()
.
§Examples
let value = sj::parse_bytes("[null]")?;
assert_eq!(value.at(0)?.map_ref_or(0)?, 0);
sourcepub fn map_ref_or_else<'a, T, E, F>(&'a self, default: F) -> Result<T, E>
pub fn map_ref_or_else<'a, T, E, F>(&'a self, default: F) -> Result<T, E>
sourcepub fn unwrap_or_null(json: Option<Self>) -> Self
pub fn unwrap_or_null(json: Option<Self>) -> Self
source§impl Json
impl Json
sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Self>>
pub fn insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Self>>
§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.
sourcepub fn by<'a, K, const N: usize>(&self, keys: K) -> Result<&Self>where
K: Into<ObjectIndexes<'a, N>>,
pub fn by<'a, K, const N: usize>(&self, keys: K) -> Result<&Self>where
K: Into<ObjectIndexes<'a, N>>,
§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());
sourcepub fn by_or_null<'a, K, const N: usize>(&self, keys: K) -> Result<&Self>where
K: Into<ObjectIndexes<'a, N>>,
pub fn by_or_null<'a, K, const N: usize>(&self, keys: K) -> Result<&Self>where
K: Into<ObjectIndexes<'a, N>>,
sourcepub fn maybe_by<'a, K, const N: usize>(&self, keys: K) -> Result<Option<&Self>>where
K: Into<ObjectIndexes<'a, N>>,
pub fn maybe_by<'a, K, const N: usize>(&self, keys: K) -> Result<Option<&Self>>where
K: Into<ObjectIndexes<'a, N>>,
§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.
sourcepub fn mut_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<&mut Self>where
K: Into<ObjectIndexes<'a, N>>,
pub fn mut_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<&mut Self>where
K: Into<ObjectIndexes<'a, N>>,
§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.
sourcepub fn maybe_mut_by<'a, K, const N: usize>(
&mut self,
keys: K
) -> Result<Option<&mut Self>>where
K: Into<ObjectIndexes<'a, N>>,
pub fn maybe_mut_by<'a, K, const N: usize>(
&mut self,
keys: K
) -> Result<Option<&mut Self>>where
K: Into<ObjectIndexes<'a, N>>,
§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.
sourcepub fn take_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<Self>where
K: Into<ObjectIndexes<'a, N>>,
pub fn take_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<Self>where
K: Into<ObjectIndexes<'a, N>>,
§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());
sourcepub fn take_by_or_null<'a, K, const N: usize>(
&mut self,
keys: K
) -> Result<Self>where
K: Into<ObjectIndexes<'a, N>>,
pub fn take_by_or_null<'a, K, const N: usize>(
&mut self,
keys: K
) -> Result<Self>where
K: Into<ObjectIndexes<'a, N>>,
sourcepub fn maybe_take_by<'a, K, const N: usize>(
&mut self,
keys: K
) -> Result<Option<Self>>where
K: Into<ObjectIndexes<'a, N>>,
pub fn maybe_take_by<'a, K, const N: usize>(
&mut self,
keys: K
) -> Result<Option<Self>>where
K: Into<ObjectIndexes<'a, N>>,
§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.
sourcepub fn as_object(&self) -> Result<&Object>
pub fn as_object(&self) -> Result<&Object>
§If the value is an object, returns an immutable reference of it
Returns an error if the value is not an object.
sourcepub fn as_mut_object(&mut self) -> Result<&mut Object>
pub fn as_mut_object(&mut self) -> Result<&mut Object>
§If the value is an object, returns a mutable reference of it
Returns an error if the value is not an object.
source§impl Json
impl Json
sourcepub fn as_str(&self) -> Result<&str>
pub fn as_str(&self) -> Result<&str>
§If the value is a string, returns an immutable reference of it
Returns an error if the value is not a string.
sourcepub fn as_mut_str(&mut self) -> Result<&mut String>
pub fn as_mut_str(&mut self) -> Result<&mut String>
§If the value is a string, returns a mutable reference of it
Returns an error if the value is not a string.
source§impl Json
impl Json
sourcepub fn format_as_bytes(&self) -> IoResult<Vec<u8>>
Available on crate feature std
only.
pub fn format_as_bytes(&self) -> IoResult<Vec<u8>>
std
only.§Formats this value as a compacted JSON string
sourcepub fn format_nicely_as_bytes(&self, tab: Option<u8>) -> IoResult<Vec<u8>>
Available on crate feature std
only.
pub fn format_nicely_as_bytes(&self, tab: Option<u8>) -> IoResult<Vec<u8>>
std
only.§Nicely formats this value as JSON string
If you don’t provide tab size, default (4
) will be used.
sourcepub fn format(&self) -> IoResult<String>
Available on crate feature std
only.
pub fn format(&self) -> IoResult<String>
std
only.§Formats this value as a compacted JSON string
sourcepub fn format_nicely(&self, tab: Option<u8>) -> IoResult<String>
Available on crate feature std
only.
pub fn format_nicely(&self, tab: Option<u8>) -> IoResult<String>
std
only.§Nicely formats this value as JSON string
If you don’t provide tab size, default (4
) will be used.
sourcepub fn write<W>(&self, stream: &mut W) -> IoResult<()>where
W: Write,
Available on crate feature std
only.
pub fn write<W>(&self, stream: &mut W) -> IoResult<()>where
W: Write,
std
only.Trait Implementations§
source§impl From<&NonZero<i128>> for Json
impl From<&NonZero<i128>> for Json
source§fn from(n: &NonZeroI128) -> Self
fn from(n: &NonZeroI128) -> Self
source§impl From<&NonZero<i16>> for Json
impl From<&NonZero<i16>> for Json
source§fn from(n: &NonZeroI16) -> Self
fn from(n: &NonZeroI16) -> Self
source§impl From<&NonZero<i32>> for Json
impl From<&NonZero<i32>> for Json
source§fn from(n: &NonZeroI32) -> Self
fn from(n: &NonZeroI32) -> Self
source§impl From<&NonZero<i64>> for Json
impl From<&NonZero<i64>> for Json
source§fn from(n: &NonZeroI64) -> Self
fn from(n: &NonZeroI64) -> Self
source§impl From<&NonZero<isize>> for Json
impl From<&NonZero<isize>> for Json
source§fn from(n: &NonZeroIsize) -> Self
fn from(n: &NonZeroIsize) -> Self
source§impl From<&NonZero<u128>> for Json
impl From<&NonZero<u128>> for Json
source§fn from(n: &NonZeroU128) -> Self
fn from(n: &NonZeroU128) -> Self
source§impl From<&NonZero<u16>> for Json
impl From<&NonZero<u16>> for Json
source§fn from(n: &NonZeroU16) -> Self
fn from(n: &NonZeroU16) -> Self
source§impl From<&NonZero<u32>> for Json
impl From<&NonZero<u32>> for Json
source§fn from(n: &NonZeroU32) -> Self
fn from(n: &NonZeroU32) -> Self
source§impl From<&NonZero<u64>> for Json
impl From<&NonZero<u64>> for Json
source§fn from(n: &NonZeroU64) -> Self
fn from(n: &NonZeroU64) -> Self
source§impl From<&NonZero<usize>> for Json
impl From<&NonZero<usize>> for Json
source§fn from(n: &NonZeroUsize) -> Self
fn from(n: &NonZeroUsize) -> Self
source§impl From<NonZero<i128>> for Json
impl From<NonZero<i128>> for Json
source§fn from(n: NonZeroI128) -> Self
fn from(n: NonZeroI128) -> Self
source§impl From<NonZero<i16>> for Json
impl From<NonZero<i16>> for Json
source§fn from(n: NonZeroI16) -> Self
fn from(n: NonZeroI16) -> Self
source§impl From<NonZero<i32>> for Json
impl From<NonZero<i32>> for Json
source§fn from(n: NonZeroI32) -> Self
fn from(n: NonZeroI32) -> Self
source§impl From<NonZero<i64>> for Json
impl From<NonZero<i64>> for Json
source§fn from(n: NonZeroI64) -> Self
fn from(n: NonZeroI64) -> Self
source§impl From<NonZero<isize>> for Json
impl From<NonZero<isize>> for Json
source§fn from(n: NonZeroIsize) -> Self
fn from(n: NonZeroIsize) -> Self
source§impl From<NonZero<u128>> for Json
impl From<NonZero<u128>> for Json
source§fn from(n: NonZeroU128) -> Self
fn from(n: NonZeroU128) -> Self
source§impl From<NonZero<u16>> for Json
impl From<NonZero<u16>> for Json
source§fn from(n: NonZeroU16) -> Self
fn from(n: NonZeroU16) -> Self
source§impl From<NonZero<u32>> for Json
impl From<NonZero<u32>> for Json
source§fn from(n: NonZeroU32) -> Self
fn from(n: NonZeroU32) -> Self
source§impl From<NonZero<u64>> for Json
impl From<NonZero<u64>> for Json
source§fn from(n: NonZeroU64) -> Self
fn from(n: NonZeroU64) -> Self
source§impl From<NonZero<usize>> for Json
impl From<NonZero<usize>> for Json
source§fn from(n: NonZeroUsize) -> Self
fn from(n: NonZeroUsize) -> Self
source§impl<K, V> FromIterator<(K, V)> for Json
impl<K, V> FromIterator<(K, V)> for Json
source§impl FromIterator<Json> for Json
impl FromIterator<Json> for Json
source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = Self>,
fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = Self>,
source§impl PartialEq<Json> for NonZeroI128
impl PartialEq<Json> for NonZeroI128
source§impl PartialEq<Json> for NonZeroI16
impl PartialEq<Json> for NonZeroI16
source§impl PartialEq<Json> for NonZeroI32
impl PartialEq<Json> for NonZeroI32
source§impl PartialEq<Json> for NonZeroI64
impl PartialEq<Json> for NonZeroI64
source§impl PartialEq<Json> for NonZeroI8
impl PartialEq<Json> for NonZeroI8
source§impl PartialEq<Json> for NonZeroIsize
impl PartialEq<Json> for NonZeroIsize
source§impl PartialEq<Json> for NonZeroU128
impl PartialEq<Json> for NonZeroU128
source§impl PartialEq<Json> for NonZeroU16
impl PartialEq<Json> for NonZeroU16
source§impl PartialEq<Json> for NonZeroU32
impl PartialEq<Json> for NonZeroU32
source§impl PartialEq<Json> for NonZeroU64
impl PartialEq<Json> for NonZeroU64
source§impl PartialEq<Json> for NonZeroU8
impl PartialEq<Json> for NonZeroU8
source§impl PartialEq<Json> for NonZeroUsize
impl PartialEq<Json> for NonZeroUsize
source§impl PartialEq<Json> for String
impl PartialEq<Json> for String
source§impl PartialEq<Json> for bool
impl PartialEq<Json> for bool
source§impl PartialEq<Json> for f32
impl PartialEq<Json> for f32
source§impl PartialEq<Json> for f64
impl PartialEq<Json> for f64
source§impl PartialEq<Json> for i128
impl PartialEq<Json> for i128
source§impl PartialEq<Json> for i16
impl PartialEq<Json> for i16
source§impl PartialEq<Json> for i32
impl PartialEq<Json> for i32
source§impl PartialEq<Json> for i64
impl PartialEq<Json> for i64
source§impl PartialEq<Json> for i8
impl PartialEq<Json> for i8
source§impl PartialEq<Json> for isize
impl PartialEq<Json> for isize
source§impl PartialEq<Json> for str
impl PartialEq<Json> for str
source§impl PartialEq<Json> for u128
impl PartialEq<Json> for u128
source§impl PartialEq<Json> for u16
impl PartialEq<Json> for u16
source§impl PartialEq<Json> for u32
impl PartialEq<Json> for u32
source§impl PartialEq<Json> for u64
impl PartialEq<Json> for u64
source§impl PartialEq<Json> for u8
impl PartialEq<Json> for u8
source§impl PartialEq<Json> for usize
impl PartialEq<Json> for usize
source§impl PartialEq<NonZero<i128>> for Json
impl PartialEq<NonZero<i128>> for Json
source§fn eq(&self, other: &NonZeroI128) -> bool
fn eq(&self, other: &NonZeroI128) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<i16>> for Json
impl PartialEq<NonZero<i16>> for Json
source§fn eq(&self, other: &NonZeroI16) -> bool
fn eq(&self, other: &NonZeroI16) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<i32>> for Json
impl PartialEq<NonZero<i32>> for Json
source§fn eq(&self, other: &NonZeroI32) -> bool
fn eq(&self, other: &NonZeroI32) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<i64>> for Json
impl PartialEq<NonZero<i64>> for Json
source§fn eq(&self, other: &NonZeroI64) -> bool
fn eq(&self, other: &NonZeroI64) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<i8>> for Json
impl PartialEq<NonZero<i8>> for Json
source§impl PartialEq<NonZero<isize>> for Json
impl PartialEq<NonZero<isize>> for Json
source§fn eq(&self, other: &NonZeroIsize) -> bool
fn eq(&self, other: &NonZeroIsize) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<u128>> for Json
impl PartialEq<NonZero<u128>> for Json
source§fn eq(&self, other: &NonZeroU128) -> bool
fn eq(&self, other: &NonZeroU128) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<u16>> for Json
impl PartialEq<NonZero<u16>> for Json
source§fn eq(&self, other: &NonZeroU16) -> bool
fn eq(&self, other: &NonZeroU16) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<u32>> for Json
impl PartialEq<NonZero<u32>> for Json
source§fn eq(&self, other: &NonZeroU32) -> bool
fn eq(&self, other: &NonZeroU32) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<u64>> for Json
impl PartialEq<NonZero<u64>> for Json
source§fn eq(&self, other: &NonZeroU64) -> bool
fn eq(&self, other: &NonZeroU64) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<NonZero<u8>> for Json
impl PartialEq<NonZero<u8>> for Json
source§impl PartialEq<NonZero<usize>> for Json
impl PartialEq<NonZero<usize>> for Json
source§fn eq(&self, other: &NonZeroUsize) -> bool
fn eq(&self, other: &NonZeroUsize) -> bool
self
and other
values to be equal, and is used
by ==
.