Skip to content
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
32 changes: 32 additions & 0 deletions gocode/flyweight/src/baseline/JsonEscapedString_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package baseline

import (
"encoding/json"
"testing"
)

func TestJsonEscapedStringEscapesControlCharacters(t *testing.T) {
var data [256]byte
var in Car
in.WrapForEncode(data[:], 0, uint64(len(data)))
in.FuelFiguresCount(0)
in.PerformanceFiguresCount(0)
in.PutManufacturer("a\x01\"b\x1f").
PutModel("Civic").
PutActivationCode("x")

var out Car
out.WrapForDecode(data[:], 0, uint64(in.SbeBlockLength()), uint64(in.SbeSchemaVersion()), uint64(len(data)))
out.FuelFigures()
out.PerformanceFigures()

escaped := out.GetManufacturerAsJsonEscapedString()
if escaped != `a\u0001\"b\u001f` {
t.Fatalf("unexpected escaped string: %s", escaped)
}

var decoded string
if err := json.Unmarshal([]byte(`"`+escaped+`"`), &decoded); err != nil || decoded != "a\x01\"b\x1f" {
t.Fatalf("escaped string does not round trip through JSON: %q, %v", decoded, err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ private void generateJsonEscapedStringGetter(
indent + "\t\t\toss.WriteString(\"\\\\t\")\n\n" +
indent + "\t\tdefault:\n" +
indent + "\t\t\tif '\\x00' <= c && c <= '\\x1f' {\n" +
indent + "\t\t\t\tfmt.Fprintf(&oss, \"%%x\", c)\n" +
indent + "\t\t\t\tfmt.Fprintf(&oss, \"\\\\u%%04x\", c)\n" +
indent + "\t\t\t} else {\n" +
indent + "\t\t\t\toss.WriteString(string(c))\n" +
indent + "\t\t\t}\n" +
Expand Down