Reputation: 2075
I want to dynamically define structs in a Go project based on a JSON file.
For example, if I had a json file like so...
{
"date": "today",
"time": 12,
"era": "never",
"alive": true
}
Then I would expect a struct to be generated (that would look) like this (but not explicitly defined in the source code)...
type DynamicJSON struct {
date, era string
time int
alive bool
}
Furthermore, I want to nest JSON objects such that I could do something like this...
{
"date": "today",
"time": 12,
"era": "never",
"alive": true,
"nested": {
"date": "tomorrow",
"alive": true
}
}
...which would actually generate two different structs, like this...
type DynamicJSON1 struct {
date, era string
time int
alive bool
}
type DynamicJSON2 struct {
date string
alive bool
}
Is this something that is currently supported?
Upvotes: 0
Views: 112
Reputation: 3537
I can't garantantee final result, but easyjson does exactly what you asking.
easyjson aims to keep generated Go code simple enough so that it can be easily optimized or fixed. Another goal is to provide users with the ability to customize the generated code by providing options not available with the standard encoding/json package, such as generating "snake_case" names or enabling omitempty behavior by default.
Upvotes: 2