Download the PHP package anujrnair/php-json-marshaller without Composer
On this page you can find all versions of the php package anujrnair/php-json-marshaller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anujrnair/php-json-marshaller
More information about anujrnair/php-json-marshaller
Files in anujrnair/php-json-marshaller
Package php-json-marshaller
Short Description A library to marshall and unmarshall JSON string to populated PHP classes
License MIT
Informations about the package php-json-marshaller
PHP JSON Marshaller
A library to marshall and unmarshall JSON strings to populated PHP classes, driven by annotations.
We would like fine grain control over our marshalling and unmarshalling because:
- json_decode always returns a StdClass object. We want to be able to decode into our own classes.
- We can add expected type information about the data we are receiving/sending and validate it before using it.
- json_encode cannot be controlled on a property by property basis. This will allow us to do that.
The latest version can be found here: https://github.com/AnujRNair/php-json-marshaller.
Installation
$ git clone https://github.com/AnujRNair/php-json-marshaller.git
$ composer install
Usage
In your classes, add MarshallProperty annotations to your properties and methods to describe how JSON should be marshalled or unmarshalled.
The MarshallProperty annotation requires a name and type, otherwise an exception will be thrown.
You can do this on any public property or method.
Once you have configured your class, load an instance of the JSONMarshaller class, and call the marshall or unmarshall function:
The $user
variable should now be an instance of the \My\Example\User
class, and be populated with the id and name we passed in from the JSON string.
The $marshalled
variable will be a json encoded string holding data from the \My\Example\User
object instance.
Nesting Objects
You can also nest classes within classes for recursive marshalling and unmarshalling like so:
And then in your code:
Arrays of Objects
You can marshall and unmarshall arrays of scalars/objects by indicating so in the type of the MarshallProperty annotation:
Then in your code:
Unmarshalling into Objects with a Constructor
To unmarshall into an object with a constructor, with required params, use the @MarshallCreator annotation to describe to the marshaller which values to instantiate the class with.
This Annotation takes an array of @MarshallProperty objects like the following:
The marshaller will then look for these values in the json string passed to it (The same level on which the class was instantiated) and instantiate the object with these values.
Caching
You can cache decoded classes for performance boosts. This will not cache data from JSON strings.
To do so, instantiate an instance of the Cache
class, and pass in a storage type. Then pass this through to the ClassDecoder
instance:
See \PhpJsonMarshaller\Cache\Storage
for all storage types available. MemcachedStorage
has a few options you can set as well.
Unknown Entries
You can specifically allow a class to fail on any unknown json variables by adding a MarshallConfig annotation to a specific class:
Now if you try unmarshalling an unknown variable into this class, an exception will be thrown.
Tests
Testing is covered by PHPUnit. Browse to the root of the library and run phpunit
.
Please ensure you have the memcached daemon running to ensure the MemcachedStorageTest
tests pass.
License
Please see the License