go-weaktyping: github.com/shogo82148/go-weaktyping Index | Examples | Files

package weaktyping

import "github.com/shogo82148/go-weaktyping"

Package 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

Index

Examples

Package Files

bool.go bool_javascript.go bool_perl.go bool_php.go bool_python.go bool_ruby.go doc.go float32_gen.go float64_gen.go generate.go int16_gen.go int32_gen.go int64_gen.go int8_gen.go int_gen.go string.go uint16_gen.go uint32_gen.go uint64_gen.go uint8_gen.go uint_gen.go util.go

type Bool Uses

type Bool bool

Bool is weak typed bool. false, 0, "", and null are treated as false, others are true. This behavior is same as JavaScriptBool.

func PtrBool Uses

func PtrBool(v Bool) *Bool

PtrBool returns the pointer of v.

func (*Bool) UnmarshalJSON Uses

func (v *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Bool) UnmarshalText Uses

func (v *Bool) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Float32 Uses

type Float32 float32

Float32 is weak typed float32.

func PtrFloat32 Uses

func PtrFloat32(v Float32) *Float32

PtrFloat32 returns the pointer of v.

func (*Float32) UnmarshalJSON Uses

func (v *Float32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Float32) UnmarshalText Uses

func (v *Float32) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Float64 Uses

type Float64 float64

Float64 is weak typed float64.

func PtrFloat64 Uses

func PtrFloat64(v Float64) *Float64

PtrFloat64 returns the pointer of v.

func (*Float64) UnmarshalJSON Uses

func (v *Float64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Float64) UnmarshalText Uses

func (v *Float64) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Int Uses

type Int int

Int is weak typed int.

func PtrInt Uses

func PtrInt(v Int) *Int

PtrInt returns the pointer of v.

func (*Int) UnmarshalJSON Uses

func (v *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

Code:

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

func (*Int) UnmarshalText Uses

func (v *Int) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Int16 Uses

type Int16 int16

Int16 is weak typed int16.

func PtrInt16 Uses

func PtrInt16(v Int16) *Int16

PtrInt16 returns the pointer of v.

func (*Int16) UnmarshalJSON Uses

func (v *Int16) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int16) UnmarshalText Uses

func (v *Int16) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Int32 Uses

type Int32 int32

Int32 is weak typed int32.

func PtrInt32 Uses

func PtrInt32(v Int32) *Int32

PtrInt32 returns the pointer of v.

func (*Int32) UnmarshalJSON Uses

func (v *Int32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int32) UnmarshalText Uses

func (v *Int32) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Int64 Uses

type Int64 int64

Int64 is weak typed int64.

func PtrInt64 Uses

func PtrInt64(v Int64) *Int64

PtrInt64 returns the pointer of v.

func (*Int64) UnmarshalJSON Uses

func (v *Int64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int64) UnmarshalText Uses

func (v *Int64) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Int8 Uses

type Int8 int8

Int8 is weak typed int8.

func PtrInt8 Uses

func PtrInt8(v Int8) *Int8

PtrInt8 returns the pointer of v.

func (*Int8) UnmarshalJSON Uses

func (v *Int8) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int8) UnmarshalText Uses

func (v *Int8) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type JavaScriptBool Uses

type JavaScriptBool bool

JavaScriptBool is weak typed bool. false, 0, "", and null are treated as false, others are true.

func PtrJavaScriptBool Uses

func PtrJavaScriptBool(v JavaScriptBool) *JavaScriptBool

PtrJavaScriptBool returns the pointer of v.

func (*JavaScriptBool) UnmarshalJSON Uses

func (v *JavaScriptBool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*JavaScriptBool) UnmarshalText Uses

func (v *JavaScriptBool) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type PHPBool Uses

type PHPBool bool

PHPBool is weak typed bool. false, 0, "", [], and null are treated as false, others are true.

func PtrPHPBool Uses

func PtrPHPBool(v PHPBool) *PHPBool

PtrPHPBool returns the pointer of v.

func (*PHPBool) UnmarshalJSON Uses

func (v *PHPBool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*PHPBool) UnmarshalText Uses

func (v *PHPBool) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type PerlBool Uses

type PerlBool bool

PerlBool is weak typed bool. false, 0, "0", "", and null are treated as false, others are true.

func PtrPerlBool Uses

func PtrPerlBool(v PerlBool) *PerlBool

PtrPerlBool returns the pointer of v.

func (*PerlBool) UnmarshalJSON Uses

func (v *PerlBool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*PerlBool) UnmarshalText Uses

func (v *PerlBool) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type PythonBool Uses

type PythonBool bool

PythonBool is weak typed bool. false, 0, "", [], {}, and null are treated as false, others are true.

func PtrPythonBool Uses

func PtrPythonBool(v PythonBool) *PythonBool

PtrPythonBool returns the pointer of v.

func (*PythonBool) UnmarshalJSON Uses

func (v *PythonBool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*PythonBool) UnmarshalText Uses

func (v *PythonBool) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type RubyBool Uses

type RubyBool bool

RubyBool is weak typed bool. false and null are treated as false, others are true.

func PtrRubyBool Uses

func PtrRubyBool(v RubyBool) *RubyBool

PtrRubyBool returns the pointer of v.

func (*RubyBool) UnmarshalJSON Uses

func (v *RubyBool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*RubyBool) UnmarshalText Uses

func (v *RubyBool) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type String Uses

type String string

String is weak typed bool.

func PtrString Uses

func PtrString(v String) *String

PtrString returns the pointer of v.

func (*String) UnmarshalJSON Uses

func (v *String) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*String) UnmarshalText Uses

func (v *String) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint Uses

type Uint uint

Uint is weak typed uint.

func PtrUint Uses

func PtrUint(v Uint) *Uint

PtrUint returns the pointer of v.

func (*Uint) UnmarshalJSON Uses

func (v *Uint) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint) UnmarshalText Uses

func (v *Uint) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint16 Uses

type Uint16 uint16

Uint16 is weak typed uint16.

func PtrUint16 Uses

func PtrUint16(v Uint16) *Uint16

PtrUint16 returns the pointer of v.

func (*Uint16) UnmarshalJSON Uses

func (v *Uint16) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint16) UnmarshalText Uses

func (v *Uint16) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint32 Uses

type Uint32 uint32

Uint32 is weak typed uint32.

func PtrUint32 Uses

func PtrUint32(v Uint32) *Uint32

PtrUint32 returns the pointer of v.

func (*Uint32) UnmarshalJSON Uses

func (v *Uint32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint32) UnmarshalText Uses

func (v *Uint32) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint64 Uses

type Uint64 uint64

Uint64 is weak typed uint64.

func PtrUint64 Uses

func PtrUint64(v Uint64) *Uint64

PtrUint64 returns the pointer of v.

func (*Uint64) UnmarshalJSON Uses

func (v *Uint64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint64) UnmarshalText Uses

func (v *Uint64) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint8 Uses

type Uint8 uint8

Uint8 is weak typed uint8.

func PtrUint8 Uses

func PtrUint8(v Uint8) *Uint8

PtrUint8 returns the pointer of v.

func (*Uint8) UnmarshalJSON Uses

func (v *Uint8) UnmarshalJSON(data []byte) error

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint8) UnmarshalText Uses

func (v *Uint8) UnmarshalText(data []byte) error

UnmarshalText implements "encoding".TextUnmarshaler.

Package weaktyping imports 5 packages (graph). Updated 2018-03-27. Refresh now. Tools for package owners.