Repository: goccy/go-json
Pull request #317 (2022-01-04 13:31:24)

Fix MarshalIndent for interface type
fix https://github.com/goccy/go-json/issues/316


Patch:
diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl
index 70a45e4..fb1ec4d 100644
--- a/internal/cmd/generator/vm.go.tmpl
+++ b/internal/cmd/generator/vm.go.tmpl
@@ -218,8 +218,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
 			oldOffset := ptrOffset
 			ptrOffset += totalLength * uintptrSize
 			oldBaseIndent := ctx.BaseIndent
-			indentDiffFromTop := c.Indent - 1
-			ctx.BaseIndent += code.Indent - indentDiffFromTop
+			ctx.BaseIndent += code.Indent
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go
index 70a45e4..fb1ec4d 100644
--- a/internal/encoder/vm/vm.go
+++ b/internal/encoder/vm/vm.go
@@ -218,8 +218,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
 			oldOffset := ptrOffset
 			ptrOffset += totalLength * uintptrSize
 			oldBaseIndent := ctx.BaseIndent
-			indentDiffFromTop := c.Indent - 1
-			ctx.BaseIndent += code.Indent - indentDiffFromTop
+			ctx.BaseIndent += code.Indent
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go
index 6461bb5..1ce3fc1 100644
--- a/internal/encoder/vm_color/vm.go
+++ b/internal/encoder/vm_color/vm.go
@@ -218,8 +218,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
 			oldOffset := ptrOffset
 			ptrOffset += totalLength * uintptrSize
 			oldBaseIndent := ctx.BaseIndent
-			indentDiffFromTop := c.Indent - 1
-			ctx.BaseIndent += code.Indent - indentDiffFromTop
+			ctx.BaseIndent += code.Indent
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go
index c428fbe..506c10c 100644
--- a/internal/encoder/vm_color_indent/vm.go
+++ b/internal/encoder/vm_color_indent/vm.go
@@ -218,8 +218,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
 			oldOffset := ptrOffset
 			ptrOffset += totalLength * uintptrSize
 			oldBaseIndent := ctx.BaseIndent
-			indentDiffFromTop := c.Indent - 1
-			ctx.BaseIndent += code.Indent - indentDiffFromTop
+			ctx.BaseIndent += code.Indent
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go
index 90ae850..5e17673 100644
--- a/internal/encoder/vm_indent/vm.go
+++ b/internal/encoder/vm_indent/vm.go
@@ -218,8 +218,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
 			oldOffset := ptrOffset
 			ptrOffset += totalLength * uintptrSize
 			oldBaseIndent := ctx.BaseIndent
-			indentDiffFromTop := c.Indent - 1
-			ctx.BaseIndent += code.Indent - indentDiffFromTop
+			ctx.BaseIndent += code.Indent
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {


Test patch:
diff --git a/encode_test.go b/encode_test.go
index d8b2b7f..d2cc4b3 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -2278,3 +2278,25 @@ func TestRecursivePtrHead(t *testing.T) {
 		t.Fatalf("failed to encode. expected %q but got %q", expected, got)
 	}
 }
+
+func TestMarshalIndent(t *testing.T) {
+	v := map[string]map[string]interface{}{
+		"a": {
+			"b": "1",
+			"c": map[string]interface{}{
+				"d": "1",
+			},
+		},
+	}
+	expected, err := stdjson.MarshalIndent(v, "", "    ")
+	if err != nil {
+		t.Fatal(err)
+	}
+	got, err := json.MarshalIndent(v, "", "    ")
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !bytes.Equal(expected, got) {
+		t.Fatalf("expected: %q but got %q", expected, got)
+	}
+}