go-json icon indicating copy to clipboard operation
go-json copied to clipboard

Panic

Open kaibox-git opened this issue 2 years ago • 1 comments

I cut structures for readability. It works with "encoding/json", but panics with "github.com/goccy/go-json"

package main

import (
	"github.com/goccy/go-json"
)

// "encoding/json"
// "github.com/goccy/go-json"

type Auth struct {
	Id                  int                  `json:"id"`
	Login               string               `json:"login"`
	CurrentOrganization *CurrentOrganization `json:"organization,omitempty" db:"organization"`
}

type CurrentOrganization struct {
	Id int `json:"id"`
}

type User struct {
	Auth
}

type AddUser struct {
	User
}

func main() {
	user := new(AddUser)
	user.Login = "user756"

	jsonUser, err := json.Marshal(user)
	if err != nil {
		println("error:", err.Error())
	}
	println("user:", jsonUser)
}

kaibox-git avatar Sep 07 '22 09:09 kaibox-git

I understand, "omitempty" is the reason.

kaibox-git avatar Sep 07 '22 09:09 kaibox-git

Thank you for your reports. This problem was fixed with https://github.com/goccy/go-json/pull/438

goccy avatar Mar 13 '23 11:03 goccy