Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,27 @@ class SJSON {
function ws() {
while (i < s.length) {
if (s[i] === 47) { // "/"
const start = i;
++i;
if (s[i] === 47)
while (s[++i] !== 10); // "\n"
else if (s[i] === 42) // "*"
while (s[++i] !== 42);
if (s[i] === 47) // "/"
while (++i < s.length && s[i] !== 10); // "\n"
else if (s[i] === 42) { // "*"
while (++i < s.length && s[i] !== 42); // "*"
if (s[i] === 47) { // "/"
i++;
break;
}
// No multi-line comment
else {
i = start;
break;
}
}
// No single-line comment
else {
i = start;
break;
}
} else if (!hasChar(WHITESPACE, s[i])) {
break;
}
Expand Down Expand Up @@ -97,15 +113,15 @@ class SJSON {
if (s[i] === 34 && s[i+1] === 34 && s[i+2] === 34) {
i += 3;
const start = i;
for (; s[i] !== 34 || s[i+1] !== 34 || s[i+2] !== 34; ++i);
for (; i+2 < s.length && (s[i] !== 34 || s[i+1] !== 34 || s[i+2] !== 34); ++i);
i += 3;
return s.toString('utf8', start, i-3);
}

const start = i;
let escape = false;
consume(34);
for (; s[i] !== 34; ++i) { // unescaped "
for (; i < s.length && s[i] !== 34; ++i) { // unescaped "
if (s[i] === 92) {
++i;
escape = true;
Expand All @@ -118,7 +134,7 @@ class SJSON {
i = start;
var octets = [];
consume(34);
for (; s[i] !== 34; ++i) { // unescaped "
for (; i < s.length && s[i] !== 34; ++i) { // unescaped "
if (s[i] === 92) {
++i;
if (s[i] == 98) octets.push(8); // \b
Expand Down Expand Up @@ -146,7 +162,7 @@ class SJSON {
ws();
consume(91); // "["
ws();
for (; s[i] !== 93; ws()) // "]"
for (; i < s.length && s[i] !== 93; ws()) // "]"
ar.push(pvalue());

consume(93);
Expand All @@ -161,15 +177,15 @@ class SJSON {
return pstring();

const start = i;
for (; !hasChar(ID_TERM, s[i]); ++i);
for (; i < s.length && !hasChar(ID_TERM, s[i]); ++i);
return s.toString("utf8", start, i);
}

function pobject() {
const object = Object.setPrototypeOf({}, null);
consume(123); // "{"
ws();
for (; s[i] !== 125; ws()) { // "}"
for (; i < s.length && s[i] !== 125; ws()) { // "}"
const key = pidentifier();
ws();
(s[i] === 58) ? consume(58) : consume(61); // ":" or "="
Expand Down
10 changes: 10 additions & 0 deletions test/sjson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ describe('simplified-json', function () {
expect(parsedSJSON.nullable).to.be.null;
});

/**
* Validate that the SJSON Parser correctly parses keys with a leading solidus.
*/
it('parses keys with a leading solidus', function () {
const parsedSJSON = SJSON.parse('/key_with_a_leading_solidus = true');

expect(parsedSJSON["/key_with_a_leading_solidus"]).to.be.a('Boolean');
expect(parsedSJSON["/key_with_a_leading_solidus"]).to.be.true;
});

/**
* Validate that the SJSON Parser throws a SyntaxError when encountering an
* unexpected token.
Expand Down