Skip to content

Commit 82379e8

Browse files
committed
Empty JSON literals should return an empty string
1 parent f4cbfd4 commit 82379e8

File tree

2 files changed

+4
-29
lines changed

2 files changed

+4
-29
lines changed

src/Lexer.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,20 @@ public function tokenize($input)
6868
switch ($token['type']) {
6969
case 'quoted_identifier':
7070
$token['value'] = $this->decodeJson(
71-
$token['value'],
72-
$offset,
73-
$input
71+
$token['value'], $offset, $input
7472
);
7573
break;
7674
case 'number':
7775
$token['value'] = (int) $token['value'];
7876
break;
7977
case 'literal':
80-
$token['value'] = $this->takeLiteral(
81-
$token['value'],
82-
$offset,
83-
$input
78+
$token['value'] = $this->literal(
79+
$token['value'], $offset, $input
8480
);
8581
break;
8682
}
8783
$tokens[] = $token;
8884
}
89-
9085
$offset += strlen($match[0]);
9186
}
9287

@@ -99,15 +94,11 @@ public function tokenize($input)
9994
return $tokens;
10095
}
10196

102-
private function takeLiteral($value, $offset, $input)
97+
private function literal($value, $offset, $input)
10398
{
10499
static $valid = '/(true|false|null)|(^[\["{])|(^\-?[0-9]*(\.[0-9]+)?([e|E][+|\-][0-9]+)?$)/';
105100
$value = str_replace('\\`', '`', ltrim(substr($value, 1, -1)));
106101

107-
if ($value === '') {
108-
throw $this->throwSyntax('Empty JSON literal', $offset, $input);
109-
}
110-
111102
return preg_match($valid, $value)
112103
? $this->decodeJson($value, $offset, $input)
113104
: $this->decodeJson('"' . $value . '"', $offset, $input);

tests/LexerTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ public function testValidatesClosedQuotes()
2525
}
2626
}
2727

28-
public function testValidatesLiteralValuesAreSet()
29-
{
30-
$l = new Lexer();
31-
try {
32-
$l->tokenize('``');
33-
$this->fail('Did not throw');
34-
} catch (SyntaxErrorException $e) {
35-
$expected = <<<EOT
36-
Syntax error at character 0
37-
``
38-
^
39-
EOT;
40-
$this->assertContains($expected, $e->getMessage());
41-
}
42-
}
43-
4428
public function testValidatesLiteralValuesAreClosed()
4529
{
4630
$l = new Lexer();

0 commit comments

Comments
 (0)