tebru / gson-php
Gson for PHP: Convert PHP objects to and from json
Installs: 122 478
Dependents: 10
Suggesters: 0
Security: 0
Stars: 154
Watchers: 9
Forks: 18
Open Issues: 6
Requires
- php: >=7.1
- phpdocumentor/reflection-docblock: ^3.2.3|^4.0.1|^5
- psr/simple-cache: ^1.0
- symfony/cache: ^3.3|^4.0|^5.0|^6.0
- tebru/doctrine-annotation-reader: ^0.3.7
- tebru/php-type: ^0.1.7
Requires (Dev)
- phpunit/phpunit: ^7.0|^8.0
- dev-master
- v0.7.x-dev
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.7
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.6.0-beta1
- v0.5.9
- v0.5.8
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-caching
- dev-fix-reflection-type-to-string
- dev-fix-null-in-array
- dev-add-psr-simple-cache
- dev-class-metadata-visitor
- dev-allow-expose-from-class-exclude
- dev-fix-array-adapter
- dev-fix-docblock-parsing-for-array
- dev-update-type-token-factory
- dev-fix-type-resolution-order
- dev-add-decode-data-to-json-exception
- dev-split-exclusion-strategies
- dev-add-path-to-writer
- dev-performance-improvements-v2
- dev-test
This package is auto-updated.
Last update: 2024-12-06 22:14:49 UTC
README
This library is a port of google/gson written in PHP. Its purpose is to easily handle the conversion between PHP objects and JSON. It shares some of google/gson's goals such as:
- A simple interface using
toJson
andfromJson
methods - Enable serialization and deserialization of 3rd party classes
- Allow schema differences between PHP objects and JSON
And in addition:
- Utilize PHP 7 scalar type hints to be intelligent about property types
- Limit the number of annotations required to use
- Allow serialization decisions based on runtime information
Overview
Here are some simple use cases to get a feel for how the library works
If you have a PHP object you want to convert to JSON
// $object obtained elsewhere $gson = Gson::builder()->build(); $json = $gson->toJson($object);
What this is doing is using the provided GsonBuilder
to set up the
Gson
object with sensible defaults. Calling Gson::toJson
and
passing in an object will return a string of JSON.
If you need to, you can force the type Gson will use to serialize
// $object obtained elsewhere $gson = Gson::builder()->build(); $json = $gson->toJson($object, MyCustomClass::class);
The reverse is very similar
// $json obtained elsewhere $gson = Gson::builder()->build(); $fooObject = $gson->fromJson($json, Foo::class);
Now we call Gson::fromJson
and pass in the json as a string and the type
of object we'd like to map to. In this example, we will be getting
an instantiated Foo
object back.
Gson has a concept of "normalized" forms. This just means data that has
been decoded with json_decode
, or can be passed into json_encode
.
// $object obtained elsewhere $gson = Gson::builder()->build(); $jsonArray = $gson->toNormalized($object); $object = $gson->fromNormalized($jsonArray, Foo::class);
Documentation
- Customizing Serialization/Deserialization
- Excluding Classes and Properties
- Customizing Class Instantiation
- Types
- Annotation Reference
- Advanced Usage
- Performance Considerations
Installation
This library requires PHP 7.1
composer require tebru/gson-php
Be sure and set up the annotation loader in one of your initial scripts.
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
License
This project is licensed under the MIT license. Please see the LICENSE
file for more information.