Skip to content

Commit

Permalink
Fields can now be nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Jul 31, 2022
1 parent f084a45 commit 7162a3f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "interaapps/jsonplus",
"version": "1.0.5",
"version": "1.0.6",
"type": "library",
"authors": [
{
Expand Down
5 changes: 2 additions & 3 deletions src/main/de/interaapps/jsonplus/JSONPlus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +52,7 @@ public function fromJson(string $json, string|null $type = null){
* @template T
* @param string $json The input json
* @param class-string<T> $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically)
* @return array<T>
* @return T[]
* */
public function fromMappedArrayJson(string $json, string $type) : array {
return $this->mapTypedArray($this->serializationAdapter->fromJson($json), $type);
Expand All @@ -62,7 +61,7 @@ public function fromMappedArrayJson(string $json, string $type) : array {
* @template T
* @param array $arr
* @param class-string<T> $type A class (className::class), type (example: "array", "int"...) or null (Detects type automatically)
* @return array<T>
* @return T[]
* */
public function mapTypedArray(array $arr, string $type) : array {
$out = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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())));
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/test/testbootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -81,6 +81,7 @@ class Test3 {
#[ArrayType(Test2::class)]
public array $myArray;
}

$arr = $json->fromMappedArrayJson('[
{
"shush": "yipi"
Expand All @@ -90,8 +91,10 @@ class Test3 {
}
]', Test2::class);



foreach ($arr as $val) {
echo $val->sheesh;
}
}


echo "\n\n";
var_dump($json->toJson((object)["test" => 314]));
2 changes: 1 addition & 1 deletion uppm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "interaapps/jsonplus",
"version": "1.0.5",
"version": "1.0.6",
"phpVersion": ">8.1",
"repositories": [],
"run": {
Expand Down

0 comments on commit 7162a3f

Please sign in to comment.