You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package contains a JsonDecoder implementation that allows you to convert your JSON data into php class objects other than `stdclass`.
11
10
12
11
## Installation
12
+
13
13
You can install the package via composer
14
+
14
15
```
15
16
composer require karriere/json-decoder
16
17
```
17
18
18
19
## Usage
19
-
By default all public properties of the class will be inspected. For all properties that have a JSON key with the same name the according value will be set.
20
+
21
+
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.
22
+
23
+
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.
24
+
25
+
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.
20
26
21
27
### A simple example
28
+
22
29
Assume you have a class `Person` that looks like this:
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.
51
+
52
+
```php
53
+
class Person
54
+
{
55
+
public $id;
56
+
public $name;
57
+
58
+
/**
59
+
* @var Address
60
+
*/
61
+
public $address;
62
+
}
63
+
```
64
+
65
+
For this class definition we can decode JSON data as follows:
Let's extend the previous example with a property called address. This address field should contain an instance of `Address`.
77
+
78
+
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.
79
+
42
80
```php
43
81
class Person
44
82
{
@@ -52,8 +90,9 @@ To be able to transform the address data into an `Address` class object you need
52
90
53
91
The transformer interface defines two methods:
54
92
55
-
* register: here you register your field, array, alias and callback bindings
56
-
* transforms: gives you the full qualified class name e.g.: Your\Namespace\Class
93
+
- register: here you register your field, array, alias and callback bindings
94
+
- transforms: gives you the full qualified class name e.g.: Your\Namespace\Class
95
+
57
96
```php
58
97
class PersonTransformer implements Transformer
59
98
{
@@ -70,6 +109,7 @@ class PersonTransformer implements Transformer
70
109
```
71
110
72
111
After registering the transformer the `JsonDecoder` will use the defined transformer:
The `JsonDecoder` class accepts two boolean constructor parameters to enable the handling of private and protected properties.
84
123
85
-
To do so a so called `PropertyAccessor`will be installed and on property set the proxy will set the property to accessible, set the according value and then will set the property to not accessible again.
124
+
As of version 4 the `JsonDecoder` class will handle `private` and `protected` properties out of the box.
86
125
87
126
### Transforming an array of elements
127
+
88
128
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.
0 commit comments