Skip to content

Commit

Permalink
Fix escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
Krivchun Maxim committed Nov 13, 2020
1 parent a11ab19 commit 979c0af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion jsoncjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,23 @@ const (
tokenMultiComment
tokenUnknownComment
tokenOther
tokenEscaping
)

func (t *jsoncTranslator) handleToken(curByte byte) (skip bool) {
switch t.lastToken {
case tokenString:
if curByte == '"' {
switch curByte {
case '"':
t.lastToken = tokenOther
case '\\':
t.lastToken = tokenEscaping
}

return false
case tokenEscaping:
t.lastToken = tokenString

return false
case tokenSingleComment:
if curByte == '\n' {
Expand Down
30 changes: 30 additions & 0 deletions jsoncjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,36 @@ func TestJSONWithCommentsReader(t *testing.T) {
})
}

func TestJSONStringEscaping(t *testing.T) {
type testData struct {
Hello string `json:"hello"`
}

t.Run("escape quote", func(tt *testing.T) {
const jsonc = `
{
"hello": "\"/"
}
`
var exp = &testData{
Hello: "\"/",
}
testJSON(tt, jsonc, exp, &testData{})
})

t.Run("escape slash", func(tt *testing.T) {
const jsonc = `
{
"hello": "Hello \\World\\"
}
`
var exp = &testData{
Hello: "Hello \\World\\",
}
testJSON(tt, jsonc, exp, &testData{})
})
}

func TestComplexExample(t *testing.T) {
const jsonc = `
{ // ...
Expand Down

0 comments on commit 979c0af

Please sign in to comment.