File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change 15
15
class JsonObjectBuilder extends DataObjectBuilder {
16
16
public function __construct (
17
17
private readonly int $ depth = 512 ,
18
+ private readonly int $ flags = 0 ,
18
19
) {
19
20
}
20
21
@@ -23,7 +24,7 @@ public function fromJsonString(string $jsonString):JsonObject {
23
24
$ json = json_decode (
24
25
$ jsonString ,
25
26
depth: $ this ->depth ,
26
- flags: JSON_THROW_ON_ERROR
27
+ flags: JSON_THROW_ON_ERROR | $ this -> flags ,
27
28
);
28
29
}
29
30
catch (NativeJsonException $ exception ) {
@@ -39,9 +40,10 @@ public function fromJsonString(string $jsonString):JsonObject {
39
40
public function fromJsonDecoded (
40
41
object |array |string |int |float |bool |null $ jsonDecoded
41
42
):JsonObject {
43
+ /** @noinspection PhpDeprecatedStdLibCallInspection */
42
44
if (is_array ($ jsonDecoded )
43
45
&& !is_int (key ($ jsonDecoded ))) {
44
- // The JSON could represent an primitive indexed array, but the json could have
46
+ // The JSON could represent a primitive indexed array, but the json could have
45
47
// been decoded as an associative array too. Deal with associative arrays first.
46
48
$ jsonData = $ this ->fromJsonDecoded (
47
49
(object )$ jsonDecoded
Original file line number Diff line number Diff line change 11
11
use Gt \Json \JsonPrimitive \JsonNullPrimitive ;
12
12
use Gt \Json \JsonPrimitive \JsonStringPrimitive ;
13
13
use PHPUnit \Framework \TestCase ;
14
- use stdClass ;
15
14
16
15
class JsonObjectBuilderTest extends TestCase {
17
16
private string $ jsonStringSimpleKVP = <<<JSON
@@ -215,4 +214,18 @@ public function testFromJson_depth() {
215
214
self ::expectExceptionMessage ("Error decoding JSON: Maximum stack depth exceeded " );
216
215
$ sut ->fromJsonString ($ jsonString );
217
216
}
217
+
218
+ public function testFromJson_customFlag_bigInt () {
219
+ $ jsonString = '{"num": 9876543210987654321 } ' ;
220
+
221
+ $ sut = new JsonObjectBuilder ();
222
+ $ json = $ sut ->fromJsonString ($ jsonString );
223
+ $ num = $ json ->getString ("num " );
224
+ self ::assertStringContainsString ("E " , $ num );
225
+
226
+ $ sut = new JsonObjectBuilder (flags: JSON_BIGINT_AS_STRING );
227
+ $ json = $ sut ->fromJsonString ($ jsonString );
228
+ $ num = $ json ->getString ("num " );
229
+ self ::assertSame ("9876543210987654321 " , $ num );
230
+ }
218
231
}
You can’t perform that action at this time.
0 commit comments