Skip to content

Different output when using anonymous fields and omitempty tags #63

Open
@gouyelliot

Description

@gouyelliot

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions