twpayne / go-jsonstruct Goto Github PK
View Code? Open in Web Editor NEWGenerate Go structs from multiple JSON objects.
License: BSD 2-Clause "Simplified" License
Generate Go structs from multiple JSON objects.
License: BSD 2-Clause "Simplified" License
As identified by @breml at the Bärner Go Meetup:
encoding/json
supports a ,string
struct tag annotation that allows strings to be interpreted as int
s, float64
s, bool
s, and string
s. go-jsonstruct should support this annotation.
Currently all nested structs are generated as literal structs within the parent struct identical to the data being parsed. Is it possible to make go-jsonstruct extract such nested struct definitions to named structs which type is then used as a field instead of the nesting of the struct literals? This would make it easier in some cases to create instances for posting to REST-api's, as otherwise I have to repeat the nested type definition when specifying the value.
E.g based on input
{
"items": [
{
"datapoints": [
{
"timestamp": 1687907013216,
"value": 42.54
},
{
"timestamp": -149967180845,
"value": 1337.43
}
],
"id": 6361985487601820
}
]
}
currently this is generated
type InsertDataPointsRequest struct {
Items []struct {
Datapoints []struct {
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
} `json:"datapoints"`
ID int64 `json:"id"`
} `json:"items"`
}
But by exploding the nested structs it could look like this
type Datapoint struct {
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
}
type Item struct {
Datapoints []Datapoint `json:"datapoints"`
ID int64 `json:"id"`
}
type InsertDataPointsRequest struct {
Items []item `json:"items"`
}
Which would make the use of the code go from this
v := InsertDataPointsRequest{
Items: []struct {
Datapoints []struct {
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
} `json:"datapoints"`
ID int64 `json:"id"`
}{{
Datapoints: []struct {
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
}{
{
Timestamp: 12,
Value: 53,
},
},
ID: 42,
}},
}
}
to this
v := InsertDataPointsRequest{
Items: []Item{{
Datapoints: []Datapoint{
{
Timestamp: 12,
Value: 53,
},
},
ID: 42,
},
},
}
Can i'm used without command but just import and call the func ?
rationale: I would like to use go-jsonstruct in a //go:generate
-directive. However the documentation says:
The arguments to the directive are space-separated tokens or double-quoted strings passed to the generator as individual arguments when it is run.
So constructs like gojsonstruct < file.json
or cat file.json | gojsonstruct
will not work. Instead I have to use
//go:generate sh -c "gojsonstruct -packagename $GOPACKAGE < ./file.json"
Which is not portable. If gojsonstruct would be able to read from file the above generator statement could be made environment neutral.
Currently, go-jsonstruct is unable to distinguish between the following cases:
{"foo":"bar"} // present string value
{"foo":""} // present but empty string value
{"foo":null} // present null value
{} // absent value
go-jsonstruct should be able to handle these sorts of values, maybe using a technique similar to database/sql.NullString
.
Currently if go-jsonstruct sees both an int
and float64
it resolves the observed value as being a float64
. It should support json.Number
as an alternative option.
I am trying to communicate with an API that returns (and receives) integers may be up to Number.MAX_SAFE_INTEGER. This may overflow if one is compiling on a machine where go decides to use 32bit integers for int.
Could perhaps go-jsonstruct create such fields with int64 type instead?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.