Похожие чаты

Gophers, how do I json.Marshal the output in my json

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"
}

12 ответов

10 просмотров

how are you expecting a non-empty array when the slice is nil?

jeffotoni- Автор вопроса
Roman Sharkov
how are you expecting a non-empty array when the s...

all empty, I need to mount the empty struct json. I'm seeing your customize the MarshalJSON

jeffotoni- Автор вопроса

golang's stdlib encoding/json library currently marshals empty arrays as null rather than []

jeffotoni- Автор вопроса

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 😔😔

jeffotoni- Автор вопроса
Roman Sharkov
how are you expecting a non-empty array when the s...

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":""}]}

jeffotoni- Автор вопроса
Roman Sharkov
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 .

jeffotoni
in your example I will always receive like this Fo...

can you create a playground example? I honestly don't understand the problem

jeffotoni- Автор вопроса
Edwing123
Wow that's kind of ugly

no, it doesn't. It marshals nil as null

jeffotoni- Автор вопроса
Edwing123
Wow that's kind of ugly

a lot, how would you do it?

Похожие вопросы

Обсуждают сегодня

Здравствуйте! Есть вопросы по ПК: 1.1. Безопасно ли запускать программы, которым я не доверяю, в виртуальной машине под KVM+qemu? (x86_64) 1.2. То же самое при использовании ...
Pavel
5
Hi everyone! I have a two-folded message to share today. First: updates and news, as we are being asked for those. We still want to go like we did the last 6 years: we w...
Marten | Unibright.io
31
У меня вопрос, а какой уровень оптимизации ставить при сборке на i5 4 ядра 16G ddr3 ОЗУ и видюха Geforce GTX 970?
Sir Jiga
18
Guys, what’s the most expensive part of your budget for your business/startup/app/website?
Ice Kream
18
Скажите, можно ли как-то "переместить" динамический массив из одной переменной в другую? Скажем, переместить из TList<> в TArray<>. Именно переместить, а не скопировать. Если ...
Eugene Krasnikov (ᴊɪɴ x)
37
Happy Friday and happy CartesiWeekly! This week on the list: 🪲 Bug Buster Update 🎥 Erick de Moura’s interview with Ornella from Web3TV 👽 Thriving Cartesi alien 🇧🇷 SBRC hackat...
Nida | Cartesi - Never DM first or ask for funds!
1
Oh.... Admins at the main TG is already muted me. How many months Sir you muted me? Or forever if you are here @Janevietani @JohnnySonic
Charles Dar
28
комрады, че-та лыжы не едут var tmpFont: TFont; begin tmpFont:= TFont.Create; try case rgFontColor.ItemIndex of 0: tmpFont.Color:= clWindowText; 1: tmpFo...
Ed Doc
34
Вот еще криповенькая штука. uMain.pas(517,3) Warning: Case statement does not handle all possible cases И ЧО? 😂
Александр (Rouse_) Багель
20
Интересно, нет ли какого-то способа получить из dll не адрес самой метки, а адрес со смещением?
The Bird of Hermes
54
Карта сайта