Giter Club home page Giter Club logo

tinygo's Introduction

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.

tinygo's People

Contributors

taowen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

fyxgaming

tinygo's Issues

Golang case insentivity

According to https://pkg.go.dev/encoding/json#Unmarshal

Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match

To have this library as an drop-in replacement, maybe this behavior should be added.

In the meantime, as the code is generated, I simply change the mapping myself.

time.Duration unmarshalling

Consider this struct

type MyStruct struct {
	timeout time.Duration
}

will produce an output like

func MyStruct_ptr2_json_unmarshal (iter *jsoniter.Iterator, out **time.Duration) {
    var val time.Duration
    time.Duration_json_unmarshal(iter, &val)
    if iter.Error == nil {
      *out = &val
    }
}

Where

time.Duration_json_unmarshal(iter, &val)

obviously does not exist.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.