Skip to content

Commit

Permalink
fixed end line chars
Browse files Browse the repository at this point in the history
  • Loading branch information
TeleMessage committed Aug 30, 2015
1 parent 33631b9 commit 43a564a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/parsers/json/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function parse() {
private function parseRecursive() {
$sb = "";
while (false !== ($c = $this->in->nextChar())) {
if ($c != self::SPACE_CHAR && $c != self::TAB_CHAR) {
if ($c != self::SPACE_CHAR && $c != self::TAB_CHAR && $c != "\r" && $c != "\n") {
$p = $this->parseStructure($c);
$o = $p->getRight();
$c = $p->getLeft();
Expand Down Expand Up @@ -132,12 +132,15 @@ private function parseStructure($c) {
if ($c == self::END_MAP) {
$this->queue->dequeue();
$cl = $this->conf->getString(Configuration::CLASS_PROPERTY, Configuration::DEFAULT_CLASS_PROPERTY_VALUE);
$p = new Pair($c, $m);
if (isset($m[$cl])) {
$o = $this->createClass($m);
$c = $this->in->nextChar();
return new Pair($c, $o);
$p = new Pair($c, $o);
}
return new Pair($c, $m);
if (false !== ($ch = $this->in->nextChar())) {
$p->setLeft($ch);
}
return $p;
}
break;
case self::START_ARRAY:
Expand All @@ -146,7 +149,9 @@ private function parseStructure($c) {
$c = $this->parseList($l);
if ($c == self::END_ARRAY) {
$this->queue->dequeue();
$c = $this->in->nextChar();
if (false !== ($ch = $this->in->nextChar())) {
$c = $ch;
}
return new Pair($c, $l);
}
break;
Expand Down Expand Up @@ -213,13 +218,13 @@ private function parseMap(&$m) {
while (false !== ($c = $this->in->nextChar())) {
if ($c == self::END_MAP) {
return $c;
} else {
} else if ($c != self::SPACE_CHAR && $c != self::TAB_CHAR && $c != "\r" && $c != "\n") {
$sb = "";
// searching key
do {
if ($c === false)
throw new \Exception("Reached end of stream - un-parsed data");
if ($c != self::ELEM_DELIM)
if ($c != self::ELEM_DELIM && $c != "\r" && $c != "\n")
$sb = $sb . $c;
} while (false !== ($c = $this->in->nextChar()) && $c != self::VALUE_DELIM);
$key = trim($sb);
Expand Down Expand Up @@ -251,7 +256,7 @@ private function parseList(&$l) {
while (false !== ($c = $this->in->nextChar())) {
if ($c == self::END_ARRAY) {
return $c;
} else if ($c == self::ELEM_DELIM || $c == self::VALUE_DELIM) {
} else if ($c == self::SPACE_CHAR || $c == self::TAB_CHAR || $c == self::ELEM_DELIM || $c == self::VALUE_DELIM || $c == "\r" || $c == "\n") {
// do nothing
} else {
$p = $this->parseListInnerElement($c);
Expand Down
5 changes: 5 additions & 0 deletions test/parsers/json/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,10 @@ public function testParse() {
$this->assertEquals("grinfeld\\phpjsonable\\utils\\Pair", get_class($result), "should " . get_class($result) . " == grinfeld\\phpjsonable\\utils\\Pair");
$this->assertEquals(100, $result->getLeft(), "should " . $result->getLeft() . " == 100");
$this->assertEquals("Test", $result->getRight(), "should " . $result->getRight() . " == Test");

$str = " {\r\n\"key1\": \"hello\"\r\n }";
$fp = new StringInputStream($str);
$res = (new Reader($fp))->parse();
$this->assertEquals("hello", $res["key1"], "should " . $res["key1"] . " == hello");
}
}

0 comments on commit 43a564a

Please sign in to comment.