JsonPreparator
Russian Readme
Russian Readme is here, (*1)
About
Service for the preparation of JSON according to the proposed scheme. Adds missing elements, removes unnecessary items., (*2)
Install
```$bash
composer require kaizer666/json-preparator, (*3)
## Usage
```$php
use Json\JsonPreparator;
function test() {
$require = true;
$schema = "/path/to/schema.json";
$data = json_decode('{
"integer_with_def": 2.0002,
"float": 123000,
"string": "text",
"object": {},
"array_string": [
"1.1.1.1.1.1.",
"1"
],
"array_integer": [
"1.1.1.1.1.",
"2.2.2.2."
],
"array": [
{
"integer_with_def": 1,
"float": 1000,
"text": "text1"
},{
"integer_with_def": 2,
"float": 2000,
"text": "text2"
},{
"integer_with_def": 3,
"float": 3000,
"text": "text3"
},{
"integer_with_def": 4,
"float": 4000,
"text": "text4"
}
]
}', true);
$preparator = new JsonPreparator($schema, $require);
$output = $preparator->prepare($data);
return response()->json($output);
}
Schema example
```$json
{
"integer_with_def": {
"type": "integer",
"default": "32",
"require": false
},
"integer": {
"type": "integer"
},
"float": {
"type": "float",
"format": {
"decimals":2,
"dec_point":".",
"thousands_sep":" "
},
"require": false
},
"string": {
"type":"string"
},
"object": {
"type":"object",
"components": {
"integer_with_def": {
"type": "integer",
"default": "32",
"require": false
},
"integer": {
"type": "integer"
},
"float": {
"type": "float",
"format": {
"decimals":2,
"dec_point":".",
"thousands_sep":" "
}
}
}
},
"array_string":{
"type": "array",
"values": {
"type": "string",
"require": false
}
},
"array_integer":{
"type": "array",
"values": {
"type": "integer"
}
},
"array": {
"type": "array",
"values": {
"type": "object",
"components": {
"integer_with_def": {
"type": "integer",
"default": "32"
},
"integer": {
"type": "integer"
},
"float": {
"type": "float",
"format": {
"decimals":2,
"dec_point":".",
"thousands_sep":" "
}
},
"string": {
"type":"string"
},
"array_string":{
"type": "array",
"values": {
"type": "string"
}
},
"array_integer":{
"type": "array",
"values": {
"type": "integer"
}
}
}
}
}
}, (*4)
## Field Types
- "integer"
- Integer number
- Options:
a) default - the default value if there is no element in the incoming array
b) require - a required value
- "float"
- A float number
- Options:
a) default - the default value if there is no element in the incoming array
b) format - number format:
- decimals - how many decimal places
- dec_point - decimal separator
- thousands_sep - thousands separator
c) require - a required value
- "string"
- String
- Options:
a) default - the default value if there is no element in the incoming array
b) require - a required value
- "object"
- Object {} (associative array)
- Options:
a) components - the list of components of the object
b) require - a required value
- "array"
- Array []
- Options:
a) values - list of array values
b) require - a required value
```, (*5)