Skip to content

Commit

Permalink
Merge pull request #377 from orisano/fix/#376
Browse files Browse the repository at this point in the history
Fix encoding of directed interface with typed nil
  • Loading branch information
goccy committed Jul 4, 2022
2 parents 27bd0f2 + 884b8db commit 1468eef
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
18 changes: 18 additions & 0 deletions encode_test.go
Expand Up @@ -2406,3 +2406,21 @@ func TestIssue339(t *testing.T) {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}

func TestIssue376(t *testing.T) {
type Container struct {
V interface{} `json:"value"`
}
type MapOnly struct {
Map map[string]int64 `json:"map"`
}
b, err := json.Marshal(Container{MapOnly{}})
if err != nil {
t.Fatal(err)
}
got := string(b)
expected := `{"value":{"map":null}}`
if got != expected {
t.Errorf("unexpected result: %v != %v", got, expected)
}
}
10 changes: 7 additions & 3 deletions internal/cmd/generator/vm.go.tmpl
Expand Up @@ -3,6 +3,7 @@ package vm

import (
"math"
"reflect"
"sort"
"unsafe"

Expand Down Expand Up @@ -194,9 +195,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
typ = iface.typ
}
if ifacePtr == nil {
b = appendNullComma(ctx, b)
code = code.Next
break
isDirectedNil := typ != nil && typ.Kind() == reflect.Struct && !runtime.IfaceIndir(typ)
if !isDirectedNil {
b = appendNullComma(ctx, b)
code = code.Next
break
}
}
ctx.KeepRefs = append(ctx.KeepRefs, up)
ifaceCodeSet, err := encoder.CompileToGetCodeSet(ctx, uintptr(unsafe.Pointer(typ)))
Expand Down
10 changes: 7 additions & 3 deletions internal/encoder/vm/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions internal/encoder/vm_color/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions internal/encoder/vm_color_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions internal/encoder/vm_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1468eef

Please sign in to comment.