Crate json_shape

Source
Expand description

Latest Version License:Apache Build Status Coverage Status

§JSON_Shape

This libraries is not intended to serialize a JSON into a value representation like serde_json does but to represent that types of data that a json or multiple jsons have:

{
    "str": "this is a string",
    "number": 123.456,
    "array": [1, 2, 3, 4],
    "bool_true": true,
    "bool_false": false,
    "nil": null,
    "tuple": [123, "string", true],
    "map": {
        "a": "b",
        "c": 123
    },
    "array of maps": [
        {
            "a": "b",
            "c": 123
        },
        {
            "a": "b",
            "b": true
        }
    ]
}

Will be parsed as:

Object{
    array: Array<Number>,
    "array of maps": Array<Object{
        a: String, 
        b: Option<Boolean>, 
        c: Option<Number>
    }>, 
    bool_false: Boolean, 
    bool_true: Boolean, 
    map: Object{
        a: String, 
        c: Number
    }, 
    nil: Null, 
    number: Number, 
    str: String,
    tuple: Tuple(Boolean, Number, String)
}

§General rules when merging two JsonShape:

  • T + Null = Option<T>
  • T + U = OneOf[T | U]
  • T + Option<U> = OneOf[T | U | Null]
  • Tuple(U, T, V) + Tuple(U, T, Null) = Tuple(U, T, Option<V>)
  • Array<T> + Array<U> => Array<OneOf[T | U]>
  • Tuple(U, T, V) + Array<U> = Array<OneOf[T | U | V]>
  • Object{key: Number, "key space": Bool} + Object{key: String, "key_special_char?": String} => Object{key: OneOf[Number | String], "key space": Option<Bool>, "key_special_char?": Option<String> }
  • OneOf[T | U] + OneOf[V | X] = OneOf[T | U | V | X]
  • OneOf[T | U] + Option<U> = OneOf[T | U | Null]

§Usage Warning

This library does not conform to Swagger or JsonSchema specifications, as they are signiticantly more complex than the intended usage for this library.

§Installation

Run the following Cargo command in your project directory:

$ cargo add json_shape

Or add the following line to your Cargo.toml:

[dependencies]
json_shape = "0.1"

§Usage

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

let source = r#"{
        "str": "this is a string",
        "number": 123.456,
        "bool_true": true,
        "bool_false": false,
        "nil": null,
        "tuple": [123, "string", true],
        "map": {
          "a": "b",
          "c": 123
        },
        "array of maps": [
            {
                "a": "b",
                "c": 123
            },
            {
                "a": "b",
                "b": true
            }
        ]
    }"#;

let json_shape = JsonShape::from_str(source).unwrap();
  • If multiple JSON sources are available, you may use JsonShape::from_sources, which expects a list of Json strings.

Modules§

error
Module containing Error types

Enums§

JsonShape
Represents any valid JSON value shape.

Traits§

IsSubset
Determines if T::self is subset of T. Only implemented for JsonShape/Value. A few rule examples
Similar
Helper trait to identify when two JsonShapes are similar but not necessarily equal, meaning they only diverge in being optional.