1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Tebru\Gson\TypeAdapter; |
10
|
|
|
|
11
|
|
|
use LogicException; |
12
|
|
|
use Tebru\Gson\Element\JsonArray; |
13
|
|
|
use Tebru\Gson\Element\JsonElement; |
14
|
|
|
use Tebru\Gson\Element\JsonNull; |
15
|
|
|
use Tebru\Gson\Element\JsonObject; |
16
|
|
|
use Tebru\Gson\Element\JsonPrimitive; |
17
|
|
|
use Tebru\Gson\JsonReadable; |
18
|
|
|
use Tebru\Gson\JsonToken; |
19
|
|
|
use Tebru\Gson\JsonWritable; |
20
|
|
|
use Tebru\Gson\TypeAdapter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class JsonElementTypeAdapter |
24
|
|
|
* |
25
|
|
|
* @author Nate Brunette <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class JsonElementTypeAdapter extends TypeAdapter |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Read the next value, convert it to its type and return it |
31
|
|
|
* |
32
|
|
|
* @param JsonReadable $reader |
33
|
|
|
* @return JsonElement |
34
|
|
|
* @throws \LogicException |
35
|
|
|
*/ |
36
|
14 |
|
public function read(JsonReadable $reader): JsonElement |
37
|
|
|
{ |
38
|
14 |
|
switch ($reader->peek()) { |
39
|
14 |
|
case JsonToken::BEGIN_OBJECT: |
40
|
5 |
|
$object = new JsonObject(); |
41
|
5 |
|
$reader->beginObject(); |
42
|
5 |
|
while ($reader->hasNext()) { |
43
|
4 |
|
$name = $reader->nextName(); |
44
|
4 |
|
$object->add($name, $this->read($reader)); |
45
|
|
|
} |
46
|
5 |
|
$reader->endObject(); |
47
|
|
|
|
48
|
5 |
|
return $object; |
49
|
13 |
|
case JsonToken::BEGIN_ARRAY: |
50
|
2 |
|
$array = new JsonArray(); |
51
|
2 |
|
$reader->beginArray(); |
52
|
2 |
|
while ($reader->hasNext()) { |
53
|
1 |
|
$array->addJsonElement($this->read($reader)); |
54
|
|
|
} |
55
|
2 |
|
$reader->endArray(); |
56
|
|
|
|
57
|
2 |
|
return $array; |
58
|
12 |
|
case JsonToken::STRING: |
59
|
5 |
|
return JsonPrimitive::create($reader->nextString()); |
60
|
7 |
|
case JsonToken::NUMBER: |
61
|
3 |
|
return JsonPrimitive::create($reader->nextDouble()); |
62
|
4 |
|
case JsonToken::BOOLEAN: |
63
|
2 |
|
return JsonPrimitive::create($reader->nextBoolean()); |
64
|
2 |
|
case JsonToken::NULL: |
65
|
1 |
|
$reader->nextNull(); |
66
|
|
|
|
67
|
1 |
|
return new JsonNull(); |
68
|
|
|
default: |
69
|
1 |
|
throw new LogicException(\sprintf('Could not handle token "%s" at "%s"', $reader->peek(), $reader->getPath())); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Write the value to the writer for the type |
75
|
|
|
* |
76
|
|
|
* @param JsonWritable $writer |
77
|
|
|
* @param JsonElement $value |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
10 |
|
public function write(JsonWritable $writer, $value): void |
81
|
|
|
{ |
82
|
10 |
|
if (null === $value || $value->isJsonNull()) { |
83
|
3 |
|
$writer->writeNull(); |
84
|
|
|
|
85
|
3 |
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
if ($value->isJsonObject()) { |
89
|
2 |
|
$writer->beginObject(); |
90
|
2 |
|
foreach ($value->asJsonObject() as $key => $element) { |
91
|
2 |
|
$writer->name($key); |
92
|
2 |
|
$this->write($writer, $element); |
93
|
|
|
} |
94
|
2 |
|
$writer->endObject(); |
95
|
|
|
|
96
|
2 |
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
8 |
|
if ($value->isJsonArray()) { |
100
|
2 |
|
$writer->beginArray(); |
101
|
2 |
|
foreach ($value->asJsonArray() as $element) { |
102
|
2 |
|
$this->write($writer, $element); |
103
|
|
|
} |
104
|
2 |
|
$writer->endArray(); |
105
|
|
|
|
106
|
2 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
8 |
|
if ($value->isInteger()) { |
110
|
1 |
|
$writer->writeInteger($value->asInteger()); |
111
|
|
|
|
112
|
1 |
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
7 |
|
if ($value->isFloat()) { |
116
|
1 |
|
$writer->writeFloat($value->asFloat()); |
117
|
|
|
|
118
|
1 |
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
if ($value->isBoolean()) { |
122
|
2 |
|
$writer->writeBoolean($value->asBoolean()); |
123
|
|
|
|
124
|
2 |
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
$writer->writeString($value->asString()); |
128
|
4 |
|
} |
129
|
|
|
} |
130
|
|
|
|