An open API service indexing awesome lists of open source software.

https://github.com/naomijub/serde_json_shape

Crate to help define the shape of a json file
https://github.com/naomijub/serde_json_shape

format json shape

Last synced: 7 months ago
JSON representation

Crate to help define the shape of a json file

Awesome Lists containing this project

README

          

[![Latest Version](https://img.shields.io/crates/v/json_shape)](https://crates.io/crates/json_shape)
[![License:Apache](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Build Status](https://github.com/naomijub/serde_json_shape/actions/workflows/rust.yml/badge.svg?branch=main)](https://github.com/naomijub/serde_json_shape/actions/workflows/rust.yml)
[![Coverage Status](https://coveralls.io/repos/github/naomijub/serde_json_shape/badge.svg?branch=main)](https://coveralls.io/github/naomijub/serde_json_shape?branch=main)

# JSON_Shape

This libraries is not intended to serialize a JSON into a value representation like [`serde_json`](https://crates.io/crates/serde_json) does but to represent that types of data that a json or multiple jsons have:

```json
{
"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:

```ru
Object{
array: Array,
"array of maps": Array,
c: Option
}>,
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 + U = OneOf[T | U]`
- `T + Option = OneOf[T | U | Null]`
- `Tuple(U, T, V) + Tuple(U, T, Null) = Tuple(U, T, Option)`
- `Array + Array => Array`
- `Tuple(U, T, V) + Array = Array`
- `Object{key: Number, "key space": Bool} + Object{key: String, "key_special_char?": String} => Object{key: OneOf[Number | String], "key space": Option, "key_special_char?": Option }`
- `OneOf[T | U] + OneOf[V | X] = OneOf[T | U | V | X]`
- `OneOf[T | U] + Option = 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:

```shell
$ cargo add json_shape
```

Or add the following line to your Cargo.toml:

```toml
[dependencies]
json_shape = "0.5"
```

## Usage

### From `String`s

```rust
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`](https://docs.rs/json_shape/latest/json_shape/enum.JsonShape.html#method.from_sources), which expects a list of Json strings.

### From `serde_json::Value`

```rust
use std::{fs::read_to_string, str::FromStr};

use json_shape::JsonShape;
use serde_json::Value;

let json_str = read_to_string("./testdata/rfc-9535-example-1.json").unwrap();
let json: Value = serde_json::from_str(&json_str).unwrap();
let shape_from_value = JsonShape::from(&json);
```

# Json_shape_build

Auxiliary library to generate Data Structures from Json Sources:

## Add dependency
```toml
[build-dependencies]
json_shape_build = "0.1"
```

In your `build.rs`:
```rust,ignore
let dir = env!("CARGO_MANIFEST_DIR");
let extension = "fixture/object.json";
let path = std::path::Path::new(dir).join(extension);
json_shape_build::compile_json("collection_name", &[path]);
```

To include in you project:
```rust,ignore
mod shapes {
json_shape_build::include_json_shape!("helloworld");
}
```