mirror of
https://github.com/json-iterator/go.git
synced 2025-03-20 20:54:55 +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 {
|
||||
stream := NewStream(DEFAULT_CONFIG, writer, 512)
|
||||
newCfg := &Config{}
|
||||
initConfig(newCfg)
|
||||
stream := NewStream(newCfg, writer, 512)
|
||||
return &AdaptedEncoder{stream}
|
||||
}
|
||||
|
||||
@ -187,5 +189,5 @@ func (adapter *AdaptedEncoder) Encode(val interface{}) error {
|
||||
}
|
||||
|
||||
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 {
|
||||
IndentionStep int
|
||||
decoderCache unsafe.Pointer
|
||||
encoderCache unsafe.Pointer
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ type Stream struct {
|
||||
n int
|
||||
Error error
|
||||
indention int
|
||||
IndentionStep int
|
||||
}
|
||||
|
||||
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,
|
||||
Error: nil,
|
||||
indention: 0,
|
||||
IndentionStep: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,7 +276,7 @@ func (stream *Stream) WriteBool(val bool) {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteObjectStart() {
|
||||
stream.indention += stream.IndentionStep
|
||||
stream.indention += stream.cfg.IndentionStep
|
||||
stream.writeByte('{')
|
||||
stream.writeIndention(0)
|
||||
}
|
||||
@ -289,8 +287,8 @@ func (stream *Stream) WriteObjectField(field string) {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteObjectEnd() {
|
||||
stream.writeIndention(stream.IndentionStep)
|
||||
stream.indention -= stream.IndentionStep
|
||||
stream.writeIndention(stream.cfg.IndentionStep)
|
||||
stream.indention -= stream.cfg.IndentionStep
|
||||
stream.writeByte('}')
|
||||
}
|
||||
|
||||
@ -305,7 +303,7 @@ func (stream *Stream) WriteMore() {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteArrayStart() {
|
||||
stream.indention += stream.IndentionStep
|
||||
stream.indention += stream.cfg.IndentionStep
|
||||
stream.writeByte('[')
|
||||
stream.writeIndention(0)
|
||||
}
|
||||
@ -316,8 +314,8 @@ func (stream *Stream) WriteEmptyArray() {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteArrayEnd() {
|
||||
stream.writeIndention(stream.IndentionStep)
|
||||
stream.indention -= stream.IndentionStep
|
||||
stream.writeIndention(stream.cfg.IndentionStep)
|
||||
stream.indention -= stream.cfg.IndentionStep
|
||||
stream.writeByte(']')
|
||||
}
|
||||
|
||||
|
@ -213,8 +213,9 @@ func Test_whitespace_before_comma(t *testing.T) {
|
||||
func Test_write_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.IndentionStep = 2
|
||||
newCfg := &Config{IndentionStep: 2}
|
||||
initConfig(newCfg)
|
||||
stream := NewStream(newCfg, buf, 4096)
|
||||
stream.WriteArrayStart()
|
||||
stream.WriteInt(1)
|
||||
stream.WriteMore()
|
||||
|
@ -210,8 +210,9 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
func Test_write_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.IndentionStep = 2
|
||||
newCfg := &Config{IndentionStep: 2}
|
||||
initConfig(newCfg)
|
||||
stream := NewStream(newCfg, buf, 4096)
|
||||
stream.WriteObjectStart()
|
||||
stream.WriteObjectField("hello")
|
||||
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) {
|
||||
should := require.New(t)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 1)
|
||||
stream.IndentionStep = 2
|
||||
newCfg := &Config{IndentionStep: 2}
|
||||
initConfig(newCfg)
|
||||
stream := NewStream(newCfg, nil, 1)
|
||||
stream.WriteVal([]int{1, 2, 3})
|
||||
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user