Quellcode durchsuchen

#75 support MarshalIndent

Tao Wen vor 8 Jahren
Ursprung
Commit
678c297af3
4 geänderte Dateien mit 32 neuen und 7 gelöschten Zeilen
  1. 4 0
      feature_adapter.go
  2. 16 0
      feature_config.go
  3. 5 1
      feature_stream.go
  4. 7 6
      jsoniter_adapter_test.go

+ 4 - 0
feature_adapter.go

@@ -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)
 }

+ 16 - 0
feature_config.go

@@ -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)]

+ 5 - 1
feature_stream.go

@@ -225,7 +225,11 @@ func (stream *Stream) WriteObjectStart() {
 
 func (stream *Stream) WriteObjectField(field string) {
 	stream.WriteString(field)
-	stream.writeByte(':')
+	if stream.indention > 0 {
+		stream.writeTwoBytes(':', ' ')
+	} else {
+		stream.writeByte(':')
+	}
 }
 
 func (stream *Stream) WriteObjectEnd() {

+ 7 - 6
jsoniter_adapter_test.go

@@ -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))
 }