All Projects → karriereat → Json Decoder

karriereat / Json Decoder

Licence: other
JsonDecoder implementation that allows you to convert your JSON data into PHP class objects

Projects that are alternatives of or similar to Json Decoder

Swiftyjsonaccelerator
macOS app to generate Swift 5 code for models from JSON (with Codeable)
Stars: ✭ 864 (+692.66%)
Mutual labels:  hacktoberfest, json
Json Api Dart
JSON:API client for Dart/Flutter
Stars: ✭ 53 (-51.38%)
Mutual labels:  hacktoberfest, json
Jsonpath Rs
JSONPath for Rust
Stars: ✭ 31 (-71.56%)
Mutual labels:  json, json-data
Jsonq
A PHP query builder for JSON
Stars: ✭ 729 (+568.81%)
Mutual labels:  json, json-data
Lychee
The most complete and powerful data-binding library and persistence infra for Kotlin 1.3, Android & Splitties Views DSL, JavaFX & TornadoFX, JSON, JDBC & SQLite, SharedPreferences.
Stars: ✭ 102 (-6.42%)
Mutual labels:  hacktoberfest, json
Jsonlite
A simple, self-contained, serverless, zero-configuration, json document store.
Stars: ✭ 819 (+651.38%)
Mutual labels:  json, json-data
Univalue
High performance RAII C++ JSON library and universal value object class
Stars: ✭ 46 (-57.8%)
Mutual labels:  json, json-data
Simd Json
Rust port of simdjson
Stars: ✭ 499 (+357.8%)
Mutual labels:  hacktoberfest, json
Jquery Jsontotable
This plugin can convert JSON data type to table for html. JSON is used primarily to transmit data between a server and web application, as an alternative to XML. In these reasons todays many applications use json data format for data transferring. And you need json to table converter for html display.
Stars: ✭ 69 (-36.7%)
Mutual labels:  json, json-data
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (-38.53%)
Mutual labels:  json, json-data
Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (+485.32%)
Mutual labels:  hacktoberfest, json
Json Node Normalizer
'json-node-normalizer' - NodeJS module that normalize json data types from json schema specifications.
Stars: ✭ 105 (-3.67%)
Mutual labels:  json, json-data
Stash
An organizer for your porn, written in Go
Stars: ✭ 591 (+442.2%)
Mutual labels:  hacktoberfest, json
Jackson Module Kotlin
Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Stars: ✭ 830 (+661.47%)
Mutual labels:  hacktoberfest, json
Ems
Extended Memory Semantics - Persistent shared object memory and parallelism for Node.js and Python
Stars: ✭ 552 (+406.42%)
Mutual labels:  json, json-data
Chinese Xinhua
📙 中华新华字典数据库。包括歇后语,成语,词语,汉字。
Stars: ✭ 8,705 (+7886.24%)
Mutual labels:  json, json-data
Superjson
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
Stars: ✭ 446 (+309.17%)
Mutual labels:  hacktoberfest, json
Json Viewer
A JSON viewer plugin for Notepad++. Displays the selected JSON string in a tree view.
Stars: ✭ 464 (+325.69%)
Mutual labels:  hacktoberfest, json
Lazyjson
A very fast, very lazy JSON parser for Java.
Stars: ✭ 55 (-49.54%)
Mutual labels:  json, json-data
Circe Yaml
YAML parser for circe using SnakeYAML
Stars: ✭ 102 (-6.42%)
Mutual labels:  hacktoberfest, json

   

JsonDecoder for PHP

This package contains a JsonDecoder implementation that allows you to convert your JSON data into php class objects other than stdclass.

Installation

You can install the package via composer

composer require karriere/json-decoder

Usage

By default the Decoder will iterate over all JSON fields defined and will try to set this values on the given class type instance. This change in behavior allows the use of json-decoder on classes that use the magic __get and __set functions like Laravel's Eloquent models.

