Support SetEscapeHTML

This commit is contained in:
Masaaki Goshima 2020-05-03 17:41:33 +09:00
parent 3d7267abc8
commit 6602d2d38c
6 changed files with 163 additions and 98 deletions

View File

@ -4,7 +4,9 @@ go 1.12
require ( require (
github.com/francoispqt/gojay v1.2.13 github.com/francoispqt/gojay v1.2.13
github.com/goccy/go-json v0.0.0-20200430045642-6a479c7159bf github.com/goccy/go-json v0.0.0-00010101000000-000000000000
github.com/json-iterator/go v1.1.9 github.com/json-iterator/go v1.1.9
github.com/mailru/easyjson v0.7.1 github.com/mailru/easyjson v0.7.1
) )
replace github.com/goccy/go-json => ../

View File

@ -25,16 +25,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/goccy/go-json v0.0.0-20200425134937-b2f2a20f6518 h1:Im9WLti1rM0Lu7yfI7Q4qUa63ACa8MpcWsKsP/s1scI=
github.com/goccy/go-json v0.0.0-20200425134937-b2f2a20f6518/go.mod h1:h37TlF6fMJkCKBvuOxM3yCc3CnzMOUL3Ya+zO4ERYXQ=
github.com/goccy/go-json v0.0.0-20200426030317-c37a3c1aac7b h1:x2rU3NmJ9v14sP8q6ibSDHWm3dAmEvVPjxiLAAz4iU0=
github.com/goccy/go-json v0.0.0-20200426030317-c37a3c1aac7b/go.mod h1:R/y4ycIiFvB+n4HQ2M0/BzGzFxFvgGk5wwIp4Q2JkCk=
github.com/goccy/go-json v0.0.0-20200426055945-3bd7507c309f h1:ZIUfVwBw7xpFIPp/7neJMAycPOv1HEbLsloc2FF6/mY=
github.com/goccy/go-json v0.0.0-20200426055945-3bd7507c309f/go.mod h1:R/y4ycIiFvB+n4HQ2M0/BzGzFxFvgGk5wwIp4Q2JkCk=
github.com/goccy/go-json v0.0.0-20200426062255-9b1349d40ab6 h1:vJLqMycJ1FZ5RAtM5uU2beB/1cw8CqhY/LQAsffUDTU=
github.com/goccy/go-json v0.0.0-20200426062255-9b1349d40ab6/go.mod h1:R/y4ycIiFvB+n4HQ2M0/BzGzFxFvgGk5wwIp4Q2JkCk=
github.com/goccy/go-json v0.0.0-20200430045642-6a479c7159bf h1:mnwIPeyuqSC+ADJsNaMkl3vlQc09zpHDzwNK83ftk9g=
github.com/goccy/go-json v0.0.0-20200430045642-6a479c7159bf/go.mod h1:R/y4ycIiFvB+n4HQ2M0/BzGzFxFvgGk5wwIp4Q2JkCk=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=

View File

