|
2 | 2 |
|
3 | 3 | ## Getting started |
4 | 4 |
|
| 5 | +### JSONPlus instance |
5 | 6 | ```php |
6 | 7 | <?php |
| 8 | +use de\interaapps\jsonplus\JSONPlus; |
| 9 | +use de\interaapps\jsonplus\serializationadapter\impl\JsonSerializationAdapter; |
| 10 | +use de\interaapps\jsonplus\serializationadapter\impl\phpjson\PHPJsonSerializationAdapter; |
7 | 11 |
|
| 12 | +$jsonPlus = JSONPlus::createDefault(); |
| 13 | +$obj = $jsonPlus->fromJson('{"obj": "hello world"}'); |
| 14 | +echo $obj->string; // -> hello world |
| 15 | + |
| 16 | +// Enabling pretty printing |
| 17 | +$jsonPlus->setPrettyPrinting(true); |
| 18 | + |
| 19 | +echo $jsonPlus->toJson($obj); // -> {"obj": "hello world"} |
| 20 | + |
| 21 | +/// Other drivers |
| 22 | +// Default if json_decode exists in JSONPlus::createDefault() |
| 23 | +$jsonPlus = new JSONPlus(new PHPJsonSerializationAdapter()); |
| 24 | +// Custom JSON implementation |
| 25 | +$jsonPlus = new JSONPlus(new JsonSerializationAdapter()); |
| 26 | +``` |
| 27 | + |
| 28 | +### Model |
| 29 | +```php |
| 30 | +<?php |
| 31 | +use de\interaapps\jsonplus\JSONPlus; |
| 32 | +use de\interaapps\jsonplus\JSONModel; |
8 | 33 | use de\interaapps\jsonplus\attributes\Serialize; |
9 | | -use de\interaapps\jsonplus\JSONModel;use de\interaapps\jsonplus\JSONPlus;use de\interaapps\jsonplus\serializationadapter\impl\JsonSerializationAdapter;use de\interaapps\jsonplus\serializationadapter\impl\phpjson\PHPJsonSerializationAdapter; |
| 34 | +use de\interaapps\jsonplus\attributes\ArrayType; |
10 | 35 |
|
11 | | -class Test2 { |
12 | | - use JSONModel; |
13 | | - |
14 | | - #[Serialize("my_array")] |
15 | | - public array $myArray; |
| 36 | +class Organisation { |
| 37 | + public string $name; |
16 | 38 | } |
17 | 39 |
|
18 | | -class Test { |
19 | | - // Adds the Test#toJson and Test::fromJson functions |
| 40 | +class User { |
20 | 41 | use JSONModel; |
21 | 42 |
|
22 | | - public string $test; |
23 | | - public Test2 $test2; |
| 43 | + public int id; |
| 44 | + |
| 45 | + #[Serialize("firstName")] |
| 46 | + public string firstName; |
| 47 | + |
| 48 | + #[Serialize(hidden: true)] |
| 49 | + public string password; |
| 50 | + |
| 51 | + public ?Organisation $organisation; |
| 52 | + |
| 53 | + // Set Type for array: |
| 54 | + #[ArrayType(User::class)] |
| 55 | + public array $friends; |
24 | 56 | } |
25 | 57 |
|
26 | | -$test = Test::fromJson('{ |
27 | | - "test": "Hello World", |
28 | | - "test2": { |
29 | | - "my_array": ["Hello There"] |
30 | | - } |
31 | | -}'); |
| 58 | +$json = '{ |
| 59 | + "id": 12343, |
| 60 | + "first_name": "Jeff", |
| 61 | + "organisation": { |
| 62 | + "name": "InteraApps" |
| 63 | + }, |
| 64 | + "friends": [ |
| 65 | + { |
| 66 | + "id": 3245, |
| 67 | + "first_name": "John", |
| 68 | + "friends": [] |
| 69 | + } |
| 70 | + ] |
| 71 | +}'; |
| 72 | + |
| 73 | +// Decoding the JSON |
| 74 | +$user = User::fromJson($json); |
| 75 | + |
| 76 | +echo "Hello. My name is ".$user->first_name.", I have the ID ".$user->id |
| 77 | + ."and I'm in the organisation ".$user->organisation->name."\n"; |
| 78 | + |
| 79 | +foreach ($user->friends as $friend) { |
| 80 | + echo "One of my friends: ".$friend->name."\n"; |
| 81 | +} |
32 | 82 |
|
33 | | -echo $test->toJson(); |
| 83 | +// Encoding to JSON |
| 84 | +echo $user->toJson(); |
34 | 85 |
|
35 | | -// Custom JSONPlus Instance |
| 86 | +// Decode typed JSON-array |
36 | 87 | $jsonPlus = JSONPlus::createDefault(); |
37 | | -// $jsonPlus = new JSONPlus(new PHPJsonSerializationAdapter()); |
38 | | -$arrJson = $jsonPlus->toJson([ |
39 | | - "A", "B", "C" |
40 | | -]); |
41 | | -echo $arrJson; |
42 | | -// '["A", "B", "C"]' |
43 | | - |
44 | | -echo $jsonPlus->fromJson($arrJson)[0]; |
45 | | -// "A" |
46 | | - |
47 | | -// Setting JSONModal |
48 | | -Test::setJsonPlusInstance($jsonPlus); |
49 | | -// For all (Default instance) |
50 | | -JSONPlus::$default = $jsonPlus; |
51 | | - |
52 | | -/// Using other JSON-Drivers |
53 | | -// Uses PHPs default json_encode and json_decode methods |
54 | | -$jsonPlus = new JSONPlus(new PHPJsonSerializationAdapter()); |
55 | | -// Uses an custom implementation of JSON. This will be automatically chosen by JSONPlus::createDefault when json_decode doesn't exist |
56 | | -$jsonPlus = new JSONPlus(new JsonSerializationAdapter()); |
| 88 | +$users = $jsonPlus->fromMappedArrayJson('[...]', User::class); |
| 89 | +foreach ($users as $user) {} |
| 90 | +``` |
| 91 | +`Tip`: If you are using PHPStorm or any other intelligent IDE you might add PHP-Docs to some fields. |
| 92 | + |
| 93 | +For Typed Arrays: |
| 94 | +```php |
| 95 | +/** |
| 96 | +* @var array<User> |
| 97 | +*/ |
57 | 98 | ``` |
58 | 99 |
|
59 | 100 | ## Installation |
|
0 commit comments