A PHP library to improve decoding - including schema validation and decoding to a class
It is basically a wrapper for:
- karriereat/json-decoder
- justinrainbow/json-schema With a little syntatic sugar and exceptions, even on PHP < 7.3
composer require greenskies/json
To decode a json string simply pass the string to Json::Decode()
This will return a standard object
$jsonString = '{"good":true}';
$decoded = Json::Decode($jsonString);
// $decoded->good = true
$jsonString = '{"good":true}';
$options = [
Json::ASSOCIATIVE => true,
];
$decoded = Json::Decode($jsonString, $options);
// $decoded['good'] = true
$jsonString = '{"processRefund": "true", "refundAmount": "17"}'
$schema = (object) [
"type"=>"object",
"properties"=>(object)[
"processRefund"=>(object)[
"type"=>"boolean"
],
"refundAmount"=>(object)[
"type"=>"number"
]
]
];
$options = [
Json::VALIDATOR => [
Json::JSON_SCHEMA => $schema,
Json::CONSTRAINTS => Constraint::CHECK_MODE_COERCE_TYPES,
],
];
$decoded = Json::Decode($jsonString, $options);
For further instructions visit justinrainbow/json-schema
$jsonString = '{"id": 1, "name": "John Doe"}';
$options = [
Json::DECODER => [
Json::CLASS_NAME => Person::class,
],
];
$decoded = Json::Decode($jsonString, $options);
$jsonString = '[{"id": 1, "name": "John Doe"}, {"id": 2, "name": "Jane Doe"}]';
$options = [
Json::DECODER => [
Json::CLASS_NAME => Person::class,
Json::DECODE_MULTIPLE => true,
],
];
$personArray = Json::Decode($jsonString, $options);
For further instructions visit karriereat/json-decoder