Automatically generate structs from json
Apollo fe7566d756 Updated README | 2 tahun lalu | |
---|---|---|
examples | 2 tahun lalu | |
README.md | 2 tahun lalu | |
autostruct.go | 2 tahun lalu | |
go.mod | 2 tahun lalu | |
sample.json | 2 tahun lalu | |
utils.go | 2 tahun lalu |
Given unknown json but want nice clean structures to load it consistently?
AutoStruct can help with that.
AutoStruct was designed to aid those who had lots of json that they wanted to properly load into json, but found that the json package doesn't support dynamically loading (especially nested json like {}
s or []
s), AutoStruct catches most cases and will attempt to use a best fix method (if it can use for example []string
or []int
it will, but if the types vary it has no choice but to make a separate structure to handle that).
It is always recommended to check the output of AutoStruct and make changes as needed. (Also note that if you changed something you might want to make a note of that in another file because AutoStruct will most likely eat any changes and default back to the original output if run again)
There are currently 2 examples to AutoStruct.
There is main.go
in the examples directory, issuing go run main.go
should cause a fresh copy of example.go to be produced.
This example shows that go code can now write structures and import and define the package.
There is main2.go
in the examples directory, issuing go run main2.go
should cause a fresh copy of example2.go to be produced from the json input simple.json
.
This example shows that we can have go code generated from json.
This example also shows that AutoStruct is guessing the type of the JSON, it really doesn't know, as such if it's not sure it will most likely end up with a type
interface{}
, just to prove that this isn't to replace the programmer, just to reduce the hours spent making the structures of the json being given.