Skip to content

Commit 5077a49

Browse files
committed
Added PHPDoc for generic functions
1 parent 883d2ab commit 5077a49

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/main/de/interaapps/jsonplus/parser/JSONDecoder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ public function readString() : string {
9393
for (; $this->i<strlen($this->json); $this->i++) {
9494
$char = $this->get();
9595

96+
foreach (['"'=>'"', "\\"=>"\\", "n"=>"\n", "r"=>"\r", "f"=>"\f", "t"=>"\t", "v"=>"\v", "/"=>"/"] as $k=>$v) {
97+
if ($char == "\\" && $this->get(1) == $k) {
98+
$char = $v;
99+
$this->i++;
100+
break;
101+
}
102+
}
103+
96104
if ($char == '"' && ($this->get(-1) != "\\" || $this->get(-2) == "\\")) {
97105
if ($this->get(-1) && $this->get(-2) != "\\")
98106
$this->i++;

src/main/de/interaapps/jsonplus/parser/JSONEncoder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ public function encode($v, $tabs="") : string {
3636
private function escapeString($str) : string {
3737
return
3838
str_replace("\n", "\\n",
39+
str_replace("\r", "\\r",
40+
str_replace("\t", "\\t",
41+
str_replace("/", "\\/",
42+
str_replace("\f", "\\f",
43+
str_replace("\v", "\\v",
3944
str_replace('"', '\"',
40-
str_replace("\\","\\\\", $str)));
45+
str_replace("\\","\\\\", $str))))))));
4146
}
4247

4348
public function setPrettyPrint(bool $prettyPrint): JSONEncoder {

src/test/testbootstrap.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use de\interaapps\jsonplus\JSONModel;
66
use de\interaapps\jsonplus\JSONPlus;
77
use de\interaapps\jsonplus\serializationadapter\impl\JsonSerializationAdapter;
8+
use de\interaapps\jsonplus\serializationadapter\impl\phpjson\PHPJsonSerializationAdapter;
89

910
chdir(".");;
1011
ini_set('display_errors', 1);
@@ -38,7 +39,7 @@ public function setName(string $name): Test {
3839
}
3940

4041
const JSON = '{
41-
"name_":"Wo\"\nrld!\\\",
42+
"name_":"Wo\"\nrld!\\\ /",
4243
"aeef23": {},
4344
"aeef2": {"test": true},
4445
"test": false,
@@ -48,7 +49,7 @@ public function setName(string $name): Test {
4849
}';
4950

5051
//echo Test::fromJson(JSON)->toJson();
51-
$json = new JSONPlus(new JsonSerializationAdapter());
52+
$json = new JSONPlus(new PHPJsonSerializationAdapter());
5253
$var = $json->fromJson(JSON, Test::class);
53-
echo $var->test."\n";
54+
echo $var->name."\n";
5455
echo $json->toJson($var);

0 commit comments

Comments
 (0)