Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different output when using anonymous fields and omitempty tags #63

Open
gouyelliot opened this issue Mar 1, 2021 · 1 comment · May be fixed by #89
Open

Different output when using anonymous fields and omitempty tags #63

gouyelliot opened this issue Mar 1, 2021 · 1 comment · May be fixed by #89
Assignees

Comments

@gouyelliot
Copy link

Hello,

I recently tried using your library in one of our software, and I'd like to report an issue that we have when doing so.

The output when calling Marshal on encoding/json and segmentio/encoding/json differs.

I was able to reproduce the error using the following code:

package main

import (
	"encoding/json"
	"fmt"
	segmentio "github.com/segmentio/encoding/json"
)

type MyStruct struct {
	MyField string `json:"my_field,omitempty"`
}

type MyStruct2 struct {
	*MyStruct
	Code int `json:"code"`
}

func main() {
	value := MyStruct2{
		MyStruct: &MyStruct{
			MyField: "test",
		},
		Code: 0,
	}

	res, _ := json.Marshal(value)
	fmt.Println(string(res)) // Prints {"my_field":"test","code":0}

	res, _ = segmentio.Marshal(value)
	fmt.Println(string(res)) // Prints {"code":0} - Not correct

	value2 := MyStruct2{
		MyStruct: &MyStruct{
			MyField: "test",
		},
		Code:     10,
	}

	res, _ = json.Marshal(value2)
	fmt.Println(string(res)) // Prints {"my_field":"test","code":10}

	res, _ = segmentio.Marshal(value2)
	fmt.Println(string(res)) // Prints {"my_field":"test","code":10}
}

Note that when removing the omitempty tag from MyStruct.MyField, the output is correct.

Segmentio Encoding Version: 0.2.7
Golang Version: 1.15.7
OS: macOS Catalina 10.15.7

dominicbarnes added a commit that referenced this issue Mar 1, 2021
@dominicbarnes
Copy link

Sorry for the late follow-up here, I've been working on a solution to this as we definitely have bugs internally when it comes to the handling of embedded pointers specifically.

If you need a workaround right now, embedding the same struct without a pointer works as expected. I'll continue working on a proper solution to this problem in the meantime.

@dominicbarnes dominicbarnes self-assigned this Mar 11, 2021
dominicbarnes added a commit that referenced this issue Apr 29, 2021
buztard added a commit to geekspace9/commercetools-go-sdk that referenced this issue Sep 27, 2021
For maximum compatibility it's used only for unmarshaling at the
moment. The existing unit tests look good, benchmarks are promising.

name \ time/op     bench_encoding_json.txt  bench_go_json.txt  bench_jsoniter.txt  bench_segmentio.txt
UnmarshalOrders-8               185ms ± 0%          89ms ± 0%          116ms ± 0%            97ms ± 0%

name \ alloc/op    bench_encoding_json.txt  bench_go_json.txt  bench_jsoniter.txt  bench_segmentio.txt
UnmarshalOrders-8              19.1MB ± 0%        30.7MB ± 0%         27.2MB ± 0%          28.3MB ± 0%

name \ allocs/op   bench_encoding_json.txt  bench_go_json.txt  bench_jsoniter.txt  bench_segmentio.txt
UnmarshalOrders-8                388k ± 0%          335k ± 0%           686k ± 0%            332k ± 0%

Using this library for marshaling produces different results from
encoding/json due tue the handling of embedded pointers. See
segmentio/encoding#63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants