mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
Merge pull request #188 from ggaaooppeenngg/compatible
Fix standard compatiblility
This commit is contained in:
commit
06b2a7cf1d
20
compatible_test.go
Normal file
20
compatible_test.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Standard Encoder has trailing newline.
|
||||||
|
func TestEncoderHasTrailingNewline(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var buf, stdbuf bytes.Buffer
|
||||||
|
enc := ConfigCompatibleWithStandardLibrary.NewEncoder(&buf)
|
||||||
|
enc.Encode(1)
|
||||||
|
stdenc := json.NewEncoder(&stdbuf)
|
||||||
|
stdenc.Encode(1)
|
||||||
|
should.Equal(stdbuf.Bytes(), buf.Bytes())
|
||||||
|
}
|
@ -110,6 +110,9 @@ type Encoder struct {
|
|||||||
// Encode encode interface{} as JSON to io.Writer
|
// Encode encode interface{} as JSON to io.Writer
|
||||||
func (adapter *Encoder) Encode(val interface{}) error {
|
func (adapter *Encoder) Encode(val interface{}) error {
|
||||||
adapter.stream.WriteVal(val)
|
adapter.stream.WriteVal(val)
|
||||||
|
if adapter.stream.cfg.addNewline {
|
||||||
|
adapter.stream.WriteRaw("\n")
|
||||||
|
}
|
||||||
adapter.stream.Flush()
|
adapter.stream.Flush()
|
||||||
return adapter.stream.Error
|
return adapter.stream.Error
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,13 @@ type Config struct {
|
|||||||
UseNumber bool
|
UseNumber bool
|
||||||
TagKey string
|
TagKey string
|
||||||
ValidateJsonRawMessage bool
|
ValidateJsonRawMessage bool
|
||||||
|
AddNewline bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type frozenConfig struct {
|
type frozenConfig struct {
|
||||||
configBeforeFrozen Config
|
configBeforeFrozen Config
|
||||||
sortMapKeys bool
|
sortMapKeys bool
|
||||||
|
addNewline bool
|
||||||
indentionStep int
|
indentionStep int
|
||||||
decoderCache unsafe.Pointer
|
decoderCache unsafe.Pointer
|
||||||
encoderCache unsafe.Pointer
|
encoderCache unsafe.Pointer
|
||||||
@ -58,6 +60,7 @@ var ConfigCompatibleWithStandardLibrary = Config{
|
|||||||
EscapeHTML: true,
|
EscapeHTML: true,
|
||||||
SortMapKeys: true,
|
SortMapKeys: true,
|
||||||
ValidateJsonRawMessage: true,
|
ValidateJsonRawMessage: true,
|
||||||
|
AddNewline: true,
|
||||||
}.Froze()
|
}.Froze()
|
||||||
|
|
||||||
// ConfigFastest marshals float with only 6 digits precision
|
// ConfigFastest marshals float with only 6 digits precision
|
||||||
@ -72,6 +75,7 @@ func (cfg Config) Froze() API {
|
|||||||
frozenConfig := &frozenConfig{
|
frozenConfig := &frozenConfig{
|
||||||
sortMapKeys: cfg.SortMapKeys,
|
sortMapKeys: cfg.SortMapKeys,
|
||||||
indentionStep: cfg.IndentionStep,
|
indentionStep: cfg.IndentionStep,
|
||||||
|
addNewline: cfg.AddNewline,
|
||||||
streamPool: make(chan *Stream, 16),
|
streamPool: make(chan *Stream, 16),
|
||||||
iteratorPool: make(chan *Iterator, 16),
|
iteratorPool: make(chan *Iterator, 16),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user