objconv icon indicating copy to clipboard operation
objconv copied to clipboard

Unmarshaling JSON into embedded structs doesn't work

Open collinvandyck opened this issue 6 years ago • 0 comments

This test tries to deserialize JSON input into a struct which embeds another. There are two parts to this test:

  1. use normal encoding/json
  2. use objconv/json

The test fails on the objconv part:

import (
	"encoding/json"
	"testing"

	convjson "github.com/segmentio/objconv/json"
)

func TestObjConv(t *testing.T) {
	type inner struct {
		Value int64
	}
	type outer struct {
		inner
	}
	input := `{"value":42}`

	var res outer
	err := json.Unmarshal([]byte(input), &res)
	if err != nil {
		t.Fatal(err)
	}
	if res.Value != int64(42) {
		t.Fatal(res.Value)
	}

	res = outer{}
	err = convjson.Unmarshal([]byte(input), &res)
	if err != nil {
		t.Fatal(err)
	}
	if res.Value != int64(42) {
		t.Fatal(res.Value)
	}
}

collinvandyck avatar Aug 23 '18 21:08 collinvandyck