Skip to content

Commit 43a564a

Browse files
author
TeleMessage
committed
fixed end line chars
1 parent 33631b9 commit 43a564a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/parsers/json/Reader.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function parse() {
8484
private function parseRecursive() {
8585
$sb = "";
8686
while (false !== ($c = $this->in->nextChar())) {
87-
if ($c != self::SPACE_CHAR && $c != self::TAB_CHAR) {
87+
if ($c != self::SPACE_CHAR && $c != self::TAB_CHAR && $c != "\r" && $c != "\n") {
8888
$p = $this->parseStructure($c);
8989
$o = $p->getRight();
9090
$c = $p->getLeft();
@@ -132,12 +132,15 @@ private function parseStructure($c) {
132132
if ($c == self::END_MAP) {
133133
$this->queue->dequeue();
134134
$cl = $this->conf->getString(Configuration::CLASS_PROPERTY, Configuration::DEFAULT_CLASS_PROPERTY_VALUE);
135+
$p = new Pair($c, $m);
135136
if (isset($m[$cl])) {
136137
$o = $this->createClass($m);
137-
$c = $this->in->nextChar();
138-
return new Pair($c, $o);
138+
$p = new Pair($c, $o);
139139
}
140-
return new Pair($c, $m);
140+
if (false !== ($ch = $this->in->nextChar())) {
141+
$p->setLeft($ch);
142+
}
143+
return $p;
141144
}
142145
break;
143146
case self::START_ARRAY:
@@ -146,7 +149,9 @@ private function parseStructure($c) {
146149
$c = $this->parseList($l);
147150
if ($c == self::END_ARRAY) {
148151
$this->queue->dequeue();
149-
$c = $this->in->nextChar();
152+
if (false !== ($ch = $this->in->nextChar())) {
153+
$c = $ch;
154+
}
150155
return new Pair($c, $l);
151156
}
152157
break;
@@ -213,13 +218,13 @@ private function parseMap(&$m) {
213218
while (false !== ($c = $this->in->nextChar())) {
214219
if ($c == self::END_MAP) {
215220
return $c;
216-
} else {
221+
} else if ($c != self::SPACE_CHAR && $c != self::TAB_CHAR && $c != "\r" && $c != "\n") {
217222
$sb = "";
218223
// searching key
219224
do {
220225
if ($c === false)
221226
throw new \Exception("Reached end of stream - un-parsed data");
222-
if ($c != self::ELEM_DELIM)
227+
if ($c != self::ELEM_DELIM && $c != "\r" && $c != "\n")
223228
$sb = $sb . $c;
224229
} while (false !== ($c = $this->in->nextChar()) && $c != self::VALUE_DELIM);
225230
$key = trim($sb);
@@ -251,7 +256,7 @@ private function parseList(&$l) {
251256
while (false !== ($c = $this->in->nextChar())) {
252257
if ($c == self::END_ARRAY) {
253258
return $c;
254-
} else if ($c == self::ELEM_DELIM || $c == self::VALUE_DELIM) {
259+
} else if ($c == self::SPACE_CHAR || $c == self::TAB_CHAR || $c == self::ELEM_DELIM || $c == self::VALUE_DELIM || $c == "\r" || $c == "\n") {
255260
// do nothing
256261
} else {
257262
$p = $this->parseListInnerElement($c);

test/parsers/json/ReaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,10 @@ public function testParse() {
105105
$this->assertEquals("grinfeld\\phpjsonable\\utils\\Pair", get_class($result), "should " . get_class($result) . " == grinfeld\\phpjsonable\\utils\\Pair");
106106
$this->assertEquals(100, $result->getLeft(), "should " . $result->getLeft() . " == 100");
107107
$this->assertEquals("Test", $result->getRight(), "should " . $result->getRight() . " == Test");
108+
109+
$str = " {\r\n\"key1\": \"hello\"\r\n }";
110+
$fp = new StringInputStream($str);
111+
$res = (new Reader($fp))->parse();
112+
$this->assertEquals("hello", $res["key1"], "should " . $res["key1"] . " == hello");
108113
}
109114
}

0 commit comments

Comments
 (0)