Skip to content

Commit 080f90f

Browse files
committed
Allowing elided quotes around JSON literal values
1 parent 5054d0b commit 080f90f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Lexer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,13 @@ private function parseJson(array $token)
421421
$value = json_decode($token['value'], true);
422422

423423
if ($error = json_last_error()) {
424-
$token['type'] = self::T_UNKNOWN;
425-
return $token;
424+
// Legacy support for elided quotes. Try to parse again by adding
425+
// quotes around the bad input value.
426+
$value = json_decode('"' . $token['value'] . '"', true);
427+
if ($error = json_last_error()) {
428+
$token['type'] = self::T_UNKNOWN;
429+
return $token;
430+
}
426431
}
427432

428433
$token['value'] = $value;

tests/LexerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ public function testTokenizesJsonNumbers()
7878
$this->assertEquals(120, $tokens[6]['value']);
7979
$this->assertEquals('2014-12', $tokens[8]['value']);
8080
}
81+
82+
public function testCanWorkWithElidedJsonLiterals()
83+
{
84+
$l = new Lexer();
85+
$tokens = $l->tokenize('`foo`');
86+
$this->assertEquals('foo', $tokens[0]['value']);
87+
$this->assertEquals('literal', $tokens[0]['type']);
88+
}
8189
}

0 commit comments

Comments
 (0)