Download the PHP package dotink/json without Composer
On this page you can find all versions of the php package dotink/json. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package json
Think of this library as a way to move your jsonSerialize()
method to a separate class that can be dependency injected, access protected/private properties and methods on their parent classes, and more.
Installation
Usage
The above function will prepare your $data
using available normalizers registered in the Json\Normalizer
namespace (by default). For example, if your $data
is an array it will use the Json\Normalizer\_Array
normalizer which will, in turn, normalize all the elements of the array.
Object Normalization
Normalizing objects operates quite a bit differently.
Which normalizer is used depends on whether or not there is a corresponding normalizer available in the Json\Normalizer
namespace (by default). If no corresponding normalizer can be found, it will revert to Json\Normalizer\_Object
which will normalize all public properties.
For example, included in this library is a Json\Normalizer\DateTime
class:
So if your object is of the class DateTime
, it will use that normalizer.
Compare, for example:
Which outputs:
To:
Which outputs:
Adding Normalizers
To add a custom object normalizer, simply create a new normalizer whose full class name (includes namespace) is prefixed by Json\Normalizer
. If you want to normalize My\Library\Acme
you would create Json\Normalizer\My\Library\Acme
:
What If I Don't Want to Use This Libraries Existing Normalizers?
You can change the namespace in which normalizers are looked for:
What If I Need Additional Dependencies to Normalize My Objects?
You can register any PSR-11 compatible container to resolve/construct your normalizers:
What If I Need To Normalize My Object Differently If It's Nested In Another Object?
You can determine whether or not the normalizer is nested using $this('nested')
:
What If I Need To Access Protected/Private Properties/Methods When Normalizing My Objects?
Normalizers proxy all instance property/method calls to the underlying object and will use reflection to access the data/method if it's not accessible, so calling $this->protectedProperty
or $this->privateMethod()
will work as if the normalizer's jsonSerialize()
method was on the class itself.
Will Child Classes Be Using the Parent's Normalizer?
Yes. Using the previous example My\Library\AcmeChild
which extends My\Library\Acme
would use Json\Normalizer\My\Library\Acme
unless there was a Json\Normalizer\My\Library\AcmeChild
.
Will My Existing JsonSerializable
Objects Be Normalized?
Yes. If your object implements JsonSerializable
there is a Json\Normalizer\JsonSerializable
normalizer which will normalize the return value of that method.
What If I Need to Normalize Objects With the Standard json_encode()
?
You can add custom normalization for objects regardless of whether or not they are encoded via Json\Serialize()
or json_encode()
by using the Json\Serialize
trait:
NOTE:
Json\Serializable
is just a concrete stand-in interface for PHP's built-inJsonSerializable
. If your object already implementsJsonSerializable
move the existingjsonSerialize()
method to a custom normalizer.
What If I Just Want to Normalize All Accessible Properties?
You can use Json\SerializeAllProperties
on your class instead of Json\Serialize
:
What If I Have Super-Secret Properties That Shouldn't Be Normalized... Starting with _
?
You can use Json\SerializeStandardProperties
on your class instead of Json\SerializeAllProperties
:
What If I Want To Normalize All Strings As "I'm a teapot?"
You can add the following class and make it autoloadable:
What If I Want To Serialize To XML?
GTFO.
Caveats
Normalization works by implementing JsonSerializable
and wrapping data in normalizers that implement it. Since it is not possible to modify an built-in PHP class such as DateTime
to directly implement JsonSerializable
then encoding built-in PHP objects can produce different results using Json\Serialize
and json_encode()
.
It is, of course, possible to extend these objects and use Json\Serialize
, but you will need to replace instantiation of such with the child class:
Why Does This Exist?
The traditional PHP json_encode()
function is capable of providing classes/objects a mechanism with which they can normalize their data to be serialized. The mechanism that does this is the JsonSerializable
interface and the corresponding jsonSerialize()
method. While this approach solves many basic cases of normalization, it does not solve many more advanced cases.
This library was written to solve a specific case wherein:
- I needed a way to have standard
json_encode
serialize objects. - What data was serialized for those objects depended on if they were nested or not.
- The normalization of data needed to be performed with additional dependencies which could not be injected into the objects, and probably shouldn't be just for the sake of their self-normalization.
Why Not Use a Totally Separate Serialization Library?
While there are many great serialization libraries available, getting a serialization library into every place where json_encode
might be used currently is not a realistic option. Additionally, without any sort of standard serialization interface, other libraries will likely still continue to use json_encode()
. See point #1.