mirror of
https://github.com/json-iterator/go.git
synced 2025-05-13 21:36:29 +02:00
move IndentionStep to config
This commit is contained in:
parent
acddcf5bbf
commit
48e9f6ec84
@ -172,7 +172,9 @@ func (decoder *AdaptedDecoder) UseNumber() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
||||||
stream := NewStream(DEFAULT_CONFIG, writer, 512)
|
newCfg := &Config{}
|
||||||
|
initConfig(newCfg)
|
||||||
|
stream := NewStream(newCfg, writer, 512)
|
||||||
return &AdaptedEncoder{stream}
|
return &AdaptedEncoder{stream}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,5 +189,5 @@ func (adapter *AdaptedEncoder) Encode(val interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
|
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
|
||||||
adapter.stream.IndentionStep = len(indent)
|
adapter.stream.cfg.IndentionStep = len(indent)
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
IndentionStep int
|
||||||
decoderCache unsafe.Pointer
|
decoderCache unsafe.Pointer
|
||||||
encoderCache unsafe.Pointer
|
encoderCache unsafe.Pointer
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ type Stream struct {
|
|||||||
n int
|
n int
|
||||||
Error error
|
Error error
|
||||||
indention int
|
indention int
|
||||||
IndentionStep int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStream(cfg *Config, out io.Writer, bufSize int) *Stream {
|
func NewStream(cfg *Config, out io.Writer, bufSize int) *Stream {
|
||||||
@ -22,7 +21,6 @@ func NewStream(cfg *Config, out io.Writer, bufSize int) *Stream {
|
|||||||
n: 0,
|
n: 0,
|
||||||
Error: nil,
|
Error: nil,
|
||||||
indention: 0,
|
indention: 0,
|
||||||
IndentionStep: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +276,7 @@ func (stream *Stream) WriteBool(val bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectStart() {
|
func (stream *Stream) WriteObjectStart() {
|
||||||
stream.indention += stream.IndentionStep
|
stream.indention += stream.cfg.IndentionStep
|
||||||
stream.writeByte('{')
|
stream.writeByte('{')
|
||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
@ -289,8 +287,8 @@ func (stream *Stream) WriteObjectField(field string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectEnd() {
|
func (stream *Stream) WriteObjectEnd() {
|
||||||
stream.writeIndention(stream.IndentionStep)
|
stream.writeIndention(stream.cfg.IndentionStep)
|
||||||
stream.indention -= stream.IndentionStep
|
stream.indention -= stream.cfg.IndentionStep
|
||||||
stream.writeByte('}')
|
stream.writeByte('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,7 +303,7 @@ func (stream *Stream) WriteMore() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteArrayStart() {
|
func (stream *Stream) WriteArrayStart() {
|
||||||
stream.indention += stream.IndentionStep
|
stream.indention += stream.cfg.IndentionStep
|
||||||
stream.writeByte('[')
|
stream.writeByte('[')
|
||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
@ -316,8 +314,8 @@ func (stream *Stream) WriteEmptyArray() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteArrayEnd() {
|
func (stream *Stream) WriteArrayEnd() {
|
||||||
stream.writeIndention(stream.IndentionStep)
|
stream.writeIndention(stream.cfg.IndentionStep)
|
||||||
stream.indention -= stream.IndentionStep
|
stream.indention -= stream.cfg.IndentionStep
|
||||||
stream.writeByte(']')
|
stream.writeByte(']')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,8 +213,9 @@ func Test_whitespace_before_comma(t *testing.T) {
|
|||||||
func Test_write_array(t *testing.T) {
|
func Test_write_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
newCfg := &Config{IndentionStep: 2}
|
||||||
stream.IndentionStep = 2
|
initConfig(newCfg)
|
||||||
|
stream := NewStream(newCfg, buf, 4096)
|
||||||
stream.WriteArrayStart()
|
stream.WriteArrayStart()
|
||||||
stream.WriteInt(1)
|
stream.WriteInt(1)
|
||||||
stream.WriteMore()
|
stream.WriteMore()
|
||||||
|
@ -210,8 +210,9 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
|
|||||||
func Test_write_object(t *testing.T) {
|
func Test_write_object(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
newCfg := &Config{IndentionStep: 2}
|
||||||
stream.IndentionStep = 2
|
initConfig(newCfg)
|
||||||
|
stream := NewStream(newCfg, buf, 4096)
|
||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
stream.WriteObjectField("hello")
|
stream.WriteObjectField("hello")
|
||||||
stream.WriteInt(1)
|
stream.WriteInt(1)
|
||||||
|
@ -31,8 +31,9 @@ func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
|||||||
|
|
||||||
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
stream := NewStream(DEFAULT_CONFIG, nil, 1)
|
newCfg := &Config{IndentionStep: 2}
|
||||||
stream.IndentionStep = 2
|
initConfig(newCfg)
|
||||||
|
stream := NewStream(newCfg, nil, 1)
|
||||||
stream.WriteVal([]int{1, 2, 3})
|
stream.WriteVal([]int{1, 2, 3})
|
||||||
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user