Enum JsonShape

Source
pub enum JsonShape {
    Null,
    Bool {
        optional: bool,
    },
    Number {
        optional: bool,
    },
    String {
        optional: bool,
    },
    Array {
        type: Box<Value>,
        optional: bool,
    },
    Object {
        content: BTreeMap<String, Value>,
        optional: bool,
    },
    OneOf {
        variants: BTreeSet<Value>,
        optional: bool,
    },
    Tuple {
        elements: Vec<Value>,
        optional: bool,
    },
}
Expand description

Represents any valid JSON value shape.

See the serde_json_shape::value module documentation for usage examples.

Variants§

§

Null

Represents a JSON null value.

§

Bool

Represents a JSON boolean.

Fields

§optional: bool

If type is optional

§

Number

Represents a JSON number.

Fields

§optional: bool

If type is optional

§

String

Represents a JSON string.

Fields

§optional: bool

If type is optional

§

Array

Represents a JSON array.

Fields

§type: Box<Value>

Type contained in the Array

§optional: bool

If type is optional

§

Object

Represents a JSON object.

Fields

§content: BTreeMap<String, Value>

Object internal members map, with key as String and value as [JsonShape]

§optional: bool

If type is optional

§

OneOf

Represents a JSON Value that can assume one of the Values described. Similar to an enum containing diffenrent internal types in Rust.

Fields

§variants: BTreeSet<Value>

All possible [JsonShape] values

§optional: bool

If type is optional

§

Tuple

Represents a JSON Array that behaves like a tuple. Similar to a Rust tuple, types are always the same and in same order

Fields

§elements: Vec<Value>

[JsonShape] order

§optional: bool

If type is optional

Implementations§

Source§

impl Value

Source

pub fn is_tuple_of(&self, types: &[Value]) -> bool

Cheecks if [JsonShape::Tuple] is tuple containing &[JsonShape] in the same order and type.

Source§

impl Value

Source

pub const fn is_optional(&self) -> bool

Is this [JsonShape] optional? eg, Option<String>

Source

pub fn keys(&self) -> Option<Keys<'_, String, Value>>

Return the keys contained in a [JsonShape::Object]

Source

pub const fn is_null(&self) -> bool

Checks if Json Node is null

Source

pub const fn is_boolean(&self) -> bool

Checks if Json Node is boolean

Source

pub const fn is_number(&self) -> bool

Checks if Json Node is number

Source

pub const fn is_string(&self) -> bool

Checks if Json Node is string

Source

pub const fn is_array(&self) -> bool

Checks if Json Node is array

Source

pub const fn is_tuple(&self) -> bool

Checks if Json Node is tuple

Source

pub const fn is_object(&self) -> bool

Checks if Json Node is object

Source

pub const fn is_oneof(&self) -> bool

Checks if Json Node is one of

Source§

impl Value

Source

pub fn from_sources(sources: &[&str]) -> Result<Self, Error>

Creates a JsonShape from multiple Json sources

§Errors

Will return Err if failed to parse Json or if shapes don’t align.

Source

pub fn is_superset(&self, json: &str) -> bool

Checks if Json is subset of specific JsonShape

use std::str::FromStr;

use json_shape::{IsSubset, JsonShape};
let shape = JsonShape::Object { content: [
    ("name".to_string(), JsonShape::String { optional: false }),
    ("surname".to_string(), JsonShape::String { optional: false }),
    ("middle name".to_string(), JsonShape::String { optional: true }),
    ("age".to_string(), JsonShape::Number { optional: false }),
    ("id".to_string(), JsonShape::OneOf { variants: [
        JsonShape::Object { content: [
            ("number".to_string(), JsonShape::Number { optional: false }),
            ("state".to_string(), JsonShape::String { optional: false }),
        ].into(), optional: false },
        JsonShape::Array { r#type: Box::new(JsonShape::Number { optional: false }), optional: false }
    ].into(), optional: false })
].into(), optional: false };

let json = r#"{
"name": "lorem",
"surname": "ipsum",
"age": 30,
"id": {
    "number": 123456,
    "state": "st"
}
}"#;

let shape_1 = JsonShape::from_str(json).unwrap();

shape_1.is_subset(&shape)
shape.is_superset(json)
Source

pub fn is_superset_checked(&self, json: &str) -> Result<bool, Error>

Checks if Json is subset of specific JsonShape

  • Checked version of [is_superset]
§Errors

Returns Err if failed to parse Json

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Value

Source§

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

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

impl FromStr for Value

Creates a JsonShape from a single Json source

use json_shape::JsonShape;
use std::str::FromStr;

let source = "[12, 34, 56]";
let json_shape = JsonShape::from_str(source).unwrap();
Source§

type Err = Error

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

fn from_str(source: &str) -> Result<Self, Self::Err>

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

impl Hash for Value

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IsSubset for Value

  • JsonShape::Number is subset of JsonShape::Option<Number>
  • JsonShape::Null is subset of JsonShape::Option<Number> and JsonShape::Null
  • JsonShape::Number is subset of JsonShape::OneOf[Number | String]
  • JsonShape::Number is NOT subset of JsonShape::Array<Number> => 1.23 != [1.23]
  • JsonShape::Array<Number> is subset of JsonShape::Array<OnOf<[Number | Boolean]>>
  • JsonShape::Object{"key_a": JsonShape::Number} is NOT subset of JsonShape::Object{"key_b": JsonShape::Number} => key_a != key_b
  • JsonShape::Object{"key_a": JsonShape::Number} is subset of JsonShape::Object{"key_a": JsonShape::Option<Number>}
  • JsonShape::Object{"key_a": JsonShape::Number} is subset of JsonShape::Object{"key_a": JsonShape::OneOf[Number | Boolean]}
Source§

fn is_subset(&self, other: &Self) -> bool

Checks if [JsonShape] is subset of other [JsonShape]

Source§

impl Ord for Value

Source§

fn cmp(&self, other: &Value) -> 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 Value

Source§

fn eq(&self, other: &Value) -> 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 Value

Source§

fn partial_cmp(&self, other: &Value) -> 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 Serialize for Value

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Similar for Value

Source§

fn similar(&self, other: &Self) -> Option<Value>

Tests for self and other values to be similar (equal ignoring the optional), returning the optional version
Source§

impl Eq for Value

Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,