@ -15,6 +15,7 @@ type Encoder struct {
buf []byte buf []byte
pool sync.Pool pool sync.Pool
enabledIndent bool enabledIndent bool
enabledHTMLEscape bool
prefix []byte prefix []byte
indentStr []byte indentStr []byte
indent int indent int
@ -65,7 +66,6 @@ func init() {
func NewEncoder(w io.Writer) *Encoder { func NewEncoder(w io.Writer) *Encoder {
enc := encPool.Get().(*Encoder) enc := encPool.Get().(*Encoder)
enc.w = w enc.w = w
enc.indent = 0
enc.reset() enc.reset()
return enc return enc
} }
@ -88,7 +88,7 @@ func (e *Encoder) Encode(v interface{}) error {
// //
// In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior. // In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.
func (e *Encoder) SetEscapeHTML(on bool) { func (e *Encoder) SetEscapeHTML(on bool) {
e.enabledHTMLEscape = on
} }
// SetIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent). // SetIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent).
@ -110,6 +110,9 @@ func (e *Encoder) release() {
func (e *Encoder) reset() { func (e *Encoder) reset() {
e.buf = e.buf[:0] e.buf = e.buf[:0]
e.indent = 0
e.enabledHTMLEscape = true
e.enabledIndent = false
} }
func (e *Encoder) encodeForMarshal(v interface{}) ([]byte, error) { func (e *Encoder) encodeForMarshal(v interface{}) ([]byte, error) {
@ -225,9 +228,16 @@ func (e *Encoder) encodeBytes(b []byte) {
e.buf = append(e.buf, b...) e.buf = append(e.buf, b...)
} }
func (e *Encoder) encodeNull() {
e.buf = append(e.buf, 'n', 'u', 'l', 'l')
}
func (e *Encoder) encodeString(s string) { func (e *Encoder) encodeString(s string) {
b := *(*[]byte)(unsafe.Pointer(&s)) if e.enabledHTMLEscape {
e.buf = append(e.buf, b...) e.encodeEscapedString(s)
} else {
e.encodeNoEscapedString(s)
}
} }
func (e *Encoder) encodeByte(b byte) { func (e *Encoder) encodeByte(b byte) {

View File

@ -305,3 +305,67 @@ func (e *Encoder) writeStringSlowPathWithHTMLEscaped(i int, s string, valLen int
} }
e.buf = append(e.buf, '"') e.buf = append(e.buf, '"')
} }
func (e *Encoder) encodeNoEscapedString(s string) {
valLen := len(s)
e.buf = append(e.buf, '"')
// write string, the fast path, without utf8 and escape support
i := 0
for ; i < valLen; i++ {
c := s[i]
if c > 31 && c != '"' && c != '\\' {
e.buf = append(e.buf, c)
} else {
break
}
}
if i == valLen {
e.buf = append(e.buf, '"')
return
}
e.writeStringSlowPath(i, s, valLen)
}
func (e *Encoder) writeStringSlowPath(i int, s string, valLen int) {
start := i
// for the remaining parts, we process them char by char
for i < valLen {
if b := s[i]; b < utf8.RuneSelf {
if safeSet[b] {
i++
continue
}
if start < i {
e.buf = append(e.buf, s[start:i]...)
}
switch b {
case '\\', '"':
e.buf = append(e.buf, '\\', b)
case '\n':
e.buf = append(e.buf, '\\', 'n')
case '\r':
e.buf = append(e.buf, '\\', 'r')
case '\t':
e.buf = append(e.buf, '\\', 't')
default:
// This encodes bytes < 0x20 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
// and served to some browsers.
e.buf = append(e.buf, []byte(`\u00`)...)
e.buf = append(e.buf, hex[b>>4], hex[b&0xF])
}
i++
start = i
continue
}
i++
continue
}
if start < len(s) {
e.buf = append(e.buf, s[start:]...)
}
e.buf = append(e.buf, '"')
}

View File

@ -49,7 +49,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeFloat64(e.ptrToFloat64(code.ptr)) e.encodeFloat64(e.ptrToFloat64(code.ptr))
code = code.next code = code.next
case opString: case opString:
e.encodeEscapedString(e.ptrToString(code.ptr)) e.encodeString(e.ptrToString(code.ptr))
code = code.next code = code.next
case opBool: case opBool:
e.encodeBool(e.ptrToBool(code.ptr)) e.encodeBool(e.ptrToBool(code.ptr))
@ -78,7 +78,7 @@ func (e *Encoder) run(code *opcode) error {
p := code.ptr p := code.ptr
headerCode := code.toSliceHeaderCode() headerCode := code.toSliceHeaderCode()
if p == 0 { if p == 0 {
e.encodeString("null") e.encodeNull()
code = headerCode.end.next code = headerCode.end.next
} else { } else {
e.encodeByte('[') e.encodeByte('[')
@ -108,7 +108,7 @@ func (e *Encoder) run(code *opcode) error {
headerCode := code.toSliceHeaderCode() headerCode := code.toSliceHeaderCode()
if p == 0 { if p == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = headerCode.end.next code = headerCode.end.next
} else { } else {
e.encodeBytes([]byte{'[', '\n'}) e.encodeBytes([]byte{'[', '\n'})
@ -143,7 +143,7 @@ func (e *Encoder) run(code *opcode) error {
p := code.ptr p := code.ptr
headerCode := code.toArrayHeaderCode() headerCode := code.toArrayHeaderCode()
if p == 0 { if p == 0 {
e.encodeString("null") e.encodeNull()
code = headerCode.end.next code = headerCode.end.next
} else { } else {
e.encodeByte('[') e.encodeByte('[')
@ -172,7 +172,7 @@ func (e *Encoder) run(code *opcode) error {
headerCode := code.toArrayHeaderCode() headerCode := code.toArrayHeaderCode()
if p == 0 { if p == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = headerCode.end.next code = headerCode.end.next
} else { } else {
e.encodeBytes([]byte{'[', '\n'}) e.encodeBytes([]byte{'[', '\n'})
@ -205,7 +205,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := code.ptr ptr := code.ptr
mapHeadCode := code.toMapHeadCode() mapHeadCode := code.toMapHeadCode()
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = mapHeadCode.end.next code = mapHeadCode.end.next
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -246,7 +246,7 @@ func (e *Encoder) run(code *opcode) error {
mapHeadCode := code.toMapHeadCode() mapHeadCode := code.toMapHeadCode()
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = mapHeadCode.end.next code = mapHeadCode.end.next
} else { } else {
e.encodeBytes([]byte{'{', '\n'}) e.encodeBytes([]byte{'{', '\n'})
@ -297,7 +297,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -313,7 +313,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -329,7 +329,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -345,7 +345,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -361,7 +361,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -377,7 +377,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -393,7 +393,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -409,7 +409,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -425,7 +425,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -441,7 +441,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -457,7 +457,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -473,7 +473,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -489,7 +489,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -505,12 +505,12 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeEscapedString(e.ptrToString(field.ptr + field.offset)) e.encodeString(e.ptrToString(field.ptr + field.offset))
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
code = field.next code = field.next
} }
@ -521,7 +521,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeByte('{') e.encodeByte('{')
@ -541,7 +541,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -561,7 +561,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeBytes([]byte{'{', '\n'}) e.encodeBytes([]byte{'{', '\n'})
@ -580,7 +580,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -599,7 +599,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -619,7 +619,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -639,7 +639,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -659,7 +659,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -679,7 +679,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -699,7 +699,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -719,7 +719,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -739,7 +739,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -759,7 +759,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -779,7 +779,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -799,7 +799,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -807,7 +807,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeIndent(code.indent + 1) e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeByte(' ') e.encodeByte(' ')
e.encodeEscapedString(e.ptrToString(field.ptr + field.offset)) e.encodeString(e.ptrToString(field.ptr + field.offset))
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
code = field.next code = field.next
} }
@ -819,7 +819,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end code = field.end
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -840,7 +840,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -865,7 +865,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -890,7 +890,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -915,7 +915,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -940,7 +940,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -965,7 +965,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -990,7 +990,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1015,7 +1015,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1040,7 +1040,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1065,7 +1065,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1090,7 +1090,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1115,7 +1115,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1140,7 +1140,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1165,7 +1165,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1176,7 +1176,7 @@ func (e *Encoder) run(code *opcode) error {
} else { } else {
e.encodeIndent(code.indent + 1) e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeEscapedString(v) e.encodeString(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
@ -1190,7 +1190,7 @@ func (e *Encoder) run(code *opcode) error {
field := code.toStructFieldCode() field := code.toStructFieldCode()
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1217,7 +1217,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1244,7 +1244,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1271,7 +1271,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1298,7 +1298,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1325,7 +1325,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1352,7 +1352,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1379,7 +1379,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1406,7 +1406,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1433,7 +1433,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1460,7 +1460,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1487,7 +1487,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1514,7 +1514,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1541,7 +1541,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1568,7 +1568,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1580,7 +1580,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeIndent(code.indent + 1) e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeByte(' ') e.encodeByte(' ')
e.encodeEscapedString(v) e.encodeString(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
@ -1595,7 +1595,7 @@ func (e *Encoder) run(code *opcode) error {
ptr := field.ptr ptr := field.ptr
if ptr == 0 { if ptr == 0 {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeString("null") e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
@ -1708,7 +1708,7 @@ func (e *Encoder) run(code *opcode) error {
c := code.toStructFieldCode() c := code.toStructFieldCode()
c.nextField.ptr = c.ptr c.nextField.ptr = c.ptr
e.encodeBytes(c.key) e.encodeBytes(c.key)
e.encodeEscapedString(e.ptrToString(c.ptr + c.offset)) e.encodeString(e.ptrToString(c.ptr + c.offset))
code = code.next code = code.next
case opStructFieldBool: case opStructFieldBool:
e.encodeByte(',') e.encodeByte(',')
@ -1841,7 +1841,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeIndent(c.indent) e.encodeIndent(c.indent)
e.encodeBytes(c.key) e.encodeBytes(c.key)
e.encodeByte(' ') e.encodeByte(' ')
e.encodeEscapedString(e.ptrToString(c.ptr + c.offset)) e.encodeString(e.ptrToString(c.ptr + c.offset))
code = code.next code = code.next
c.nextField.ptr = c.ptr c.nextField.ptr = c.ptr
case opStructFieldBoolIndent: case opStructFieldBoolIndent:
@ -2019,7 +2019,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeByte(',') e.encodeByte(',')
} }
e.encodeBytes(c.key) e.encodeBytes(c.key)
e.encodeEscapedString(v) e.encodeString(v)
} }
code = code.next code = code.next
code.ptr = c.ptr code.ptr = c.ptr
@ -2230,7 +2230,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeIndent(c.indent) e.encodeIndent(c.indent)
e.encodeBytes(c.key) e.encodeBytes(c.key)
e.encodeByte(' ') e.encodeByte(' ')
e.encodeEscapedString(v) e.encodeString(v)
} }
code = code.next code = code.next
code.ptr = c.ptr code.ptr = c.ptr

View File

@ -5,7 +5,6 @@ import "bytes"
func Marshal(v interface{}) ([]byte, error) { func Marshal(v interface{}) ([]byte, error) {
var b *bytes.Buffer var b *bytes.Buffer
enc := NewEncoder(b) enc := NewEncoder(b)
enc.SetIndent("", "")
bytes, err := enc.encodeForMarshal(v) bytes, err := enc.encodeForMarshal(v)
if err != nil { if err != nil {
enc.release() enc.release()