mirror of
https://github.com/json-iterator/go.git
synced 2025-05-16 21:45:43 +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)
|
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) {
|
func MarshalToString(v interface{}) (string, error) {
|
||||||
return ConfigDefault.MarshalToString(v)
|
return ConfigDefault.MarshalToString(v)
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ type frozenConfig struct {
|
|||||||
type Api interface {
|
type Api interface {
|
||||||
MarshalToString(v interface{}) (string, error)
|
MarshalToString(v interface{}) (string, error)
|
||||||
Marshal(v interface{}) ([]byte, error)
|
Marshal(v interface{}) ([]byte, error)
|
||||||
|
MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
|
||||||
UnmarshalFromString(str string, v interface{}) error
|
UnmarshalFromString(str string, v interface{}) error
|
||||||
Unmarshal(data []byte, v interface{}) error
|
Unmarshal(data []byte, v interface{}) error
|
||||||
Get(data []byte, path ...interface{}) Any
|
Get(data []byte, path ...interface{}) Any
|
||||||
@ -54,6 +55,7 @@ var ConfigFastest = Config{
|
|||||||
}.Froze()
|
}.Froze()
|
||||||
|
|
||||||
func (cfg Config) Froze() *frozenConfig {
|
func (cfg Config) Froze() *frozenConfig {
|
||||||
|
// TODO: cache frozen config
|
||||||
frozenConfig := &frozenConfig{
|
frozenConfig := &frozenConfig{
|
||||||
sortMapKeys: cfg.SortMapKeys,
|
sortMapKeys: cfg.SortMapKeys,
|
||||||
indentionStep: cfg.IndentionStep,
|
indentionStep: cfg.IndentionStep,
|
||||||
@ -224,6 +226,20 @@ func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
|||||||
return copied, nil
|
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 {
|
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||||
data := []byte(str)
|
data := []byte(str)
|
||||||
data = data[:lastNotSpacePos(data)]
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
@ -225,7 +225,11 @@ func (stream *Stream) WriteObjectStart() {
|
|||||||
|
|
||||||
func (stream *Stream) WriteObjectField(field string) {
|
func (stream *Stream) WriteObjectField(field string) {
|
||||||
stream.WriteString(field)
|
stream.WriteString(field)
|
||||||
stream.writeByte(':')
|
if stream.indention > 0 {
|
||||||
|
stream.writeTwoBytes(':', ' ')
|
||||||
|
} else {
|
||||||
|
stream.writeByte(':')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectEnd() {
|
func (stream *Stream) WriteObjectEnd() {
|
||||||
|
@ -3,7 +3,6 @@ package jsoniter
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
@ -71,13 +70,15 @@ func Test_use_number_for_unmarshal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_marshal_indent(t *testing.T) {
|
func Test_marshal_indent(t *testing.T) {
|
||||||
t.Skip("WIP")
|
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
output, err := json.MarshalIndent(struct {
|
obj := struct {
|
||||||
F1 int
|
F1 int
|
||||||
F2 []int
|
F2 []int
|
||||||
}{1, []int{2, 3, 4}}, "", " ")
|
}{1, []int{2, 3, 4}}
|
||||||
|
output, err := json.MarshalIndent(obj, "", " ")
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
fmt.Println(string(output))
|
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||||
should.Equal("{\nab\"F1\": 1,\nab\"F2\": 2\na}", 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…
x
Reference in New Issue
Block a user