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.
Number
Represents a JSON number.
String
Represents a JSON string.
Array
Represents a JSON array.
Object
Represents a JSON object.
Fields
OneOf
Represents a JSON Value that can assume one of the Values described. Similar to an enum containing diffenrent internal types in Rust.
Tuple
Represents a JSON Array that behaves like a tuple. Similar to a Rust tuple, types are always the same and in same order
Implementations§
Source§impl Value
impl Value
Sourcepub fn is_tuple_of(&self, types: &[Value]) -> bool
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
impl Value
Source§impl Value
impl Value
Sourcepub fn from_sources(sources: &[&str]) -> Result<Self, Error>
pub fn from_sources(sources: &[&str]) -> Result<Self, Error>
Sourcepub fn is_superset(&self, json: &str) -> bool
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)
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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 FromStr for Value
Creates a JsonShape
from a single Json 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§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]}
impl IsSubset for Value
JsonShape::Number
is subset ofJsonShape::Option<Number>
JsonShape::Null
is subset ofJsonShape::Option<Number>
andJsonShape::Null
JsonShape::Number
is subset ofJsonShape::OneOf[Number | String]
JsonShape::Number
is NOT subset ofJsonShape::Array<Number>
=>1.23 != [1.23]
JsonShape::Array<Number>
is subset ofJsonShape::Array<OnOf<[Number | Boolean]>>
JsonShape::Object{"key_a": JsonShape::Number}
is NOT subset ofJsonShape::Object{"key_b": JsonShape::Number}
=>key_a != key_b
JsonShape::Object{"key_a": JsonShape::Number}
is subset ofJsonShape::Object{"key_a": JsonShape::Option<Number>}
JsonShape::Object{"key_a": JsonShape::Number}
is subset ofJsonShape::Object{"key_a": JsonShape::OneOf[Number | Boolean]}
Source§impl Ord for Value
impl Ord for Value
Source§impl PartialOrd for Value
impl PartialOrd for Value
impl Eq for Value
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more