s-mcdonald / jason
Jason (JSON) is a PHP library that allows you to serialize classes with attributes, build Json structures with a builder and much more.
Requires
- php: >=8.2
Requires (Dev)
- mockery/mockery: 1.5.1
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-01-03 14:41:09 UTC
README
Serialize a class using attributes.
Notice
This library is no longer supported or maintained. Efforts have now moved to https://github.com/s-mcdonald/json.
The new Json library will completely replace this and include Hydration.
It is still available but no fixes or updates will be posted.
Documentation
Usage
Using Attributes to Serialize/Encode
class User implements JsonSerializable { #[Property('userName')] public string $name; #[Property] public array $phoneNumbers; #[Property('creditCard')] public function getCreditCard(): int { $this->creditCard = $credit; } }
Now Serialize using the Json static
or the JsonSerializer
echo Json::serialize($user); // or use the JsonSerializer $serializer = new JsonSerializer(); echo $serializer->serialize($user); // Produces { "userName": "Foo", "phoneNumbers": [ "044455444", "244755465" ], "creditCard": 54454.5, }
JsonBuilder
echo Json::createJsonBuilder() ->addNumericProperty('id', 11) ->addStringProperty('title', "Perfume Oil") ->addNumericProperty('rating', 4.26) ->addNumericProperty('stock', 65) ->addObjectProperty( 'thumbnail', Json::createJsonBuilder() ->addStringProperty("url", "https://i.dummyjson.com/data/products/11/thumbnail.jpg") ->addStringProperty("title", "thumbnail.jpg") ) ->addArrayProperty("images", [ "https://i.dummyjson.com/data/products/11/1.jpg", "https://i.dummyjson.com/data/products/11/2.jpg" ]) ;
Will create the following
{ "id": 11, "title": "Perfume Oil", "rating": 4.26, "stock": 65, "thumbnail": { "url": "https://i.dummyjson.com/data/products/11/thumbnail.jpg", "title": "thumbnail.jpg" }, "images": [ "https://i.dummyjson.com/data/products/11/1.jpg", "https://i.dummyjson.com/data/products/11/2.jpg" ] }
JsonAsserter
JsonAsserter::assertStringIsValidJson('{"foo":"bar"}');
Json::createFromArray
$json = Json::createFromArray(["foo" => "baz"]); echo $json; // {"foo":"baz"} echo $json->toString(); // {"foo":"baz"} echo $json->toPretty(); // { // "foo":"baz" // }
Json::createFromUrl
$json = Json::createFromUrl("http://some-domain.com/json-endpoint.json"); echo $json->toString(); // {"foo":"baz"}
Json::createFromFile
$json = Json::createFromFile("/path/on/server.json"); echo $json->toPretty(); // { // "foo":"baz" // }
Json::createFromStringable
$json = Json::createFromStringable('{"foo":"baz"}'); echo $json->toString(); // {"foo":"baz"}
Json::mergeCombine
$json = Json::mergeCombine('{"foo":"baz"}', '{"buz":"qux"}'); echo $json->toString(); // {"foo":"baz","buz":"qux"}
Json::convertJsonToArray
$json = Json::convertJsonToArray('{"foo":"baz","buz":"qux"}'); echo $json->toString(); // ["foo" => "baz","buz" => "qux"}
Json::convertFromJsonToObject
$json = Json::convertFromJsonToObject('{"foo":"baz","buz":"qux"}'); echo $json->toString(); // instance of JsonSerializable::class with values
Alongside using the Json entity, there is also a separate Encoder/Decoder that you can use.
JsonEncoder
$encoded = (new JsonEncoder())->encode($someValue)->getBody();
JsonDecoder
$decoded = (new JsonDecoder())->encode($someValue)->getBody();
Recursive Iteration of Json
foreach($json->toIterator() as $value) { // will return all property values no matter how deep // and nested in the Json structure. }
Installation
Via Composer. Run the following command from your project's root.
composer require s-mcdonald/jason
Dependencies
- Php 8.0
License
Jason is licensed under the terms of the MIT License (See LICENSE file for details).