From 7162a3f781c7f39e7e9479b5663e72b2487e6c6d Mon Sep 17 00:00:00 2001 From: Julian Gojani Date: Sun, 31 Jul 2022 17:57:02 +0200 Subject: [PATCH] Fields can now be nullable --- composer.json | 2 +- src/main/de/interaapps/jsonplus/JSONPlus.php | 5 ++--- .../jsonplus/typemapper/ObjectTypeMapper.php | 4 ++-- src/test/testbootstrap.php | 15 +++++++++------ uppm.json | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 1a2031d..eb46ba3 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "interaapps/jsonplus", - "version": "1.0.5", + "version": "1.0.6", "type": "library", "authors": [ { diff --git a/src/main/de/interaapps/jsonplus/JSONPlus.php b/src/main/de/interaapps/jsonplus/JSONPlus.php index 4d3cfcc..add3a5e 100644 --- a/src/main/de/interaapps/jsonplus/JSONPlus.php +++ b/src/main/de/interaapps/jsonplus/JSONPlus.php @@ -10,7 +10,6 @@ use de\interaapps\jsonplus\typemapper\PassThroughTypeMapper; use de\interaapps\jsonplus\typemapper\StdClassObjectTypeMapper; use de\interaapps\jsonplus\typemapper\TypeMapper; -use ReflectionClass; class JSONPlus { private bool $prettyPrinting = false; @@ -53,7 +52,7 @@ public function fromJson(string $json, string|null $type = null){ * @template T * @param string $json The input json * @param class-string $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically) - * @return array + * @return T[] * */ public function fromMappedArrayJson(string $json, string $type) : array { return $this->mapTypedArray($this->serializationAdapter->fromJson($json), $type); @@ -62,7 +61,7 @@ public function fromMappedArrayJson(string $json, string $type) : array { * @template T * @param array $arr * @param class-string $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically) - * @return array + * @return T[] * */ public function mapTypedArray(array $arr, string $type) : array { $out = []; diff --git a/src/main/de/interaapps/jsonplus/typemapper/ObjectTypeMapper.php b/src/main/de/interaapps/jsonplus/typemapper/ObjectTypeMapper.php index f0eb983..46c3b30 100644 --- a/src/main/de/interaapps/jsonplus/typemapper/ObjectTypeMapper.php +++ b/src/main/de/interaapps/jsonplus/typemapper/ObjectTypeMapper.php @@ -27,7 +27,7 @@ public function map(mixed $o, string $type): mixed { foreach ($class->getProperties() as $property) { if (!$property->isStatic()) { - $name = $property?->getName(); + $name = $property->getName(); $serializeAttribs = $property->getAttributes(Serialize::class); foreach ($serializeAttribs as $attrib) { $attrib = $attrib->newInstance(); @@ -46,7 +46,7 @@ public function map(mixed $o, string $type): mixed { } } - $property->setValue($oo, $this->jsonPlus->map($o?->{$name}, strval($property->getType()))); + $property->setValue($oo, $this->jsonPlus->map($o?->{$name}, strval($property->getType()?->getName()))); } } } diff --git a/src/test/testbootstrap.php b/src/test/testbootstrap.php index 504e43c..bc2c1d0 100644 --- a/src/test/testbootstrap.php +++ b/src/test/testbootstrap.php @@ -6,7 +6,6 @@ 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; chdir(".");; ini_set('display_errors', 1); @@ -33,10 +32,11 @@ class Test { use JSONModel; #[Serialize("name_")] public string $name = "NOT INITIALIZED"; + public bool $test; public int $feef; public array $aeef; - public object $aeef2; + public ?object $aeef2; public Test2 $test2; public $aaaa; public $aa; @@ -64,7 +64,7 @@ public function setName(string $name): Test { "aa": false }'; -echo Test::fromJson(JSON)->toJson(); +echo "Hello ".Test::fromJson(JSON)->toJson(); $json = new JSONPlus(new JsonSerializationAdapter()); $var = $json->fromJson(JSON, Test::class); echo $var->myEnum; @@ -81,6 +81,7 @@ class Test3 { #[ArrayType(Test2::class)] public array $myArray; } + $arr = $json->fromMappedArrayJson('[ { "shush": "yipi" @@ -90,8 +91,10 @@ class Test3 { } ]', Test2::class); - - foreach ($arr as $val) { echo $val->sheesh; -} \ No newline at end of file +} + + +echo "\n\n"; +var_dump($json->toJson((object)["test" => 314])); \ No newline at end of file diff --git a/uppm.json b/uppm.json index bbf2de2..c243634 100644 --- a/uppm.json +++ b/uppm.json @@ -1,6 +1,6 @@ { "name": "interaapps/jsonplus", - "version": "1.0.5", + "version": "1.0.6", "phpVersion": ">8.1", "repositories": [], "run": {