mirror of https://github.com/goccy/go-json.git
Fix indent encoding
This commit is contained in:
parent
75f34df1c6
commit
689587cd72
|
@ -22,6 +22,7 @@ type Encoder struct {
|
||||||
enabledIndent bool
|
enabledIndent bool
|
||||||
enabledHTMLEscape bool
|
enabledHTMLEscape bool
|
||||||
unorderedMap bool
|
unorderedMap bool
|
||||||
|
baseIndent int
|
||||||
prefix []byte
|
prefix []byte
|
||||||
indentStr []byte
|
indentStr []byte
|
||||||
}
|
}
|
||||||
|
@ -156,6 +157,7 @@ func (e *Encoder) release() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) reset() {
|
func (e *Encoder) reset() {
|
||||||
|
e.baseIndent = 0
|
||||||
e.enabledHTMLEscape = true
|
e.enabledHTMLEscape = true
|
||||||
e.enabledIndent = false
|
e.enabledIndent = false
|
||||||
e.unorderedMap = false
|
e.unorderedMap = false
|
||||||
|
@ -298,7 +300,7 @@ func appendStructEnd(b []byte) []byte {
|
||||||
func (e *Encoder) appendStructEndIndent(b []byte, indent int) []byte {
|
func (e *Encoder) appendStructEndIndent(b []byte, indent int) []byte {
|
||||||
b = append(b, '\n')
|
b = append(b, '\n')
|
||||||
b = append(b, e.prefix...)
|
b = append(b, e.prefix...)
|
||||||
b = append(b, bytes.Repeat(e.indentStr, indent)...)
|
b = append(b, bytes.Repeat(e.indentStr, e.baseIndent+indent)...)
|
||||||
return append(b, '}', ',', '\n')
|
return append(b, '}', ',', '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,5 +321,5 @@ func encodeByteSlice(b []byte, src []byte) []byte {
|
||||||
|
|
||||||
func (e *Encoder) encodeIndent(b []byte, indent int) []byte {
|
func (e *Encoder) encodeIndent(b []byte, indent int) []byte {
|
||||||
b = append(b, e.prefix...)
|
b = append(b, e.prefix...)
|
||||||
return append(b, bytes.Repeat(e.indentStr, indent)...)
|
return append(b, bytes.Repeat(e.indentStr, e.baseIndent+indent)...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -237,7 +237,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
|
||||||
|
|
||||||
ctx.ptrs = newPtrs
|
ctx.ptrs = newPtrs
|
||||||
|
|
||||||
bb, err := e.runEscaped(ctx, b, ifaceCodeSet)
|
bb, err := e.run(ctx, b, ifaceCodeSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -198,10 +198,13 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
|
||||||
|
|
||||||
ctx.ptrs = newPtrs
|
ctx.ptrs = newPtrs
|
||||||
|
|
||||||
bb, err := e.runEscaped(ctx, b, ifaceCodeSet)
|
oldBaseIndent := e.baseIndent
|
||||||
|
e.baseIndent = code.indent
|
||||||
|
bb, err := e.runEscapedIndent(ctx, b, ifaceCodeSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
e.baseIndent = oldBaseIndent
|
||||||
|
|
||||||
ctx.ptrs = oldPtrs
|
ctx.ptrs = oldPtrs
|
||||||
ctxptr = ctx.ptr()
|
ctxptr = ctx.ptr()
|
||||||
|
@ -233,7 +236,7 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
|
||||||
if err := encodeWithIndent(
|
if err := encodeWithIndent(
|
||||||
&buf,
|
&buf,
|
||||||
bb,
|
bb,
|
||||||
string(e.prefix)+string(bytes.Repeat(e.indentStr, code.indent)),
|
string(e.prefix)+string(bytes.Repeat(e.indentStr, e.baseIndent+code.indent)),
|
||||||
string(e.indentStr),
|
string(e.indentStr),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -543,9 +546,10 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
|
||||||
})
|
})
|
||||||
buf := b[pos[0]:]
|
buf := b[pos[0]:]
|
||||||
buf = buf[:0]
|
buf = buf[:0]
|
||||||
|
|
||||||
for _, kv := range kvs {
|
for _, kv := range kvs {
|
||||||
buf = append(buf, e.prefix...)
|
buf = append(buf, e.prefix...)
|
||||||
buf = append(buf, bytes.Repeat(e.indentStr, code.indent+1)...)
|
buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent+1)...)
|
||||||
|
|
||||||
buf = append(buf, []byte(kv.key)...)
|
buf = append(buf, []byte(kv.key)...)
|
||||||
buf[len(buf)-2] = ':'
|
buf[len(buf)-2] = ':'
|
||||||
|
@ -555,7 +559,7 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
|
||||||
buf = buf[:len(buf)-2]
|
buf = buf[:len(buf)-2]
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
buf = append(buf, e.prefix...)
|
buf = append(buf, e.prefix...)
|
||||||
buf = append(buf, bytes.Repeat(e.indentStr, code.indent)...)
|
buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent)...)
|
||||||
buf = append(buf, '}', ',', '\n')
|
buf = append(buf, '}', ',', '\n')
|
||||||
b = b[:pos[0]]
|
b = b[:pos[0]]
|
||||||
b = append(b, buf...)
|
b = append(b, buf...)
|
||||||
|
|
|
@ -198,10 +198,13 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
|
||||||
|
|
||||||
ctx.ptrs = newPtrs
|
ctx.ptrs = newPtrs
|
||||||
|
|
||||||
bb, err := e.runEscaped(ctx, b, ifaceCodeSet)
|
oldBaseIndent := e.baseIndent
|
||||||
|
e.baseIndent = code.indent
|
||||||
|
bb, err := e.runIndent(ctx, b, ifaceCodeSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
e.baseIndent = oldBaseIndent
|
||||||
|
|
||||||
ctx.ptrs = oldPtrs
|
ctx.ptrs = oldPtrs
|
||||||
ctxptr = ctx.ptr()
|
ctxptr = ctx.ptr()
|
||||||
|
@ -233,7 +236,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
|
||||||
if err := encodeWithIndent(
|
if err := encodeWithIndent(
|
||||||
&buf,
|
&buf,
|
||||||
bb,
|
bb,
|
||||||
string(e.prefix)+string(bytes.Repeat(e.indentStr, code.indent)),
|
string(e.prefix)+string(bytes.Repeat(e.indentStr, e.baseIndent+code.indent)),
|
||||||
string(e.indentStr),
|
string(e.indentStr),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -545,7 +548,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
|
||||||
buf = buf[:0]
|
buf = buf[:0]
|
||||||
for _, kv := range kvs {
|
for _, kv := range kvs {
|
||||||
buf = append(buf, e.prefix...)
|
buf = append(buf, e.prefix...)
|
||||||
buf = append(buf, bytes.Repeat(e.indentStr, code.indent+1)...)
|
buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent+1)...)
|
||||||
|
|
||||||
buf = append(buf, []byte(kv.key)...)
|
buf = append(buf, []byte(kv.key)...)
|
||||||
buf[len(buf)-2] = ':'
|
buf[len(buf)-2] = ':'
|
||||||
|
@ -555,7 +558,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
|
||||||
buf = buf[:len(buf)-2]
|
buf = buf[:len(buf)-2]
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
buf = append(buf, e.prefix...)
|
buf = append(buf, e.prefix...)
|
||||||
buf = append(buf, bytes.Repeat(e.indentStr, code.indent)...)
|
buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent)...)
|
||||||
buf = append(buf, '}', ',', '\n')
|
buf = append(buf, '}', ',', '\n')
|
||||||
b = b[:pos[0]]
|
b = b[:pos[0]]
|
||||||
b = append(b, buf...)
|
b = append(b, buf...)
|
||||||
|
|
Loading…
Reference in New Issue