Skip to content

Commit 73600a9

Browse files
authored
Merge pull request #91 from francoispqt/fix/skip-unicode-string
Fix error when skipping string with unicode
2 parents ef10bf3 + 41189cb commit 73600a9

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

decode_object_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func TestDecodeObjectBasic(t *testing.T) {
447447
err: false,
448448
},
449449
{
450-
name: "basic-skip-data",
450+
name: "basic-skip-data-error-uint8-negative",
451451
json: `{
452452
"testStr": "hello world!",
453453
"testInt": 4535,
@@ -469,7 +469,7 @@ func TestDecodeObjectBasic(t *testing.T) {
469469
"testUint32": 343443,
470470
"testUint64": 545665757,
471471
"skipString": "skipping string with escaped \\n new line",
472-
"skipInt": 3,
472+
"skipInt": 3
473473
}`,
474474
expectedResult: testObject{
475475
testStr: "hello world!",
@@ -488,6 +488,48 @@ func TestDecodeObjectBasic(t *testing.T) {
488488
},
489489
err: true,
490490
},
491+
{
492+
name: "skip-data-with-unicode",
493+
json: `{
494+
"skipString": "hello\u1234\u2123",
495+
"testStr": "hello world!",
496+
"testInt": 4535,
497+
"testBool": true,
498+
"testFloat32": 2.345,
499+
"testFloat64": 123.677,
500+
"testInt8": 23,
501+
"skipObject": {
502+
"escapedString": "string with unicode \u1234\u1234\u1234"
503+
},
504+
"testInt16": 1245,
505+
"testInt32": 456778,
506+
"testInt64": 1446685358,
507+
"testUint8": 255,
508+
"skipArray": [[],[],{}],
509+
"testUint16": 3455,
510+
"skipBool": true,
511+
"skipNull": null,
512+
"testUint32": 343443,
513+
"testUint64": 545665757,
514+
"skipInt": 3
515+
}`,
516+
expectedResult: testObject{
517+
testStr: "hello world!",
518+
testInt: 4535,
519+
testBool: true,
520+
testFloat32: 2.345,
521+
testFloat64: 123.677,
522+
testInt8: 23,
523+
testInt16: 1245,
524+
testInt32: 456778,
525+
testInt64: 1446685358,
526+
testUint8: 255,
527+
testUint16: 3455,
528+
testUint32: 343443,
529+
testUint64: 545665757,
530+
},
531+
err: false,
532+
},
491533
}
492534

493535
for _, testCase := range testCases {

decode_string.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ func (dec *Decoder) skipEscapedString() error {
178178
return dec.raiseInvalidJSONErr(dec.cursor)
179179
}
180180
return nil
181+
case 'u': // is unicode, we skip the following characters and place the cursor one one byte backward to avoid it breaking when returning to skipString
182+
if err := dec.skipString(); err != nil {
183+
return err
184+
}
185+
dec.cursor--
186+
return nil
181187
case 'n', 'r', 't', '/', 'f', 'b':
182188
return nil
183189
default:
@@ -195,11 +201,12 @@ func (dec *Decoder) skipEscapedString() error {
195201
func (dec *Decoder) skipString() error {
196202
for dec.cursor < dec.length || dec.read() {
197203
switch dec.data[dec.cursor] {
198-
// string found
204+
// found the closing quote
205+
// let's return
199206
case '"':
200207
dec.cursor = dec.cursor + 1
201208
return nil
202-
// slash found
209+
// solidus found start parsing an escaped string
203210
case '\\':
204211
dec.cursor = dec.cursor + 1
205212
err := dec.skipEscapedString()

decode_string_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,20 +738,24 @@ func TestSkipString(t *testing.T) {
738738
expectedResult: "",
739739
err: false,
740740
},
741+
{
742+
name: "string-unicode",
743+
json: `[2]\u66fe\u5b97\u5357"`,
744+
expectedResult: "",
745+
err: false,
746+
},
741747
}
742748

743749
for _, testCase := range testCases {
744-
str := ""
745750
dec := NewDecoder(strings.NewReader(testCase.json))
746751
err := dec.skipString()
747752
if testCase.err {
748753
assert.NotNil(t, err, "err should not be nil")
749754
if testCase.errType != nil {
750755
assert.IsType(t, testCase.errType, err, "err should be of expected type")
751756
}
752-
} else {
753-
assert.Nil(t, err, "err should be nil")
757+
return
754758
}
755-
assert.Equal(t, testCase.expectedResult, str, fmt.Sprintf("str should be equal to '%s'", testCase.expectedResult))
759+
assert.Nil(t, err, "err should be nil")
756760
}
757761
}

0 commit comments

Comments
 (0)