mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
#75 support MarshalIndent
This commit is contained in:
parent
1253b8edd3
commit
678c297af3
@ -51,6 +51,10 @@ func Marshal(v interface{}) ([]byte, error) {
|
||||
return ConfigDefault.Marshal(v)
|
||||
}
|
||||
|
||||
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
||||
return ConfigDefault.MarshalIndent(v, prefix, indent)
|
||||
}
|
||||
|
||||
func MarshalToString(v interface{}) (string, error) {
|
||||
return ConfigDefault.MarshalToString(v)
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ type frozenConfig struct {
|
||||
type Api interface {
|
||||
MarshalToString(v interface{}) (string, error)
|
||||
Marshal(v interface{}) ([]byte, error)
|
||||
MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
|
||||
UnmarshalFromString(str string, v interface{}) error
|
||||
Unmarshal(data []byte, v interface{}) error
|
||||
Get(data []byte, path ...interface{}) Any
|
||||
@ -54,6 +55,7 @@ var ConfigFastest = Config{
|
||||
}.Froze()
|
||||
|
||||
func (cfg Config) Froze() *frozenConfig {
|
||||
// TODO: cache frozen config
|
||||
frozenConfig := &frozenConfig{
|
||||
sortMapKeys: cfg.SortMapKeys,
|
||||
indentionStep: cfg.IndentionStep,
|
||||
@ -224,6 +226,20 @@ func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
||||
return copied, nil
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
||||
if prefix != "" {
|
||||
panic("prefix is not supported")
|
||||
}
|
||||
for _, r := range indent {
|
||||
if r != ' ' {
|
||||
panic("indent can only be space")
|
||||
}
|
||||
}
|
||||
newCfg := cfg.configBeforeFrozen
|
||||
newCfg.IndentionStep = len(indent)
|
||||
return newCfg.Froze().Marshal(v)
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
|
@ -225,8 +225,12 @@ func (stream *Stream) WriteObjectStart() {
|
||||
|
||||
func (stream *Stream) WriteObjectField(field string) {
|
||||
stream.WriteString(field)
|
||||
if stream.indention > 0 {
|
||||
stream.writeTwoBytes(':', ' ')
|
||||
} else {
|
||||
stream.writeByte(':')
|
||||
}
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteObjectEnd() {
|
||||
stream.writeIndention(stream.cfg.indentionStep)
|
||||
|
@ -3,7 +3,6 @@ package jsoniter
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
@ -71,13 +70,15 @@ func Test_use_number_for_unmarshal(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_marshal_indent(t *testing.T) {
|
||||
t.Skip("WIP")
|
||||
should := require.New(t)
|
||||
output, err := json.MarshalIndent(struct {
|
||||
obj := struct {
|
||||
F1 int
|
||||
F2 []int
|
||||
}{1, []int{2, 3, 4}}, "", " ")
|
||||
}{1, []int{2, 3, 4}}
|
||||
output, err := json.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
fmt.Println(string(output))
|
||||
should.Equal("{\nab\"F1\": 1,\nab\"F2\": 2\na}", string(output))
|
||||
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||
output, err = MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user