-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JsonSerializationAdapter.php (Custom JSON implementation) and more
- Loading branch information
1 parent
60ad5c7
commit 1fcefc4
Showing
7 changed files
with
244 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
namespace de\interaapps\jsonplus\parser; | ||
|
||
|
||
class JSONDecoder { | ||
private int $i = 0; | ||
public function __construct( | ||
private string $json | ||
) { | ||
} | ||
|
||
public function jumpEmpties(){ | ||
while ((trim($this->get()) == '' || $this->get()=="\n") && $this->i < strlen($this->json)) | ||
$this->i++; | ||
} | ||
|
||
public function get($off = 0) : string { | ||
if (strlen($this->json) > $this->i+$off && $this->i+$off >= 0) | ||
return $this->json[$this->i+$off]; | ||
return ""; | ||
} | ||
|
||
public function readNext() : mixed { | ||
$this->jumpEmpties(); | ||
|
||
if ($this->get() == '{') | ||
return $this->readObject(); | ||
if ($this->get() == '[') | ||
return $this->readArray(); | ||
if ($this->get() == '"') | ||
return $this->readString(); | ||
|
||
return $this->readPrimitive(); | ||
} | ||
public function readObject() : object|null { | ||
$o = (object)[]; | ||
$this->i++; | ||
for (; $this->i<strlen($this->json); $this->i++) { | ||
$this->jumpEmpties(); | ||
if ($this->get() == '}') { | ||
$this->i++; | ||
return $o; | ||
} | ||
|
||
$key = $this->readNext(); | ||
$this->jumpEmpties(); | ||
if ($this->get() == ":") | ||
$this->i++; | ||
$this->jumpEmpties(); | ||
$value = $this->readNext(); | ||
|
||
$o->{$key} = $value; | ||
|
||
$this->jumpEmpties(); | ||
if ($this->get() == '}') { | ||
$this->i++; | ||
return $o; | ||
} | ||
|
||
if ($this->get(1) == ",") | ||
$this->i++; | ||
} | ||
|
||
return null; | ||
} | ||
public function readArray() : array { | ||
$this->i++; | ||
$a = []; | ||
for (; $this->i<strlen($this->json); $this->i++) { | ||
$this->jumpEmpties(); | ||
if ($this->get() == ']') { | ||
$this->i++; | ||
return $a; | ||
} | ||
|
||
array_push($a, $this->readNext()); | ||
|
||
$this->jumpEmpties(); | ||
if ($this->get() == ']') { | ||
$this->i++; | ||
return $a; | ||
} | ||
|
||
if ($this->get(1) == ",") | ||
$this->i++; | ||
} | ||
return []; | ||
} | ||
|
||
public function readString() : string { | ||
$s = ""; | ||
$this->i++; | ||
for (; $this->i<strlen($this->json); $this->i++) { | ||
$char = $this->get(); | ||
|
||
if ($char == '"' && ($this->get(-1) != "\\" || $this->get(-2) == "\\")) { | ||
if ($this->get(-1) && $this->get(-2) != "\\") | ||
$this->i++; | ||
return $s; | ||
} | ||
$s .= $char; | ||
} | ||
return ""; | ||
} | ||
|
||
private function primitiveStringToPHP($p){ | ||
if ($p == "false") | ||
return false; | ||
if ($p == "true") | ||
return true; | ||
return (double) $p; | ||
} | ||
|
||
public function readPrimitive() : mixed { | ||
$p = ""; | ||
for (; $this->i<strlen($this->json); $this->i++) { | ||
$char = $this->get(); | ||
if ($char == '"' || $char == ',' || $char == '}' || $char == ']') | ||
return $this->primitiveStringToPHP($p); | ||
$p .= $char; | ||
} | ||
$this->i--; | ||
return $this->primitiveStringToPHP($p); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
|
||
namespace de\interaapps\jsonplus\parser; | ||
|
||
|
||
class JSONEncoder { | ||
private bool $prettyPrint = false; | ||
public function encode($v, $tabs="") : string { | ||
$this->prettyPrintNewLine = $this->prettyPrint ? "\n": ''; | ||
$identedTabs = $this->prettyPrint ? $tabs." " : ''; | ||
|
||
if (is_bool($v)) { | ||
return $v ? "true" : "false"; | ||
} else if (is_int($v) || is_bool($v) || is_double($v)) { | ||
return strval($v); | ||
} else if (is_array($v)) { | ||
if (count($v) == 0) | ||
return "[]"; | ||
return '['.$this->prettyPrintNewLine.implode(", ".$this->prettyPrintNewLine, array_map(fn($k)=>$identedTabs.$this->encode($k, $identedTabs), $v)).$this->prettyPrintNewLine.$tabs.']'; | ||
} else if (is_string($v)) { | ||
return '"'.$this->escapeString($v).'"'; | ||
} else if (is_object($v)) { | ||
$v = (array) $v; | ||
foreach ($v as $key=>$value) { | ||
if (!isset($value)) | ||
unset($v[$key]); | ||
} | ||
if (count($v) == 0) | ||
return "{}"; | ||
return '{'.$this->prettyPrintNewLine.implode(", ".$this->prettyPrintNewLine, array_map(fn($k, $val)=>$identedTabs.$this->encode($k, $identedTabs).': '.$this->encode($val, $identedTabs), array_keys($v), array_values($v))).$this->prettyPrintNewLine.$tabs.'}'; | ||
} | ||
return ""; | ||
} | ||
|
||
private function escapeString($str) : string { | ||
return | ||
str_replace("\n", "\\n", | ||
str_replace('"', '\"', | ||
str_replace("\\","\\\\", $str))); | ||
} | ||
|
||
public function setPrettyPrint(bool $prettyPrint): JSONEncoder { | ||
$this->prettyPrint = $prettyPrint; | ||
return $this; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/de/interaapps/jsonplus/serializationadapter/impl/JsonSerializationAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace de\interaapps\jsonplus\serializationadapter\impl; | ||
|
||
use de\interaapps\jsonplus\parser\JSONDecoder; | ||
use de\interaapps\jsonplus\parser\JSONEncoder; | ||
use de\interaapps\jsonplus\serializationadapter\SerializationAdapter; | ||
|
||
|
||
class JsonSerializationAdapter implements SerializationAdapter { | ||
public function fromJson($json){ | ||
return (new JSONDecoder($json))->readNext(); | ||
} | ||
|
||
public function toJson($v, bool $prettyPrint) { | ||
return (new JSONEncoder()) | ||
->setPrettyPrint($prettyPrint) | ||
->encode($v, ""); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/de/interaapps/jsonplus/typemapper/StdClassObjectTypeMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
namespace de\interaapps\jsonplus\typemapper; | ||
|
||
|
||
use de\interaapps\jsonplus\attributes\Serialize; | ||
use de\interaapps\jsonplus\JSONPlus; | ||
use ReflectionClass; | ||
|
||
class StdClassObjectTypeMapper implements TypeMapper { | ||
public function __construct( | ||
private JSONPlus $jsonPlus | ||
){ | ||
} | ||
|
||
public function map(mixed $o, string $type): mixed { | ||
/* Implement if CASING is implemented | ||
$oo = []; | ||
foreach ($o as $k=>$v) { | ||
echo $k; | ||
$oo[$k] = $v; | ||
}*/ | ||
|
||
return (object) $o; | ||
} | ||
|
||
public function mapToJson(mixed $o, string $type): mixed { | ||
return (object) $o; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters