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; |
10
|
|
|
|
11
|
|
|
use Tebru\Gson\Context\ReaderContext; |
12
|
|
|
use Tebru\Gson\Context\WriterContext; |
13
|
|
|
use Tebru\Gson\Internal\ObjectConstructor\CreateFromInstance; |
14
|
|
|
use Tebru\Gson\Internal\ObjectConstructorAware; |
15
|
|
|
use Tebru\Gson\Internal\TypeAdapterProvider; |
16
|
|
|
use Tebru\PhpType\TypeToken; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Gson |
20
|
|
|
* |
21
|
|
|
* @author Nate Brunette <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Gson |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* A service to fetch the correct [@see TypeAdapter] for a given type |
27
|
|
|
* |
28
|
|
|
* @var TypeAdapterProvider |
29
|
|
|
*/ |
30
|
|
|
private $typeAdapterProvider; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ReaderContext |
34
|
|
|
*/ |
35
|
|
|
private $readerContext; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var WriterContext |
39
|
|
|
*/ |
40
|
|
|
private $writerContext; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor |
44
|
|
|
* |
45
|
|
|
* @param TypeAdapterProvider $typeAdapterProvider |
46
|
|
|
* @param ReaderContext $readerContext |
47
|
|
|
* @param WriterContext $writerContext |
48
|
|
|
*/ |
49
|
47 |
|
public function __construct( |
50
|
|
|
TypeAdapterProvider $typeAdapterProvider, |
51
|
|
|
ReaderContext $readerContext, |
52
|
|
|
WriterContext $writerContext |
53
|
|
|
) { |
54
|
47 |
|
$this->typeAdapterProvider = $typeAdapterProvider; |
55
|
47 |
|
$this->readerContext = $readerContext; |
56
|
47 |
|
$this->writerContext = $writerContext; |
57
|
47 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new builder object |
61
|
|
|
* |
62
|
|
|
* @return GsonBuilder |
63
|
|
|
*/ |
64
|
52 |
|
public static function builder(): GsonBuilder |
65
|
|
|
{ |
66
|
52 |
|
return new GsonBuilder(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convenience method to convert an object to normalized data |
71
|
|
|
* |
72
|
|
|
* Optionally accepts a type to force serialization to |
73
|
|
|
* |
74
|
|
|
* @param mixed $object |
75
|
|
|
* @param null|string $type |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
19 |
|
public function toNormalized($object, ?string $type = null) |
79
|
|
|
{ |
80
|
19 |
|
if (is_scalar($object) && !$this->writerContext->enableScalarAdapters()) { |
81
|
1 |
|
return $object; |
82
|
|
|
} |
83
|
|
|
|
84
|
18 |
|
$typeToken = $type === null ? TypeToken::createFromVariable($object) : TypeToken::create($type); |
85
|
18 |
|
$typeAdapter = $this->typeAdapterProvider->getAdapter($typeToken); |
86
|
|
|
|
87
|
18 |
|
return $typeAdapter->write($object, $this->writerContext); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Converts an object to a json string |
92
|
|
|
* |
93
|
|
|
* Optionally accepts a type to force serialization to |
94
|
|
|
* |
95
|
|
|
* @param mixed $object |
96
|
|
|
* @param null|string $type |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
13 |
|
public function toJson($object, ?string $type = null): string |
100
|
|
|
{ |
101
|
13 |
|
return json_encode($this->toNormalized($object, $type)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Converts a normalized data to a valid json type |
106
|
|
|
* |
107
|
|
|
* @param mixed $decodedJson |
108
|
|
|
* @param object|string $type |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
27 |
|
public function fromNormalized($decodedJson, $type) |
112
|
|
|
{ |
113
|
27 |
|
if (is_scalar($decodedJson) && !$this->readerContext->enableScalarAdapters()) { |
114
|
1 |
|
return $decodedJson; |
115
|
|
|
} |
116
|
|
|
|
117
|
26 |
|
$isObject = is_object($type); |
118
|
26 |
|
$typeToken = $isObject ? TypeToken::create(get_class($type)) : TypeToken::create($type); |
119
|
26 |
|
$typeAdapter = $this->typeAdapterProvider->getAdapter($typeToken); |
120
|
26 |
|
$this->readerContext->setUsesExistingObject($isObject); |
121
|
26 |
|
$this->readerContext->setPayload($decodedJson); |
122
|
|
|
|
123
|
26 |
|
if ($isObject && $typeAdapter instanceof ObjectConstructorAware) { |
124
|
2 |
|
$typeAdapter->setObjectConstructor(new CreateFromInstance($type)); |
125
|
|
|
} |
126
|
|
|
|
127
|
26 |
|
return $typeAdapter->read($decodedJson, $this->readerContext); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Converts a json string to a valid json type |
132
|
|
|
* |
133
|
|
|
* @param string $json |
134
|
|
|
* @param object|string $type |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
26 |
|
public function fromJson(string $json, $type) |
138
|
|
|
{ |
139
|
26 |
|
return $this->fromNormalized(json_decode($json, true), $type); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|