Skip to content

json-iterator/tinygo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Tao Wen
Dec 21, 2021
84b5b69 · Dec 21, 2021

History

76 Commits
Dec 20, 2021
Dec 9, 2021
Dec 21, 2021
Dec 8, 2021
Dec 16, 2021
Dec 21, 2021
Dec 8, 2021
Dec 1, 2021
Dec 9, 2021
Dec 17, 2021
Dec 2, 2021
Dec 8, 2021
Dec 4, 2021
Dec 4, 2021
Dec 7, 2021
Dec 8, 2021
Dec 4, 2021
Dec 8, 2021
Dec 7, 2021
Dec 21, 2021
Dec 17, 2021
Dec 16, 2021
Dec 16, 2021
Dec 17, 2021
Dec 17, 2021
Dec 16, 2021

Repository files navigation

make json.Unmarshal work in tinygo

type NamedArray = []string

type 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: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.

About

make json.Unmarshal work in tinygo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages