iesreza / go-weaktyping

go-weaktyping is a go package for weakly-typed-JSON

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status GoDoc

go-weaktyping

go-weaktyping is a go package for unmashaling weakly-typed-JSON.

Here is an example of weakly-typed-JSON.

{
  "integer1": "123",
  "integer2": 456
}

If you want to umarshal this JSON into following Go struct, "encoding/json".Unmarshal will fail.

ptr := &struct {
  Integer1 int
  Integer2 int
}()
json.Unmarshal(in, ptr) // will fail

You can use weaktyping.Int instead of int for unmarshaling this JSON.

ptr := &struct {
  Integer1 weaktyping.Int
  Integer2 weaktyping.Int
}()
json.Unmarshal(in, ptr) // will succeed

See godoc for more detail.

EXAMPLES

func ExampleInt_UnmarshalJSON() {
	ptr := &struct {
		Foo weaktyping.Int `json:"foo"`
	}{}

	if err := json.Unmarshal([]byte(`{"foo":123}`), ptr); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Foo:", ptr.Foo)
	if err := json.Unmarshal([]byte(`{"foo":"456"}`), ptr); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Foo:", ptr.Foo)

	// Output:
	// Foo: 123
	// Foo: 456
}

LICENSE

This software is released under the MIT License, see LICENSE.

About

go-weaktyping is a go package for weakly-typed-JSON

License:MIT License


Languages

Language:Go 95.6%Language:Shell 4.4%