If a property equally named like the JSON field is found or a explicit Binding is defined for the JSON field it will be decoded into the defined place. Otherwise the property will just be created and assigned.

The JsonDecoder class can receive one parameter called shouldAutoCase. If set to true it will try to find the camel-case version from either snake-case or kebap-case automatically if no other binding was registered for the field and it will use an AliasBinding if one of the variants can be found.

A simple example

Assume you have a class Person that looks like this:

class Person
{
    public $id;
    public $name;
}

The following code will transform the given JSON data into an instance of Person.

$jsonDecoder = new JsonDecoder();
$jsonData = '{"id": 1, "name": "John Doe"}';

$person = $jsonDecoder->decode($jsonData, Person::class);

More complex use case

Let's extend the previous example with a property called address. This address field should contain an instance of Address. In the prior versions of json-decoder it was necessary to define a custom Transformer to be able to handle this situation. As of version 4 you can use the introduced method scanAndRegister to automatically generate the transformer based on class annotations.

class Person
{
    public $id;
    public $name;

    /**
     * @var Address
     */
    public $address;
}

For this class definition we can decode JSON data as follows:

$jsonDecoder = new JsonDecoder();
$jsonDecoder->scanAndRegister(Person::class);

$jsonData = '{"id": 1, "name": "John Doe", "address": {"street": "Samplestreet", "city": "Samplecity"}}';

$person = $jsonDecoder->decode($jsonData, Person::class);

Defining a Transformer

If you don't use annotations or need a more flexible Transformer you can also create a custom transformer. Let's look at the previous example without annotation.

class Person
{
    public $id;
    public $name;
    public $address;
}

To be able to transform the address data into an Address class object you need to define a transformer for Person:

The transformer interface defines two methods:

  • register: here you register your field, array, alias and callback bindings
  • transforms: gives you the full qualified class name e.g.: Your\Namespace\Class
class PersonTransformer implements Transformer
{
    public function register(ClassBindings $classBindings)
    {
        $classBindings->register(new FieldBinding('address', 'address', Address::class));
    }

    public function transforms()
    {
        return Person::class;
    }
}

After registering the transformer the JsonDecoder will use the defined transformer:

$jsonDecoder = new JsonDecoder();
$jsonDecoder->register(new PersonTransformer());

$jsonData = '{"id": 1, "name": "John Doe"}';

$person = $jsonDecoder->decode($jsonData, Person::class);

Handling private and protected properties

As of version 4 the JsonDecoder class will handle private and protected properties out of the box.

Transforming an array of elements

If your JSON contains an array of elements at the root level you can use the decodeMultiple method to transform the JSON data into an array of class type objects.

$jsonDecoder = new JsonDecoder();

$jsonData = '[{"id": 1, "name": "John Doe"}, {"id": 2, "name": "Jane Doe"}]';

$personArray = $jsonDecoder->decodeMultiple($jsonData, Person::class);

Documentation

Transformer Bindings

The following Binding implementations are available

FieldBinding

Defines a JSON field to property binding for the given type.

Signature:

new FieldBinding($property, $jsonField, $type);

This defines a field mapping for the property $property to a class instance of type $type with data in $jsonField.

ArrayBinding

Defines a array field binding for the given type.

Signature:

new ArrayBinding($property, $jsonField, $type);

This defines a field mapping for the property $property to an array of class instance of type $type with data in $jsonField.

AliasBinding

Defines a JSON field to property binding.

Signature:

new AliasBinding($property, $jsonField);

DateTimeBinding

Defines a JSON field to property binding and converts the given string to a DateTime instance.

Signature:

new new DateTimeBinding($property, $jsonField, $isRequired = false, $dateTimeFormat = DateTime::ATOM);

CallbackBinding

Defines a property binding that gets the callback result set as its value.

Signature:

new CallbackBinding($property, $callback);

License

Apache License 2.0 Please see LICENSE for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].