Back to godoc.org
github.com/shogo82148/go-weaktyping

Package weaktyping

v1.0.0
Latest Go to latest
Published: Apr 3, 2020 | License: MIT | Module: github.com/shogo82148/go-weaktyping

Overview

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

type Bool

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

func PtrBool(v Bool) *Bool

PtrBool returns the pointer of v.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Bool) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Float32

type Float32 float32

Float32 is weak typed float32.

func PtrFloat32

func PtrFloat32(v Float32) *Float32

PtrFloat32 returns the pointer of v.

func (*Float32) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Float32) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Float64

type Float64 float64

Float64 is weak typed float64.

func PtrFloat64

func PtrFloat64(v Float64) *Float64

PtrFloat64 returns the pointer of v.

func (*Float64) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Float64) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Int

type Int int

Int is weak typed int.

func PtrInt

func PtrInt(v Int) *Int

PtrInt returns the pointer of v.

func (*Int) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

Example

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)
Foo: 123
Foo: 456

func (*Int) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Int16

type Int16 int16

Int16 is weak typed int16.

func PtrInt16

func PtrInt16(v Int16) *Int16

PtrInt16 returns the pointer of v.

func (*Int16) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int16) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Int32

type Int32 int32

Int32 is weak typed int32.

func PtrInt32

func PtrInt32(v Int32) *Int32

PtrInt32 returns the pointer of v.

func (*Int32) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int32) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Int64

type Int64 int64

Int64 is weak typed int64.

func PtrInt64

func PtrInt64(v Int64) *Int64

PtrInt64 returns the pointer of v.

func (*Int64) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int64) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Int8

type Int8 int8

Int8 is weak typed int8.

func PtrInt8

func PtrInt8(v Int8) *Int8

PtrInt8 returns the pointer of v.

func (*Int8) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Int8) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type JavaScriptBool

type JavaScriptBool bool

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

func PtrJavaScriptBool

func PtrJavaScriptBool(v JavaScriptBool) *JavaScriptBool

PtrJavaScriptBool returns the pointer of v.

func (*JavaScriptBool) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*JavaScriptBool) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type PHPBool

type PHPBool bool

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

func PtrPHPBool

func PtrPHPBool(v PHPBool) *PHPBool

PtrPHPBool returns the pointer of v.

func (*PHPBool) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*PHPBool) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type PerlBool

type PerlBool bool

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

func PtrPerlBool

func PtrPerlBool(v PerlBool) *PerlBool

PtrPerlBool returns the pointer of v.

func (*PerlBool) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*PerlBool) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type PythonBool

type PythonBool bool

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

func PtrPythonBool

func PtrPythonBool(v PythonBool) *PythonBool

PtrPythonBool returns the pointer of v.

func (*PythonBool) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*PythonBool) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type RubyBool

type RubyBool bool

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

func PtrRubyBool

func PtrRubyBool(v RubyBool) *RubyBool

PtrRubyBool returns the pointer of v.

func (*RubyBool) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*RubyBool) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type String

type String string

String is weak typed bool.

func PtrString

func PtrString(v String) *String

PtrString returns the pointer of v.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*String) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint

type Uint uint

Uint is weak typed uint.

func PtrUint

func PtrUint(v Uint) *Uint

PtrUint returns the pointer of v.

func (*Uint) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint16

type Uint16 uint16

Uint16 is weak typed uint16.

func PtrUint16

func PtrUint16(v Uint16) *Uint16

PtrUint16 returns the pointer of v.

func (*Uint16) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint16) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint32

type Uint32 uint32

Uint32 is weak typed uint32.

func PtrUint32

func PtrUint32(v Uint32) *Uint32

PtrUint32 returns the pointer of v.

func (*Uint32) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint32) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint64

type Uint64 uint64

Uint64 is weak typed uint64.

func PtrUint64

func PtrUint64(v Uint64) *Uint64

PtrUint64 returns the pointer of v.

func (*Uint64) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint64) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

type Uint8

type Uint8 uint8

Uint8 is weak typed uint8.

func PtrUint8

func PtrUint8(v Uint8) *Uint8

PtrUint8 returns the pointer of v.

func (*Uint8) UnmarshalJSON

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

UnmarshalJSON implements "encoding/json".Unmarshaler.

func (*Uint8) UnmarshalText

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

UnmarshalText implements "encoding".TextUnmarshaler.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier