Enum jsondata::Json [−][src]
Json type implements JavaScript Object Notation as per specification RFC-8259.
- JSON scalar types - Null, Number, Boolean, String, are supported.
- JSON container types - Array, Object, are supported.
- JSON numbers can be 128-bit integers or 64-bit floating point.
- When document is known to contain lot of numbers and only one of
them needs to be extracted, parsing the entire document can be
inefficient just to get that one field. Numbers are implemented
with deferred conversion, using
Integral
andFloating
types. - Arrays are implemented as vector of Json values Vec<Json>.
- Objects are implemented as vector of properties, Vec<Property>, where each property is a tuple of (key, value). Here key is String type and value is Json type.
Json enum type has documented variants and undocumented variants. Applications, when matching with Json, must use the catch-all variant:
match json { Json::Null => // handle null, Json::Bool(b) => // handle bool, Json::Integer(i) => // handle integer, Json::Float(f) => // handle float, Json::String(s) => // handle string, Json::Array(a) => // handle array, Json::Object(o) => // handle object, _ => // catch all. }
Parsing JSON text:
let json: jsondata::Json = "10".parse().unwrap();
return a Json enum type.
Converting Rust native types to Json enum:
Json supports conversion from bool, u8, i8, u16, i16, u32,
i32, u64, i64, i128, i128, f32, f64, String, str,
Vec<Json> and Vec<Property> types using the From trait.
Converting from u128 shall cause panic if value is larger than
i128::max()
.
let json: jsondata::Json = 10.into(); let json: jsondata::Json = true.into(); let json: jsondata::Json = "hello world".into();
On the other direction, Json enum can be converted to Rust native types using accessor methods,
- is_null() to check whether Json is Null
- to_bool(), to_integer(), to_float(), to_string() methods return the
underlying value as Option<
T
> whereT
is bool or i128 or f64 or String. - to_array(), return JSON array as Vec<Json>.
- to_object(), return JSON object as Vec<Property>.
- Refer to API list and conversion traits implementation for more details.
Some of the properties implemented for Json are:
- Json implements total ordering.
- Default value for Json is Null.
- Json types are clone-able but do not implement Copy.
- Json value can be serialized into JSON format using Display trait.
Panics
Json implements AsRef and AsMut traits for str, Vec<Json>, Vec<Property> types. This means, call to as_ref() and as_mut() shall panic when underlying Json variant do not match with expected type.
Variants
Bool(bool)
String(String)
Implementations
impl Json
[src]
Implementation provides methods to construct and validate Json values.
pub fn new<T>(value: T) -> Json where
Self: From<T>,
[src]
Self: From<T>,
Construct Json from bool, i128, f64, String, str, Vec.
Array can be composed as:
use jsondata::Json; let mut js = Json::new::<Vec<Json>>(Vec::new()); js.append("", Json::new(10)); js.append("", Json::new("hello world".to_string()));
It is also possible to construct the vector of Json outside the append() method, and finally use Json::new() to construct the array.
Object can be composed as:
use jsondata::{Json, Property}; let mut js = Json::new::<Vec<Property>>(Vec::new()); js.set("/key1", Json::new(10)); js.set("/key2", Json::new(true));
It is also possible to construct the vector of properties outside the set() method, and finally use Json::new() to construct the object.
pub fn validate(&mut self) -> Result<()>
[src]
Validate parts of JSON text that are not yet parsed. Typically, when used in database context, JSON documents are validated once but parsed multiple times.
pub fn compute(&mut self) -> Result<()>
[src]
Compute parses unparsed text and convert them into numbers. When a JSON document is parsed once but operated on multiple times it is better to call compute for better performance.
use jsondata::Json; let text = r#"[null,true,false,10,"true"]"#; let mut json: Json = text.parse().unwrap(); json.compute(); // perform lookup and arithmetic operations on parsed document.
pub fn typename(&self) -> String
[src]
impl Json
[src]
Implementation provides CRUD access into Json document using Json Pointer. For all methods,
- Path must be valid JSON Pointer.
- Path fragment must be valid key if parent container is an object.
- Path fragment must be a number index if parent container is an array.
pub fn get(&self, path: &str) -> Result<Json>
[src]
Get a json field, within the document, locatable by path
.
pub fn set(&mut self, path: &str, value: Json) -> Result<()>
[src]
Set a json field, within the document, locatable by path
.
pub fn delete(&mut self, path: &str) -> Result<()>
[src]
Delete a JSON field, within the document, locatable by path
.
pub fn append(&mut self, path: &str, value: Json) -> Result<()>
[src]
Append a string or array to a JSON field within the document that is either a string or array.
pub fn range<R>(&self, range: R) -> Json where
R: RangeBounds<isize>,
[src]
R: RangeBounds<isize>,
Range operation on Json array,
- Range
[start..end]
. - RangeFrom
[start..]
. - RangeFull
[..]
. - RangeInclusive
[start..=end]
. - RangeTo
[..end]
. - RangeToInclusive
[..=end]
.
If range is called on non array Json, returns a Json Error.
impl Json
[src]
Implementation clones underlying type for each Json variant. The return value is always an Option because JSON follows a schema-less data representation.
pub fn is_null(&self) -> bool
[src]
pub fn to_bool(&self) -> Option<bool>
[src]
pub fn to_integer(&self) -> Option<i128>
[src]
pub fn to_float(&self) -> Option<f64>
[src]
pub fn as_str(&self) -> Option<&str>
[src]
pub fn to_array(&self) -> Option<Vec<Json>>
[src]
pub fn to_object(&self) -> Option<Vec<Property>>
[src]
pub fn is_error(&self) -> bool
[src]
pub fn to_error(&self) -> Option<Error>
[src]
pub fn to_result(&self) -> Result<&Json>
[src]
Trait Implementations
impl Add<Json> for Json
[src]
type Output = Json
The resulting type after applying the +
operator.
fn add(self, rhs: Json) -> Json
[src]
impl AsMut<Vec<Json, Global>> for Json
[src]
impl AsMut<Vec<Property, Global>> for Json
[src]
impl AsMut<str> for Json
[src]
impl AsRef<Vec<Json, Global>> for Json
[src]
impl AsRef<Vec<Property, Global>> for Json
[src]
impl AsRef<str> for Json
[src]
impl BitAnd<Json> for Json
[src]
type Output = Json
The resulting type after applying the &
operator.
fn bitand(self, rhs: Json) -> Json
[src]
impl BitOr<Json> for Json
[src]
type Output = Json
The resulting type after applying the |
operator.
fn bitor(self, rhs: Json) -> Json
[src]
impl BitXor<Json> for Json
[src]
type Output = Json
The resulting type after applying the ^
operator.
fn bitxor(self, rhs: Json) -> Json
[src]
impl Clone for Json
[src]
impl Debug for Json
[src]
impl Default for Json
[src]
impl Display for Json
[src]
impl Div<Json> for Json
[src]
type Output = Json
The resulting type after applying the /
operator.
fn div(self, rhs: Json) -> Json
[src]
impl Eq for Json
[src]
impl From<&'_ str> for Json
[src]
impl<A, B, C> From<(A, B, C)> for Json where
A: Into<Json>,
B: Into<Json>,
C: Into<Json>,
[src]
A: Into<Json>,
B: Into<Json>,
C: Into<Json>,
impl<T> From<(T,)> for Json where
T: Into<Json>,
[src]
T: Into<Json>,
impl<U, V> From<(U, V)> for Json where
U: Into<Json>,
V: Into<Json>,
[src]
U: Into<Json>,
V: Into<Json>,
impl From<String> for Json
[src]
impl From<Vec<Property, Global>> for Json
[src]
impl<T> From<Vec<T, Global>> for Json where
T: Into<Json>,
[src]
T: Into<Json>,
impl From<bool> for Json
[src]
impl From<f32> for Json
[src]
impl From<f64> for Json
[src]
impl From<i128> for Json
[src]
impl From<i16> for Json
[src]
impl From<i32> for Json
[src]
impl From<i64> for Json
[src]
impl From<i8> for Json
[src]
impl From<isize> for Json
[src]
impl From<u128> for Json
[src]
impl From<u16> for Json
[src]
impl From<u32> for Json
[src]
impl From<u64> for Json
[src]
impl From<u8> for Json
[src]
impl From<usize> for Json
[src]
impl FromStr for Json
[src]
type Err = Error
The associated error which can be returned from parsing.
fn from_str(text: &str) -> Result<Json>
[src]
impl Index<&'_ str> for Json
[src]
impl Index<isize> for Json
[src]
impl Mul<Json> for Json
[src]
type Output = Json
The resulting type after applying the *
operator.
fn mul(self, rhs: Json) -> Json
[src]
impl Neg for Json
[src]
impl Not for Json
[src]
impl Ord for Json
[src]
fn cmp(&self, other: &Json) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl PartialEq<Json> for Json
[src]
impl PartialOrd<Json> for Json
[src]
fn partial_cmp(&self, other: &Json) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl Rem<Json> for Json
[src]
type Output = Json
The resulting type after applying the %
operator.
fn rem(self, rhs: Json) -> Json
[src]
impl Shl<Json> for Json
[src]
type Output = Json
The resulting type after applying the <<
operator.
fn shl(self, rhs: Json) -> Json
[src]
impl Shr<Json> for Json
[src]
type Output = Json
The resulting type after applying the >>
operator.
fn shr(self, rhs: Json) -> Json
[src]
impl Sub<Json> for Json
[src]
Auto Trait Implementations
impl RefUnwindSafe for Json
impl Send for Json
impl Sync for Json
impl Unpin for Json
impl UnwindSafe for Json
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,