Skip to content

Commit

Permalink
fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Janek Lasocki-Biczysko committed May 21, 2015
1 parent 57ee005 commit b8f2cdf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/janeklb/json/JSONCharInputReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class JSONCharInputReader
private $outputInterface;
private $state;
private $depth;
private $quoteCount;

/**
* Create a JSONCharInputReader object
Expand All @@ -34,6 +35,7 @@ public function __construct(JSONChunkProcessor $outputInterface)
$this->state = self::STATE_OUTSIDE;
$this->outputInterface = $outputInterface;
$this->depth = 0;
$this->quoteCount = 0;
}

/**
Expand Down Expand Up @@ -62,27 +64,39 @@ public function readChar($char)
case self::STATE_INCURLY:
case self::STATE_INSQUARE:

if ($char == '"' && !$this->lastCharIs('\\'))
$this->quoteCount++;

$this->buffer .= $char;

// if quote count is odd we're inside a string....
if ($this->quoteCount % 2)
break;

$closing = $this->state == self::STATE_INCURLY ? '}' : ']';
$opening = $this->state == self::STATE_INCURLY ? '{' : '[';

if ($char == $opening)
// if this is another opening brace/bracket character, increase the depth
$this->depth++;
else if ($char == $closing && --$this->depth == 0)
{
// if this is a closing character, decrease the depth and process the buffer if
// the bottom was reached
$this->processBuffer();
$this->quoteCount = 0;
}

break;

// Inside a string
case self::STATE_INSTRING:
$this->buffer .= $char;
if ($char == '"')

if ($char == '"' && !$this->lastCharIs('\\'))
$this->state = self::STATE_WAITING;

$this->buffer .= $char;

break;

// Waiting on any input within a JSON stream
Expand Down Expand Up @@ -136,6 +150,15 @@ public function readChar($char)
}
}

private function lastCharIs($char) {
$len = strlen($this->buffer);
if ($len == 0)
return false;

$lastChar = $this->buffer[$len - 1];
return $lastChar === $char;
}

/**
* Process the JSON data stream's buffer and reset the state
*
Expand Down
7 changes: 7 additions & 0 deletions test/suite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require 'PHPUnit/Autoload.php';

$suite = new PHPUnit_Framework_TestSuite('JSONCharInputReader Test Suite');
$suite->addTestFile(__DIR__ . "/test.php");
PHPUnit_TextUI_TestRunner::run($suite);
18 changes: 18 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public function testMixed()
}

public function testEscaped()
{
$this->sendInput('"abc\"def",');
$this->assertObjects('abc"def');
}

public function testEscapedInObject()
{
$this->sendInput('{"x": "x\"a"},{"a\"b":1}');

Expand All @@ -118,6 +124,18 @@ public function testEscaped()
}

public function testBracketsAndBracesInString()
{
$this->sendInput('"str}ing", "str]ing", ');
$this->assertObjects("str}ing", "str]ing");
}

public function testBracketsAndBracesInArrayString()
{
$this->sendInput('["str}ing"], ["str]ing"], ["str]ing", "str}ing"]');
$this->assertObjects(array("str}ing"), array("str]ing"), array("str]ing", "str}ing"));
}

public function testBracketsAndBracesInObjectString()
{
$this->sendInput('{"bracket": "val]ue", "brace": "val}ue"}');

Expand Down

0 comments on commit b8f2cdf

Please sign in to comment.