Skip to content

Commit 7d79c52

Browse files
committed
fix: string contains slash will cause lexer parse error
1 parent a470ac0 commit 7d79c52

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.PHONY: test push-to-npm
2+
3+
test:
4+
npm run test
5+
6+
push-to-npm:
7+
npm publish --access public
8+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "streaming-json",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "A streamlined, user-friendly JSON streaming preprocessor, crafted in JavaScript.",
55
"sideEffects": false,
66
"main": "dist/index.js",

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,9 @@ class Lexer {
14201420
// pop `\` from stack
14211421
this.popTokenStack();
14221422
continue;
1423+
} else if (this.streamStoppedInAString()) {
1424+
this.JSONContent += tokenSymbol;
1425+
continue;
14231426
}
14241427
break;
14251428
case TOKEN_ESCAPE_CHARACTER: // TOKEN_ESCAPE_CHARACTER needs to be defined somewhere

test/test.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
var expect = require('expect.js');
22
const assert = require('assert');
33

4-
54
// js 测试源文件
65
var streamingjson = require('../src/index.js');
76

8-
9-
107
describe('Test complete JSON base', () => {
118
const testCases = [
129
// test case: basic object properity
@@ -62,9 +59,17 @@ describe('Test complete JSON base', () => {
6259
[`{"a":"string`, `{"a":"string"}`],
6360
[`{"a":"string"`, `{"a":"string"}`],
6461
[`{"a":"string",`, `{"a":"string"}`],
65-
[`{"a":"abcdefghijklmnopqrstuvwxyz",`, `{"a":"abcdefghijklmnopqrstuvwxyz"}`],
66-
[`{"a":"ABCDEFGHIJKLMNOPQRSTUVWXYZ",`, `{"a":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}`],
62+
[
63+
`{"a":"abcdefghijklmnopqrstuvwxyz",`,
64+
`{"a":"abcdefghijklmnopqrstuvwxyz"}`,
65+
],
66+
[
67+
`{"a":"ABCDEFGHIJKLMNOPQRSTUVWXYZ",`,
68+
`{"a":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}`,
69+
],
6770
[`{"a":"0123456789",`, `{"a":"0123456789"}`],
71+
[`{"a":"https://`, `{"a":"https://"}`],
72+
[`{"a":"https://example.com/api/v1`, `{"a":"https://example.com/api/v1"}`],
6873
[`{"a":"\\u0`, `{"a":""}`],
6974
[`{"a":"\\u00`, `{"a":""}`],
7075
[`{"a":"\\u004`, `{"a":""}`],
@@ -252,7 +257,10 @@ describe('Test complete JSON base', () => {
252257
[`{"a":{"b":"c"}}`, `{"a":{"b":"c"}}`],
253258

254259
// test case: multiple object properity
255-
[`{"a":1,"b":1.20,"c":0.03,"d":-1,"e":-1.20,"f":-0.03,"g":1.997e3,"h":-1.338e19,"i":"a","j":null,"k":true,"l":false,"m":{},"n":[]]}`, `{"a":1,"b":1.20,"c":0.03,"d":-1,"e":-1.20,"f":-0.03,"g":1.997e3,"h":-1.338e19,"i":"a","j":null,"k":true,"l":false,"m":{},"n":[]]}`],
260+
[
261+
`{"a":1,"b":1.20,"c":0.03,"d":-1,"e":-1.20,"f":-0.03,"g":1.997e3,"h":-1.338e19,"i":"a","j":null,"k":true,"l":false,"m":{},"n":[]]}`,
262+
`{"a":1,"b":1.20,"c":0.03,"d":-1,"e":-1.20,"f":-0.03,"g":1.997e3,"h":-1.338e19,"i":"a","j":null,"k":true,"l":false,"m":{},"n":[]]}`,
263+
],
256264

257265
// test case: basic array element
258266
[`[`, `[]`],
@@ -339,6 +347,7 @@ describe('Test complete JSON base', () => {
339347
[`["a","b"`, `["a","b"]`],
340348
[`["a","b",`, `["a","b"]`],
341349
[`["a","b"]`, `["a","b"]`],
350+
[`["https://example.com/api/v1`, `["https://example.com/api/v1"]`],
342351
[`["\\u0`, `[""]`],
343352
[`["\\u00`, `[""]`],
344353
[`["\\u004`, `[""]`],
@@ -351,6 +360,10 @@ describe('Test complete JSON base', () => {
351360
[`["\\u0049","\\u0`, `["\\u0049",""]`],
352361
[`["\\u0049","\\u00`, `["\\u0049",""]`],
353362
[`["\\u0049","\\u005`, `["\\u0049",""]`],
363+
[
364+
`["https://example.com/api/v1", "http://example.com:8080/user/sample_user/index.html`,
365+
`["https://example.com/api/v1", "http://example.com:8080/user/sample_user/index.html"]`,
366+
],
354367
[`["\\u0049","\\u0050`, `["\\u0049","\\u0050"]`],
355368
[`["\\u0049","\\u0050"`, `["\\u0049","\\u0050"]`],
356369
[`["\\u0049","\\u0050"]`, `["\\u0049","\\u0050"]`],
@@ -429,7 +442,10 @@ describe('Test complete JSON base', () => {
429442
[`[{"a":[{"b":"c"},{"d":[{`, `[{"a":[{"b":"c"},{"d":[{}]}]}]`],
430443

431444
// test case: multiple array element
432-
[`[1,1.20,0.03,-1,-1.20,-0.03,1.997e3,-1.338e19,"a",null,true,false,{},[]]`, `[1,1.20,0.03,-1,-1.20,-0.03,1.997e3,-1.338e19,"a",null,true,false,{},[]]`],
445+
[
446+
`[1,1.20,0.03,-1,-1.20,-0.03,1.997e3,-1.338e19,"a",null,true,false,{},[]]`,
447+
`[1,1.20,0.03,-1,-1.20,-0.03,1.997e3,-1.338e19,"a",null,true,false,{},[]]`,
448+
],
433449

434450
// test case: array as array element
435451
[`[[`, `[[]]`],
@@ -461,13 +477,28 @@ describe('Test complete JSON base', () => {
461477
// test case: ignore token
462478
[`{ }`, `{ }`],
463479
[`{ " a " : -1.2 , `, `{ " a " : -1.2}`],
464-
[`{ " a " : -1.2 , " b " : " c " `, `{ " a " : -1.2 , " b " : " c "}`],
465-
[`{ " a " : -1.2 , " b " : " c " , " d" : true `, `{ " a " : -1.2 , " b " : " c " , " d" : true}`],
466-
[`{ " a " : -1.2 , " b " : " c " , " d" : true , "e " : { } } `, `{ " a " : -1.2 , " b " : " c " , " d" : true , "e " : { } }`],
480+
[
481+
`{ " a " : -1.2 , " b " : " c " `,
482+
`{ " a " : -1.2 , " b " : " c "}`,
483+
],
484+
[
485+
`{ " a " : -1.2 , " b " : " c " , " d" : true `,
486+
`{ " a " : -1.2 , " b " : " c " , " d" : true}`,
487+
],
488+
[
489+
`{ " a " : -1.2 , " b " : " c " , " d" : true , "e " : { } } `,
490+
`{ " a " : -1.2 , " b " : " c " , " d" : true , "e " : { } }`,
491+
],
467492
[`[ ]`, `[ ]`],
468493
[`[ 1`, `[ 1]`],
469-
[`[ 1 , -1.020 , true , false, null`, `[ 1 , -1.020 , true , false, null]`],
470-
[`[ 1 , -1.020 , true , false, null, { }`, `[ 1 , -1.020 , true , false, null, { }]`],
494+
[
495+
`[ 1 , -1.020 , true , false, null`,
496+
`[ 1 , -1.020 , true , false, null]`,
497+
],
498+
[
499+
`[ 1 , -1.020 , true , false, null, { }`,
500+
`[ 1 , -1.020 , true , false, null, { }]`,
501+
],
471502
];
472503

473504
testCases.forEach(([testCase, expected]) => {
@@ -480,10 +511,10 @@ describe('Test complete JSON base', () => {
480511
});
481512
});
482513

483-
484514
describe('Test complete JSON nested', () => {
485515
it('should correctly process and validate a complex, nested JSON string', () => {
486-
const streamingJSONContent = '{"string": "这是一个字符串", "integer": 42, "float": 3.14159, "boolean_true": true, "boolean_false": false, "null": null, "object": {"empty_object": {}, "non_empty_object": {"key": "value"}, "nested_object": {"nested_key": {"sub_nested_key": "sub_nested_value"}}}, "array":["string in array", 123, 45.67, true, false, null, {"object_in_array": "object_value"},["nested_array"]]}'
516+
const streamingJSONContent =
517+
'{"string": "这是一个字符串", "integer": 42, "float": 3.14159, "boolean_true": true, "boolean_false": false, "null": null, "object": {"empty_object": {}, "non_empty_object": {"key": "value"}, "nested_object": {"nested_key": {"sub_nested_key": "sub_nested_value"}}}, "array":["string in array", 123, 45.67, true, false, null, {"object_in_array": "object_value"},["nested_array"]]}';
487518

488519
const lexer = new streamingjson.Lexer();
489520
lexer.AppendString(streamingJSONContent);
@@ -496,7 +527,6 @@ describe('Test complete JSON nested', () => {
496527
});
497528
});
498529

499-
500530
describe('Test complete JSON nested 2', () => {
501531
it('should correctly process and validate a complex, nested JSON string', () => {
502532
const streamingJSONContent = `{
@@ -547,7 +577,7 @@ describe('Test complete JSON nested 2', () => {
547577
"empty_objects": { },
548578
"empty_arrays": []
549579
}
550-
}`
580+
}`;
551581

552582
const lexer = new streamingjson.Lexer();
553583
lexer.AppendString(streamingJSONContent);
@@ -560,7 +590,6 @@ describe('Test complete JSON nested 2', () => {
560590
});
561591
});
562592

563-
564593
describe('Test complete JSON escape and etc', () => {
565594
it('should correctly process and validate a complex, nested JSON string', () => {
566595
const streamingJSONContent = `{
@@ -585,7 +614,7 @@ describe('Test complete JSON escape and etc', () => {
585614
"complex_number": 3.14e-10
586615
}
587616
}
588-
}`
617+
}`;
589618

590619
const lexer = new streamingjson.Lexer();
591620
lexer.AppendString(streamingJSONContent);

0 commit comments

Comments
 (0)