Skip to content

Commit

Permalink
Merge pull request #10 from adhocore/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adhocore authored Jul 19, 2018
2 parents 2af3e68 + db48ab3 commit edfd65c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ $someJsonText = file_get_contents('...');

// You can pass args like in `json_decode`
(new Comment)->decode($someJsonText, $assoc = true, $depth = 512, $options = JSON_BIGINT_AS_STRING);

// Or you can use static alias of decode:
Comment::parse($json, true);
```
22 changes: 22 additions & 0 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ public function strip($json)
$index += $charnext === '*/' ? 1 : 0;
}

$this->reset();

return $return;
}

protected function reset()
{
$this->comment = 0;
$this->inStr = false;
}

protected function inStringOrCommentEnd($prev, $char, $charnext)
{
if (0 === $this->comment && $char === '"' && $prev !== '\\') {
Expand Down Expand Up @@ -108,4 +116,18 @@ public function decode($json, $assoc = false, $depth = 512, $options = 0)

return $decoded;
}

/**
* Static alias of decode().
*/
public static function parse($json, $assoc = false, $depth = 512, $options = 0)
{
static $parser;

if (!$parser) {
$parser = new static;
}

return $parser->decode($json, $assoc, $depth, $options);
}
}
14 changes: 14 additions & 0 deletions tests/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public function testDecodeThrows()
(new Comment)->decode('{"a":1, /* comment */, "b":}', true);
}

public function testParse()
{
$parsed = Comment::parse('{
// comment
"a//b":"/*value*/"
/* also comment */
}', true);

$this->assertNotEmpty($parsed);
$this->assertInternalType('array', $parsed);
$this->assertArrayHasKey('a//b', $parsed);
$this->assertSame('/*value*/', $parsed['a//b']);
}

public function theTests()
{
return [
Expand Down

0 comments on commit edfd65c

Please sign in to comment.