Download the PHP package denisyfrolov/json-serializer without Composer
On this page you can find all versions of the php package denisyfrolov/json-serializer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download denisyfrolov/json-serializer
More information about denisyfrolov/json-serializer
Files in denisyfrolov/json-serializer
Package json-serializer
Short Description Simple PHP serializer for any objects to JSON.
License MIT
Informations about the package json-serializer
Simple PHP serializer for any objects to JSON.
- Installation
- Introduction
- Features
- Serialization
- Example
- Quality
- Contribute
- Author
Installation
Use Composer to install the package:
Introduction
What is serialization?
In the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment.
Features
This is a very simple class which allows you to serialize any child objects to JSON.
- Recursively serializes all the properties you allow to serialize, including multidimensional arrays of objects of other classes.
- Uses annotations to customize the scheme for serialized objects and to mark which classes and properties should be serialized.
- Allows you to customize JSON's field names.
- Allows you to exclude any property of objects from serialization.
- Allows you to prevent serialization of any properties, classes of which should not be serialized.
Serialization
Make your class to be inherited from the serializator class JsonSerializableObject
:
Mark your class as serializable using the flag @JsonSerializable
in an annotations area of the class:
If your class uses different classes as types for properties you want to serialize, mark all of these classes as serializable too. Otherwise the exception will be thrown: Class ClassName is not marked as serializable
.
Make getters for all the properties you want to serialize. Also make the properties publicly accessible:
By default the serializer uses property names as field names in JSON's schema. If you want to customize the names in the schema, use @JsonPropertyName property_name
keyword in an annotations area of the property:
To prevent any properties from serialization, mark the properties as Non Serializable using keyword @JsonPropertyNonSerializable
in an annotations area of the property:
Call jsonSerialize()
method to serialize your object to JSON format.
Output
Example
You can find this example with all the classes in example
folder of the project.
Code
Output
Quality
To run the PHPUnit tests at the command line, go to the project's root directory and issue phpunit
.
This library attempts to comply with PSR-4.
If you notice compliance oversights, please send a patch via pull request.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Author
License
The code base is licensed under the MIT license.