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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ gocode/flyweight/src/*/*.go
!gocode/flyweight/src/*/*_test.go
gocode/flyweight/src/*/*/*.go
!gocode/flyweight/src/*/*/*_test.go
gocode/flyweight/src/otf/code-generation-schema.sbeir
gocode/flyweight/src/otf/*.sbeir

# csharp
csharp/*/bin
Expand Down
73 changes: 73 additions & 0 deletions gocode/flyweight/src/otf/json_char_escaping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package otf

import (
"encoding/binary"
stdjson "encoding/json"
"github.com/real-logic/simple-binary-encoding/json"
"github.com/real-logic/simple-binary-encoding/otf"
"github.com/stretchr/testify/assert"
"testing"
)

//go:generate java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -Dsbe.output.dir=. -Dsbe.target.language=golang -Dsbe.go.generate.generate.flyweights=true -Dsbe.generate.ir=true -Dsbe.keyword.append.token=X -jar ../../../${SBE_JAR} ../../../../sbe-tool/src/test/resources/json-char-escaping-schema.xml

const JsonCharSchemaFilename = "json-char-escaping-schema.sbeir"

// printChars renders a Chars message whose char fields and var data hold the given bytes.
func printChars(t *testing.T, single byte, array string, first byte, last byte, note string) string {
ir := otf.NewIrDecoder()
if ir.DecodeFile(JsonCharSchemaFilename) < 0 {
t.Fatalf("Error decoding %s", JsonCharSchemaFilename)
}
printer, err := json.NewJsonPrinter(ir)
assert.NoError(t, err)

const headerLength = 8
const blockLength = 1 + 4 + 2
buffer := make([]byte, headerLength+blockLength+2+len(note))
binary.LittleEndian.PutUint16(buffer[0:], blockLength)
binary.LittleEndian.PutUint16(buffer[2:], 1)
binary.LittleEndian.PutUint16(buffer[4:], 8)
binary.LittleEndian.PutUint16(buffer[6:], 0)
buffer[headerLength] = single
copy(buffer[headerLength+1:], array)
buffer[headerLength+5] = first
buffer[headerLength+6] = last
binary.LittleEndian.PutUint16(buffer[headerLength+blockLength:], uint16(len(note)))
copy(buffer[headerLength+blockLength+2:], note)

out, err := printer.PrintJson(buffer)
assert.NoError(t, err)
return out
}

func decodeChars(t *testing.T, out string) map[string]interface{} {
var decoded map[string]interface{}
if err := stdjson.Unmarshal([]byte(out), &decoded); err != nil {
t.Fatalf("invalid JSON: %v\n%s", err, out)
}
return decoded
}

func TestJsonCharFieldsAreEscaped(t *testing.T) {
assert := assert.New(t)

decoded := decodeChars(t, printChars(t, '"', "a\"\\\n", '\\', '"', "x\ty\nz\x01"))

assert.Equal("\"", decoded["single"])
assert.Equal([]interface{}{"a", "\"", "\\", "\n"}, decoded["array"])
assert.Equal(map[string]interface{}{"first": "\\", "last": "\""}, decoded["Pair"])
assert.Equal("x\ty\nz\x01", decoded["note"])
assert.Len(decoded, 4)
}

func TestJsonPlainCharFieldsAreUnchanged(t *testing.T) {
assert := assert.New(t)

decoded := decodeChars(t, printChars(t, 'S', "abcd", 'f', 'l', "note"))

assert.Equal("S", decoded["single"])
assert.Equal([]interface{}{"a", "b", "c", "d"}, decoded["array"])
assert.Equal(map[string]interface{}{"first": "f", "last": "l"}, decoded["Pair"])
assert.Equal("note", decoded["note"])
}
48 changes: 44 additions & 4 deletions sbe-tool/src/main/golang/json/json_token_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,49 @@ func (t *JsonTokenListener) escapeString(str string) {
}

func (t *JsonTokenListener) escape(c rune) {
if c == '"' || c == '\\' || c == '\b' || c == '\f' || c == '\n' || c == '\r' || c == '\t' {
t.output.WriteString("\\")
switch c {
case '"':
t.output.WriteString("\\\"")
case '\\':
t.output.WriteString("\\\\")
case '\b':
t.output.WriteString("\\b")
case '\f':
t.output.WriteString("\\f")
case '\n':
t.output.WriteString("\\n")
case '\r':
t.output.WriteString("\\r")
case '\t':
t.output.WriteString("\\t")
default:
if c < 0x20 {
t.output.WriteString("\\u00")
t.output.WriteByte("0123456789abcdef"[c>>4])
t.output.WriteByte("0123456789abcdef"[c&0xf])
} else {
t.output.WriteRune(c)
}
}
}

t.output.WriteRune(c)
// appendChars writes the first arrayLength bytes of buffer, which hold a char field, as a JSON string for a single
// character or as an array of one character strings, escaping each byte.
func (t *JsonTokenListener) appendChars(buffer []byte, arrayLength int32) {
if arrayLength > 1 {
t.output.WriteString("[")
}
for i := int32(0); i < arrayLength; i++ {
if i > 0 {
t.output.WriteString(", ")
}
t.doubleQuote()
t.escape(rune(buffer[i]))
t.doubleQuote()
}
if arrayLength > 1 {
t.output.WriteString("]")
}
}

func (t *JsonTokenListener) determineName(
Expand Down Expand Up @@ -167,7 +205,9 @@ func (t *JsonTokenListener) appendEncodingAsString(
}
} else {
elementSize := encoding.PrimitiveType().Size()
if arrayLength < 1 && encoding.PrimitiveType() == otf.CHAR {
if arrayLength >= 1 && encoding.PrimitiveType() == otf.CHAR {
t.appendChars(buffer, arrayLength)
} else if arrayLength < 1 && encoding.PrimitiveType() == otf.CHAR {
t.doubleQuote()
t.escapePrintableChar(buffer, 0, int(arrayLength), elementSize)
} else {
Expand Down
32 changes: 32 additions & 0 deletions sbe-tool/src/test/resources/json-char-escaping-schema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
package="json_char_escaping"
id="8"
version="0"
semanticVersion="0"
description="Char fields and var data that carry wire controlled bytes into JSON output"
byteOrder="littleEndian">
<types>
<composite name="messageHeader" description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
<composite name="varStringEncoding">
<type name="length" primitiveType="uint16"/>
<type name="varData" primitiveType="uint8" length="0" characterEncoding="UTF-8"/>
</composite>
<type name="Array4" primitiveType="char" length="4"/>
<composite name="Pair">
<type name="first" primitiveType="char"/>
<type name="last" primitiveType="char"/>
</composite>
</types>
<sbe:message name="Chars" id="1">
<field name="single" id="1" type="char"/>
<field name="array" id="2" type="Array4"/>
<field name="pair" id="3" type="Pair"/>
<data name="note" id="4" type="varStringEncoding"/>
</sbe:message>
</sbe:messageSchema>
Loading