I need to show json's empty object {} when do json.Marshal() for a struct pointer. I can only output either null value or empty struct value.
If the person key is filled with &Person{} or new(Person), it will show empty struct like below:
{
"data": {
"person": {
"name": "",
"age": 0
},
"created_date": "2009-11-10T23:00:00Z"
}
}
And if we don't initialize it at all, it will show null.
{
"data": {
"person": null,
"created_date": "2009-11-10T23:00:00Z"
}
}
I want to show "person": {}. Is it possible?
Go Playground for the complete code: https://play.golang.org/p/tT15G2ESPVc
omitemptyfor thePerson's fields. play.golang.org/p/o3jWdru_8bCPersonpointer type, like so: play.golang.org/p/EKQc7uf1_Vkomitemptyfor thePerson's fields has worked and by far the simplest. But it will affect myPerson's fields encoding when I want to keep showing, for example,Person.namein the marshal result when it is empty