This stuff is easier than you might think. It's just not that well documented for the casual reader. I would recommend ffjson
over normal json
tho. It's made up in such a manner that you don't need to change your syntax other than the library name.
It's easy as this:
type User struct {
Id int `json:'id'`
Name string `json:name`
SomeId1 int `json:some_id_1`
SomeId2 int `json:some_id_2`
SomeId3 int `json:some_id_3`
SomeId4 int `json:some_id_4`
}
item := map[string]User{}
for i := 0; i < 10; i++ {
item[strconv.itoa(i)] = User{i, "Username X", 38393, 29384, 12393, 123981}
}
buf, err := ffjson.Marshal(&item)
The downside of structs (even in ffjson
still) is that reflection
will always be used, which, in times that you're in need of high performance, you will waste a lot of CPU cycles. ffjson
is 2-3 times faster than normal json
when you keep it to maps. This way the library can compile every data-structure you marshal and re-use it instead of constantly inspecting the data-integrity/structure with reflect
.