Skip to content

Commit 7162a3f

Browse files
committed
Fields can now be nullable
1 parent f084a45 commit 7162a3f

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/jsonplus",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"type": "library",
55
"authors": [
66
{

src/main/de/interaapps/jsonplus/JSONPlus.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use de\interaapps\jsonplus\typemapper\PassThroughTypeMapper;
1111
use de\interaapps\jsonplus\typemapper\StdClassObjectTypeMapper;
1212
use de\interaapps\jsonplus\typemapper\TypeMapper;
13-
use ReflectionClass;
1413

1514
class JSONPlus {
1615
private bool $prettyPrinting = false;
@@ -53,7 +52,7 @@ public function fromJson(string $json, string|null $type = null){
5352
* @template T
5453
* @param string $json The input json
5554
* @param class-string<T> $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically)
56-
* @return array<T>
55+
* @return T[]
5756
* */
5857
public function fromMappedArrayJson(string $json, string $type) : array {
5958
return $this->mapTypedArray($this->serializationAdapter->fromJson($json), $type);
@@ -62,7 +61,7 @@ public function fromMappedArrayJson(string $json, string $type) : array {
6261
* @template T
6362
* @param array $arr
6463
* @param class-string<T> $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically)
65-
* @return array<T>
64+
* @return T[]
6665
* */
6766
public function mapTypedArray(array $arr, string $type) : array {
6867
$out = [];

src/main/de/interaapps/jsonplus/typemapper/ObjectTypeMapper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function map(mixed $o, string $type): mixed {
2727

2828
foreach ($class->getProperties() as $property) {
2929
if (!$property->isStatic()) {
30-
$name = $property?->getName();
30+
$name = $property->getName();
3131
$serializeAttribs = $property->getAttributes(Serialize::class);
3232
foreach ($serializeAttribs as $attrib) {
3333
$attrib = $attrib->newInstance();
@@ -46,7 +46,7 @@ public function map(mixed $o, string $type): mixed {
4646
}
4747
}
4848

49-
$property->setValue($oo, $this->jsonPlus->map($o?->{$name}, strval($property->getType())));
49+
$property->setValue($oo, $this->jsonPlus->map($o?->{$name}, strval($property->getType()?->getName())));
5050
}
5151
}
5252
}

src/test/testbootstrap.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use de\interaapps\jsonplus\JSONModel;
77
use de\interaapps\jsonplus\JSONPlus;
88
use de\interaapps\jsonplus\serializationadapter\impl\JsonSerializationAdapter;
9-
use de\interaapps\jsonplus\serializationadapter\impl\phpjson\PHPJsonSerializationAdapter;
109

1110
chdir(".");;
1211
ini_set('display_errors', 1);
@@ -33,10 +32,11 @@ class Test {
3332
use JSONModel;
3433
#[Serialize("name_")]
3534
public string $name = "NOT INITIALIZED";
35+
3636
public bool $test;
3737
public int $feef;
3838
public array $aeef;
39-
public object $aeef2;
39+
public ?object $aeef2;
4040
public Test2 $test2;
4141
public $aaaa;
4242
public $aa;
@@ -64,7 +64,7 @@ public function setName(string $name): Test {
6464
"aa": false
6565
}';
6666

67-
echo Test::fromJson(JSON)->toJson();
67+
echo "Hello ".Test::fromJson(JSON)->toJson();
6868
$json = new JSONPlus(new JsonSerializationAdapter());
6969
$var = $json->fromJson(JSON, Test::class);
7070
echo $var->myEnum;
@@ -81,6 +81,7 @@ class Test3 {
8181
#[ArrayType(Test2::class)]
8282
public array $myArray;
8383
}
84+
8485
$arr = $json->fromMappedArrayJson('[
8586
{
8687
"shush": "yipi"
@@ -90,8 +91,10 @@ class Test3 {
9091
}
9192
]', Test2::class);
9293

93-
94-
9594
foreach ($arr as $val) {
9695
echo $val->sheesh;
97-
}
96+
}
97+
98+
99+
echo "\n\n";
100+
var_dump($json->toJson((object)["test" => 314]));

uppm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/jsonplus",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"phpVersion": ">8.1",
55
"repositories": [],
66
"run": {

0 commit comments

Comments
 (0)