Skip to content

Commit

Permalink
Merge pull request #9 from nspcc-dev/quotes
Browse files Browse the repository at this point in the history
tables: escape double quotes for C# compatibility
  • Loading branch information
AnnaShaleva authored Jan 12, 2024
2 parents 3374ff1 + 6f8327a commit 296698a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ func TestUnmarshalPtrPtr(t *testing.T) {

func TestEscape(t *testing.T) {
const input = `"foobar"<html>` + " [\u2028 \u2029]"
const expected = `"\"foobar\"\u003Chtml\u003E [\u2028 \u2029]"`
const expected = `"\u0022foobar\u0022\u003Chtml\u003E [\u2028 \u2029]"`
b, err := Marshal(input)
if err != nil {
t.Fatalf("Marshal error: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ func (e *encodeState) string(s string, escapeHTML bool) int {
e.WriteString(s[start:i])
}
switch b {
case '\\', '"':
case '\\':
e.WriteByte('\\')
e.WriteByte(b)
case '\n':
Expand All @@ -927,7 +927,7 @@ func (e *encodeState) string(s string, escapeHTML bool) int {
e.WriteByte('\\')
e.WriteByte('t')
default:
// This encodes bytes < 0x20 except for \t, \n and \r.
// This encodes bytes < 0x20 and " except for \t, \n and \r.
// If escapeHTML is set, it also escapes <, >, and &
// because they can lead to security holes when
// user-controlled strings are rendered into JSON
Expand Down Expand Up @@ -1001,7 +1001,7 @@ func (e *encodeState) stringBytes(s []byte, escapeHTML bool) int {
e.Write(s[start:i])
}
switch b {
case '\\', '"':
case '\\':
e.WriteByte('\\')
e.WriteByte(b)
case '\n':
Expand All @@ -1014,7 +1014,7 @@ func (e *encodeState) stringBytes(s []byte, escapeHTML bool) int {
e.WriteByte('\\')
e.WriteByte('t')
default:
// This encodes bytes < 0x20 except for \t, \n and \r.
// This encodes bytes < 0x20 and " except for \t, \n and \r.
// If escapeHTML is set, it also escapes <, >, and &
// because they can lead to security holes when
// user-controlled strings are rendered into JSON
Expand Down
7 changes: 4 additions & 3 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type StringTag struct {
var stringTagExpected = `{
"BoolStr": "true",
"IntStr": "42",
"StrStr": "\"xzbit\""
"StrStr": "\u0022xzbit\u0022"
}`

func TestStringTag(t *testing.T) {
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestRefValMarshal(t *testing.T) {
V2: 15,
V3: new(ValText),
}
const want = `{"R0":"ref","R1":"ref","R2":"\"ref\"","R3":"\"ref\"","V0":"val","V1":"val","V2":"\"val\"","V3":"\"val\""}`
const want = `{"R0":"ref","R1":"ref","R2":"\u0022ref\u0022","R3":"\u0022ref\u0022","V0":"val","V1":"val","V2":"\u0022val\u0022","V3":"\u0022val\u0022"}`
b, err := Marshal(&s)
if err != nil {
t.Fatalf("Marshal: %v", err)
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestMarshalerEscaping(t *testing.T) {
}

var ct CText
want = `"\"\u003C\u0026\u003E\""`
want = `"\u0022\u003C\u0026\u003E\u0022"`
b, err = Marshal(ct)
if err != nil {
t.Fatalf("Marshal(ct): %v", err)
Expand Down Expand Up @@ -611,6 +611,7 @@ var encodeStringTests = []struct {
{"\x1e", `"\u001E"`},
{"\x1f", `"\u001F"`},
{"'", `"\u0027"`},
{"\"", `"\u0022"`},
}

func TestEncodeString(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestEncoderSetEscapeHTML(t *testing.T) {
want string
}{
{"c", c, `"\u003C\u0026\u003E"`, `"<&>"`},
{"ct", ct, `"\"\u003C\u0026\u003E\""`, `"\"<&>\""`},
{"ct", ct, `"\u0022\u003C\u0026\u003E\u0022"`, `"\u0022<&>\u0022"`},
{`"<&>"`, "<&>", `"\u003C\u0026\u003E"`, `"<&>"`},
} {
var buf bytes.Buffer
Expand Down

0 comments on commit 296698a

Please sign in to comment.