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
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.
21
21
22
-
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.
22
+
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 (you need the `#[AllowDynamicProperties]` attribute if you are on PHP 8.2.).
23
23
24
24
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.
25
25
@@ -28,36 +28,47 @@ The `JsonDecoder` class can receive one parameter called `shouldAutoCase`. If se
28
28
Assume you have a class `Person` that looks like this:
29
29
30
30
```php
31
+
#[AllowDynamicProperties]
31
32
class Person
32
33
{
33
-
public $id;
34
-
public $name;
34
+
public int $id;
35
+
public string $name;
36
+
public ?string $lastname = '';
35
37
}
36
38
```
37
39
38
40
The following code will transform the given JSON data into an instance of `Person`.
Please be aware that since PHP 8.2. dynamic properties are deprecated. So if you still wish to have the ability to make
50
+
use of those dynamic properties you have to add the PHP attribute `AllowDynamicProperties` to your class.
51
+
If you are using PHP 8.2. (and greater) and don't use the `AllowDynamicProperties` attribute all dynamic properties will
52
+
be ignored.
53
+
47
54
### More complex use case
48
55
49
-
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.
56
+
Let's extend the previous example with a property called address. This address field should contain an instance of `Address`.
57
+
As of version 4 you can use the introduced method `scanAndRegister` to automatically generate the transformer based on class annotations.
58
+
Since version 5 you can also make use of the property type instead of a class annotation.
50
59
51
60
```php
52
61
class Person
53
62
{
54
-
public $id;
55
-
public $name;
63
+
public int $id;
64
+
public string $name;
56
65
57
66
/**
58
67
* @var Address
59
68
*/
60
69
public $address;
70
+
71
+
public ?Address $typedAddress = null;
61
72
}
62
73
```
63
74
@@ -67,7 +78,7 @@ For this class definition we can decode JSON data as follows:
0 commit comments