Documentation
¶
Overview ¶
Package nullable provides types what allows to determine if a JSON key has been set to null or not provided.
usr := struct {
Name nullable.String `json:"name"`
}{}
data := []byte("{}")
if err := json.Unmarshal(data, &usr); err != nil {
log.Fatalf("unmarshaling failed: %s\n", err)
}
fmt.Println("name present", usr.Name.Present) // false
fmt.Println("name valid", usr.Name.Valid) // false
fmt.Println("name value", usr.Name.Value) // ""
data = []byte("{\"name\":null}")
if err := json.Unmarshal(data, &usr); err != nil {
log.Fatalf("unmarshaling failed: %s\n", err)
}
fmt.Println("name present", usr.Name.Present) // true
fmt.Println("name valid", usr.Name.Valid) // false
fmt.Println("name value", usr.Name.Value) // ""
data = []byte("{\"name\":\"John\"}")
if err := json.Unmarshal(data, &usr); err != nil {
log.Fatalf("unmarshaling failed: %s\n", err)
}
fmt.Println("name present", usr.Name.Present) // true
fmt.Println("name valid", usr.Name.Valid) // true
fmt.Println("name value", usr.Name.Value) // "John"
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid bool
Value bool
}
Bool represents a bool that may be null or not present in json at all.
func NewBoolNull ¶
func NewBoolNull() *Bool
func (Bool) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON implements json.Marshaler interface.
type Float ¶
type Float struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid float
Value float64
}
Float represents a float that may be null or not present in json at all.
func NewFloatNull ¶
func NewFloatNull() *Float
func (Float) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface.
func (*Float) UnmarshalJSON ¶
UnmarshalJSON implements json.Marshaler interface.
type FloatSlice ¶
type FloatSlice struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid []float64
Value []float64
}
FloatSlice represents a float slice that may be null or not present in json at all.
func (*FloatSlice) UnmarshalJSON ¶
func (f *FloatSlice) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Marshaler interface.
type Int ¶
type Int struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid int64
Value int64
}
Int represents an int that may be null or not present in json at all.
func NewIntNull ¶
func NewIntNull() *Int
func (Int) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implements json.Marshaler interface.
type IntSlice ¶
type IntSlice struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid []int64
Value []int64
}
IntSlice represents an int slice that may be null or not present in json at all.
func (*IntSlice) UnmarshalJSON ¶
UnmarshalJSON implements json.Marshaler interface.
type String ¶
type String struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid string
Value string
}
String represents a string that may be null or not present in json at all.
func NewStringNull ¶
func NewStringNull() *String
func (String) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface.
func (*String) UnmarshalJSON ¶
UnmarshalJSON implements json.Marshaler interface.
type StringSlice ¶
type StringSlice struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null
Value []string
}
StringSlice represents a []string that may be null or not present in json at all.
func (*StringSlice) UnmarshalJSON ¶
func (s *StringSlice) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Marshaler interface.
