Enum Json

Source
pub enum Json {
    Null,
    Bool(bool),
    Integer(Integral),
    Float(Floating),
    String(String),
    Array(Vec<Json>),
    Object(Vec<Property>),
    // some variants omitted
}
Expand description

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 and Floating 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::from(10);
let json = jsondata::Json::from(true);
let json = jsondata::Json::from("hello world");

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> where T 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§

§

Null

§

Bool(bool)

§

Integer(Integral)

§

Float(Floating)

§

String(String)

§

Array(Vec<Json>)

§

Object(Vec<Property>)

Implementations§

Source§

impl Json

Implementation provides methods to construct and validate Json values.

Source

pub fn new<T>(value: T) -> Json
where 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.

Source

pub fn validate(&mut self) -> Result<()>

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.

Source

pub fn compute(&mut self) -> Result<()>

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.
Source

pub fn type_name(&self) -> String

Source§

impl Json

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.
Source

pub fn get(&self, path: &str) -> Result<Json>

Get a json field, within the document, locatable by path.

Source

pub fn set(&mut self, path: &str, value: Json) -> Result<()>

Set a json field, within the document, locatable by path.

Source

pub fn delete(&mut self, path: &str) -> Result<()>

Delete a JSON field, within the document, locatable by path.

Source

pub fn append(&mut self, path: &str, value: Json) -> Result<()>

Append a string or array to a JSON field within the document that is either a string or array.

Source

pub fn range<R>(&self, range: R) -> Json
where 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.

Source§

impl Json

Implementation clones underlying type for each Json variant. The return value is always an Option because JSON follows a schema-less data representation.

Source

pub fn is_null(&self) -> bool

Source

pub fn to_bool(&self) -> Option<bool>

Source

pub fn to_integer(&self) -> Option<i128>

Source

pub fn to_float(&self) -> Option<f64>

Source

pub fn as_str(&self) -> Option<&str>

Source

pub fn to_array(&self) -> Option<Vec<Json>>

Source

pub fn to_object(&self) -> Option<Vec<Property>>

Source

pub fn is_error(&self) -> bool

Source

pub fn to_error(&self) -> Option<Error>

Source

pub fn to_result(&self) -> Result<&Json>

Trait Implementations§

Source§

impl Add for Json

Source§

type Output = Json

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Json) -> Json

Performs the + operation. Read more
Source§

impl AsMut<Vec<Json>> for Json

Source§

fn as_mut(&mut self) -> &mut Vec<Json>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<Vec<Property>> for Json

Source§

fn as_mut(&mut self) -> &mut Vec<Property>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<str> for Json

Source§

fn as_mut(&mut self) -> &mut str

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<Vec<Json>> for Json

Source§

fn as_ref(&self) -> &Vec<Json>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Vec<Property>> for Json

Source§

fn as_ref(&self) -> &Vec<Property>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<str> for Json

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl BitAnd for Json

Source§

type Output = Json

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Json) -> Json

Performs the & operation. Read more
Source§

impl BitOr for Json

Source§

type Output = Json

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Json) -> Json

Performs the | operation. Read more
Source§

impl BitXor for Json

Source§

type Output = Json

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Json) -> Json

Performs the ^ operation. Read more
Source§

impl Clone for Json

Source§

fn clone(&self) -> Json

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Json

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Json

Source§

fn default() -> Json

Returns the “default value” for a type. Read more
Source§

impl Display for Json

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div for Json

Source§

type Output = Json

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Json) -> Json

Performs the / operation. Read more
Source§

impl From<&str> for Json

Source§

fn from(val: &str) -> Json

Converts to this type from the input type.
Source§

impl<A, B, C> From<(A, B, C)> for Json
where Json: From<A> + From<B> + From<C>,

Source§

fn from(val: (A, B, C)) -> Json

Converts to this type from the input type.
Source§

impl<T> From<(T,)> for Json
where Json: From<T>,

Source§

fn from(val: (T,)) -> Json

Converts to this type from the input type.
Source§

impl<U, V> From<(U, V)> for Json
where Json: From<U> + From<V>,

Source§

fn from(val: (U, V)) -> Json

Converts to this type from the input type.
Source§

impl From<Json> for bool

Source§

fn from(val: Json) -> bool

Converts to this type from the input type.
Source§

impl From<String> for Json

Source§

fn from(val: String) -> Json

Converts to this type from the input type.
Source§

impl From<Vec<Property>> for Json

Source§

fn from(val: Vec<Property>) -> Json

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for Json
where Json: From<T>,

Source§

fn from(val: Vec<T>) -> Json

Converts to this type from the input type.
Source§

impl From<bool> for Json

Source§

fn from(val: bool) -> Json

Converts to this type from the input type.
Source§

impl From<f32> for Json

Source§

fn from(val: f32) -> Json

Converts to this type from the input type.
Source§

impl From<f64> for Json

Source§

fn from(val: f64) -> Json

Converts to this type from the input type.
Source§

impl From<i128> for Json

Source§

fn from(val: i128) -> Json

Converts to this type from the input type.
Source§

impl From<i16> for Json

Source§

fn from(val: i16) -> Json

