Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/json-iterator/tinygo
make json.Unmarshal work in tinygo
https://github.com/json-iterator/tinygo
Last synced: about 1 month ago
JSON representation
make json.Unmarshal work in tinygo
- Host: GitHub
- URL: https://github.com/json-iterator/tinygo
- Owner: json-iterator
- License: mit
- Created: 2021-12-01T07:42:16.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-21T07:20:01.000Z (about 3 years ago)
- Last Synced: 2024-06-21T18:56:47.956Z (6 months ago)
- Language: Go
- Size: 141 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# make `json.Unmarshal` work in tinygo
```go
type NamedArray = []stringtype RefNamedArray struct {
Value NamedArray
}import "encoding/json"
var val1 RefNamedArray
var val2 NamedArray
json.Unmarshal([]byte(`{ "Value": ["hello","world"] }`), &val1)
json.Unmarshal([]byte(`["hello","world"]`), &val2)
```The code above does not work in tinygo, due to incomplete runtime reflection support. To fix this, we use code generation instead of runtime reflection to implement `json.Unmarshal`
```go
//go:generate go run github.com/json-iterator/tinygo/gen
type NamedArray = []string//go:generate go run github.com/json-iterator/tinygo/gen
type RefNamedArray struct {
Value NamedArray
}import "github.com/json-iterator/tinygo"
// list all the types you need to unmarshal here
json := jsoniter.CreateJsonAdapter(RefNamedArray_json{}, NamedArray_json{})var val1 RefNamedArray
var val2 NamedArray
json.Unmarshal([]byte(`{ "Value": ["hello","world"] }`), &val1)
json.Unmarshal([]byte(`["hello","world"]`), &val2)
```run `go generate` command to generate RefNamedArray_json and NamedArray_json.