of a struct array with no value?
out:
{"code":"","a":null}
but I would like to do this
{"code":"","a":[{"id":"","name":""}]}
type A struct {
ID string json:"id"
Name string json:"name"
}
type B struct {
Code string json:"code"
A []A json:"a"
}
how are you expecting a non-empty array when the slice is nil?
all empty, I need to mount the empty struct json. I'm seeing your customize the MarshalJSON
golang's stdlib encoding/json library currently marshals empty arrays as null rather than []
do you know a way around? I'm not finding any other solution than to declare the structure and append something empty to make it work 😔😔
The simplest solution I found was to make an append even empty so that Go does the allocation and thus the json of the output. b.A = append(b.A, a) {"code":"","a":[{"id":"","name":""}]}
https://go.dev/play/p/OW3MJNwjwE-
in your example I will always receive like this Foo{X: nil} so I always need to test if it's nil and if I need to tell Go that I want to print its struct .
can you create a playground example? I honestly don't understand the problem
https://go.dev/play/p/0kQ68cX7n3c
Wow that's kind of ugly
no, it doesn't. It marshals nil as null
a lot, how would you do it?
Обсуждают сегодня