Converts to this type from the input type.
Source§

impl From<i32> for Json

Source§

fn from(val: i32) -> Json

Converts to this type from the input type.
Source§

impl From<i64> for Json

Source§

fn from(val: i64) -> Json

Converts to this type from the input type.
Source§

impl From<i8> for Json

Source§

fn from(val: i8) -> Json

Converts to this type from the input type.
Source§

impl From<isize> for Json

Source§

fn from(val: isize) -> Json

Converts to this type from the input type.
Source§

impl From<u128> for Json

Source§

fn from(val: u128) -> Json

Converts to this type from the input type.
Source§

impl From<u16> for Json

Source§

fn from(val: u16) -> Json

Converts to this type from the input type.
Source§

impl From<u32> for Json

Source§

fn from(val: u32) -> Json

Converts to this type from the input type.
Source§

impl From<u64> for Json

Source§

fn from(val: u64) -> Json

Converts to this type from the input type.
Source§

impl From<u8> for Json

Source§

fn from(val: u8) -> Json

Converts to this type from the input type.
Source§

impl From<usize> for Json

Source§

fn from(val: usize) -> Json

Converts to this type from the input type.
Source§

impl FromStr for Json

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(text: &str) -> Result<Json>

Parses a string s to return a value of this type. Read more
Source§

impl Index<&str> for Json

Source§

type Output = Json

The returned type after indexing.
Source§

fn index(&self, index: &str) -> &Json

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<isize> for Json

Source§

type Output = Json

The returned type after indexing.
Source§

fn index(&self, index: isize) -> &Json

Performs the indexing (container[index]) operation. Read more
Source§

impl Mul for Json

Source§

type Output = Json

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Json) -> Json

Performs the * operation. Read more
Source§

impl Neg for Json

Source§

type Output = Json

The resulting type after applying the - operator.
Source§

fn neg(self) -> Json

Performs the unary - operation. Read more
Source§

impl Not for Json

Source§

type Output = Json

The resulting type after applying the ! operator.
Source§

fn not(self) -> Json

Performs the unary ! operation. Read more
Source§

impl Ord for Json

Source§

fn cmp(&self, other: &Json) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Json

Source§

fn eq(&self, other: &Json) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Json

Source§

fn partial_cmp(&self, other: &Json) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Rem for Json

Source§

type Output = Json

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Json) -> Json

Performs the % operation. Read more
Source§

impl Shl for Json

Source§

type Output = Json

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Json) -> Json

Performs the << operation. Read more
Source§

impl Shr for Json

Source§

type Output = Json

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Json) -> Json

Performs the >> operation. Read more
Source§

impl Sub for Json

Source§

type Output = Json

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Json) -> Json

Performs the - operation. Read more
Source§

impl<A, B, C> TryFrom<Json> for (A, B, C)
where A: TryFrom<Json, Error = Error>, B: TryFrom<Json, Error = Error>, C: TryFrom<Json, Error = Error>,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<(A, B, C)>

Performs the conversion.
Source§

impl<T> TryFrom<Json> for (T,)
where T: TryFrom<Json, Error = Error>,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<(T,)>

Performs the conversion.
Source§

impl<U, V> TryFrom<Json> for (U, V)
where U: TryFrom<Json, Error = Error>, V: TryFrom<Json, Error = Error>,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<(U, V)>

Performs the conversion.
Source§

impl TryFrom<Json> for String

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<String>

Performs the conversion.
Source§

impl TryFrom<Json> for Vec<Property>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<Vec<Property>>

Performs the conversion.
Source§

impl<T> TryFrom<Json> for Vec<T>
where T: TryFrom<Json, Error = Error>,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<Vec<T>>

Performs the conversion.
Source§

impl TryFrom<Json> for f32

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<f32>

Performs the conversion.
Source§

impl TryFrom<Json> for f64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<f64>

Performs the conversion.
Source§

impl TryFrom<Json> for i128

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<i128>

Performs the conversion.
Source§

impl TryFrom<Json> for i16

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<i16>

Performs the conversion.
Source§

impl TryFrom<Json> for i32

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<i32>

Performs the conversion.
Source§

impl TryFrom<Json> for i64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<i64>

Performs the conversion.
Source§

impl TryFrom<Json> for i8

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<i8>

Performs the conversion.
Source§

impl TryFrom<Json> for isize

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<isize>

Performs the conversion.
Source§

impl TryFrom<Json> for u128

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<u128>

Performs the conversion.
Source§

impl TryFrom<Json> for u16

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<u16>

Performs the conversion.
Source§

impl TryFrom<Json> for u32

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<u32>

Performs the conversion.
Source§

impl TryFrom<Json> for u64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<u64>

Performs the conversion.
Source§

impl TryFrom<Json> for u8

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<u8>

Performs the conversion.
Source§

impl TryFrom<Json> for usize

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: Json) -> Result<usize>

Performs the conversion.
Source§

impl Eq for Json

Auto Trait Implementations§

§

impl Freeze for Json

§

impl RefUnwindSafe for Json

§

impl Send for Json

§

impl Sync for Json

§

impl Unpin for Json

§

impl UnwindSafe for Json

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.