Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Float32Slice ¶
type Float32Slice []float32
Float32Slice 扩展支持 JSON 的 []float32 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1,1.2" value: [123,"456",1,"2.1",2.3] value: null value: 123 value: 123.1
func (*Float32Slice) UnmarshalJSON ¶
func (ns *Float32Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type Float64Slice ¶
type Float64Slice []float64
Float64Slice 扩展支持 JSON 的 []float64 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1,1.2" value: [123,"456",1,"2.1",2.3] value: null value: 123 value: 123.1
func (*Float64Slice) UnmarshalJSON ¶
func (ns *Float64Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fsgo/fsgo/fsjson" ) func main() { type user struct { IDS fsjson.Float64Slice } txtList := []string{ `{}`, `{"IDS":""}`, `{"IDS":null}`, `{"IDS":123}`, `{"IDS":["123", 456,0,2.1," 3.2"]}`, `{"IDS":[1,2]}`, `{"IDS":2.3}`, `{"IDS":-1}`, `{"IDS":"abc"}`, // not support `{"IDS":"abc,def,1"}`, // not support `{"IDS":err}`, // not support `{"IDS":1.0}`, `{"IDS":"1.0"}`, } printUser := func(u *user) string { if u == nil { return "nil" } if u.IDS == nil { return "&{IDS:nil}" } return fmt.Sprintf("%+v", u) } for i := 0; i < len(txtList); i++ { txt := txtList[i] var u *user err := json.Unmarshal([]byte(txt), &u) fmt.Printf("%-30s -> err=%v user=%s\n", txt, err != nil, printUser(u)) } }
Output: {} -> err=false user=&{IDS:nil} {"IDS":""} -> err=false user=&{IDS:nil} {"IDS":null} -> err=false user=&{IDS:nil} {"IDS":123} -> err=false user=&{IDS:[123]} {"IDS":["123", 456,0,2.1," 3.2"]} -> err=false user=&{IDS:[123 456 0 2.1 3.2]} {"IDS":[1,2]} -> err=false user=&{IDS:[1 2]} {"IDS":2.3} -> err=false user=&{IDS:[2.3]} {"IDS":-1} -> err=false user=&{IDS:[-1]} {"IDS":"abc"} -> err=true user=&{IDS:nil} {"IDS":"abc,def,1"} -> err=true user=&{IDS:nil} {"IDS":err} -> err=true user=nil {"IDS":1.0} -> err=false user=&{IDS:[1]} {"IDS":"1.0"} -> err=false user=&{IDS:[1]}
type Int16Slice ¶
type Int16Slice []int16
Int16Slice 扩展支持 JSON 的 []int16 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456" value: [123,"456",1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Int16Slice) UnmarshalJSON ¶
func (ns *Int16Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type Int32Slice ¶
type Int32Slice []int32
Int32Slice 扩展支持 JSON 的 []int32 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456" value: [123,"456",1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Int32Slice) UnmarshalJSON ¶
func (ns *Int32Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type Int64Slice ¶
type Int64Slice []int64
Int64Slice 扩展支持 JSON 的 []int64 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456" value: [123,"456",1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Int64Slice) UnmarshalJSON ¶
func (ns *Int64Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fsgo/fsgo/fsjson" ) func main() { type user struct { IDS fsjson.Int64Slice } txtList := []string{ `{}`, `{"IDS":""}`, `{"IDS":null}`, `{"IDS":123}`, `{"IDS":["123",456,0]}`, `{"IDS":[1,2,-1]}`, `{"IDS":-2}`, `{"IDS":2.3}`, // not support `{"IDS":"abc"}`, // not support `{"IDS":"abc,def,1"}`, // not support `{"IDS":err}`, // not support `{"IDS":1.0}`, // not support `{"IDS":"1.0"}`, // not support } printUser := func(u *user) string { if u == nil { return "nil" } if u.IDS == nil { return "&{IDS:nil}" } return fmt.Sprintf("%+v", u) } for i := 0; i < len(txtList); i++ { txt := txtList[i] var u *user err := json.Unmarshal([]byte(txt), &u) fmt.Printf("%-30s -> err=%v user=%s\n", txt, err != nil, printUser(u)) } }
Output: {} -> err=false user=&{IDS:nil} {"IDS":""} -> err=false user=&{IDS:nil} {"IDS":null} -> err=false user=&{IDS:nil} {"IDS":123} -> err=false user=&{IDS:[123]} {"IDS":["123",456,0]} -> err=false user=&{IDS:[123 456 0]} {"IDS":[1,2,-1]} -> err=false user=&{IDS:[1 2 -1]} {"IDS":-2} -> err=false user=&{IDS:[-2]} {"IDS":2.3} -> err=true user=&{IDS:nil} {"IDS":"abc"} -> err=true user=&{IDS:nil} {"IDS":"abc,def,1"} -> err=true user=&{IDS:nil} {"IDS":err} -> err=true user=nil {"IDS":1.0} -> err=true user=&{IDS:nil} {"IDS":"1.0"} -> err=true user=&{IDS:nil}
type Int8Slice ¶
type Int8Slice []int8
Int8Slice 扩展支持 JSON 的 []int8 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456" value: [123,"456",1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Int8Slice) UnmarshalJSON ¶
UnmarshalJSON 实现了自定义的 json.Unmarshaler
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fsgo/fsgo/fsjson" ) func main() { type user struct { IDS fsjson.Int8Slice } txtList := []string{ `{}`, `{"IDS":""}`, `{"IDS":null}`, `{"IDS":123}`, `{"IDS":["123",456,0]}`, // not support, 456 not int8 `{"IDS":[1,2,-1]}`, `{"IDS":-2}`, `{"IDS":2.3}`, // not support `{"IDS":"abc"}`, // not support `{"IDS":"abc,def,1"}`, // not support `{"IDS":err}`, // not support `{"IDS":1.0}`, // not support `{"IDS":"1.0"}`, // not support } printUser := func(u *user) string { if u == nil { return "nil" } if u.IDS == nil { return "&{IDS:nil}" } return fmt.Sprintf("%+v", u) } for i := 0; i < len(txtList); i++ { txt := txtList[i] var u *user err := json.Unmarshal([]byte(txt), &u) fmt.Printf("%-30s -> err=%v user=%s\n", txt, err != nil, printUser(u)) } }
Output: {} -> err=false user=&{IDS:nil} {"IDS":""} -> err=false user=&{IDS:nil} {"IDS":null} -> err=false user=&{IDS:nil} {"IDS":123} -> err=false user=&{IDS:[123]} {"IDS":["123",456,0]} -> err=true user=&{IDS:nil} {"IDS":[1,2,-1]} -> err=false user=&{IDS:[1 2 -1]} {"IDS":-2} -> err=false user=&{IDS:[-2]} {"IDS":2.3} -> err=true user=&{IDS:nil} {"IDS":"abc"} -> err=true user=&{IDS:nil} {"IDS":"abc,def,1"} -> err=true user=&{IDS:nil} {"IDS":err} -> err=true user=nil {"IDS":1.0} -> err=true user=&{IDS:nil} {"IDS":"1.0"} -> err=true user=&{IDS:nil}
type IntSlice ¶
type IntSlice []int
IntSlice 扩展支持 JSON 的 []int 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456" value: [123,"456",1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*IntSlice) UnmarshalJSON ¶
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type Object ¶
type Object struct { Value any // contains filtered or unexported fields }
Object 兼容多种空值格式的 JSON Object,一般是用在解析 JSON 数据时。
正常情况下,JSON 的 object 的空值可以是:null 和 {} 这两种, 但是对于 PHP 程序,由于使用 PHP 的 array 类型而不是 PHP 的 class 类型,导致 PHP 程序数据的 JSON 数据可能 {"User":[]} 这样,而 User 数据实际应该是一个 Object, 导致 Go 程序解析失败。
这个 Object 类型即为解决该问题而定义。 可以允许 JSON 值为 [],""
func (*Object) MarshalJSON ¶
MarshalJSON 实现了自定义的 json.Marshaler 返回的是 Value 的 json 内容
func (*Object) UnmarshalJSON ¶
UnmarshalJSON 实现了自定义的 json.Unmarshaler
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fsgo/fsgo/fsjson" ) func main() { type class struct { Name string } type user struct { Cl *fsjson.Object } txtList := []string{ `{"Cl":[]}`, `{"Cl":null}`, `{"Cl":""}`, `{"Cl":{"Name":"hello"}}`, `{"Cl":"abc"}`, // not support } for i := 0; i < len(txtList); i++ { txt := txtList[i] c := &class{} u := &user{ Cl: fsjson.NewObject(c), } err := json.Unmarshal([]byte(txt), &u) fmt.Printf("%-30s -> err=%v class=%+v\n", txt, err != nil, c) } }
Output: {"Cl":[]} -> err=false class=&{Name:} {"Cl":null} -> err=false class=&{Name:} {"Cl":""} -> err=false class=&{Name:} {"Cl":{"Name":"hello"}} -> err=false class=&{Name:hello} {"Cl":"abc"} -> err=true class=&{Name:}
type StringSlice ¶
type StringSlice []string
StringSlice 扩展支持 JSON 的 []string 类型 其实际值可以是多种格式,比如:
value: "a" value: "a,b" value: ["a","b",123] value: null value: 123
func (*StringSlice) UnmarshalJSON ¶
func (ss *StringSlice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fsgo/fsgo/fsjson" ) func main() { type user struct { Alias fsjson.StringSlice } txtList := []string{ `{}`, `{"Alias":""}`, `{"Alias":null}`, `{"Alias":123}`, `{"Alias":2.3}`, `{"Alias":"abc"}`, `{"Alias":"abc,def,1"}`, `{"Alias":["abc","def",123,0.1]}`, `{"Alias":err}`, // not support } for i := 0; i < len(txtList); i++ { var u *user err := json.Unmarshal([]byte(txtList[i]), &u) fmt.Printf("err=%v user=%#v\n", err != nil, u) } }
Output: err=false user=&fsjson_test.user{Alias:fsjson.StringSlice(nil)} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice(nil)} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice(nil)} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice{"123"}} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice{"2.3"}} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice{"abc"}} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice{"abc", "def", "1"}} err=false user=&fsjson_test.user{Alias:fsjson.StringSlice{"abc", "def", "123", "0.1"}} err=true user=(*fsjson_test.user)(nil)
type Uint16Slice ¶
type Uint16Slice []uint16
Uint16Slice 扩展支持 JSON 的 []uint16 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1" value: [123,"456",1,-1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Uint16Slice) UnmarshalJSON ¶
func (ns *Uint16Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type Uint32Slice ¶
type Uint32Slice []uint32
Uint32Slice 扩展支持 JSON 的 []uint32 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1" value: [123,"456",1,-1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Uint32Slice) UnmarshalJSON ¶
func (ns *Uint32Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type Uint64Slice ¶
type Uint64Slice []uint64
Uint64Slice 扩展支持 JSON 的 []uint64 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1" value: [123,"456",1,-1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Uint64Slice) UnmarshalJSON ¶
func (ns *Uint64Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fsgo/fsgo/fsjson" ) func main() { type user struct { IDS fsjson.Uint64Slice } txtList := []string{ `{}`, `{"IDS":""}`, `{"IDS":null}`, `{"IDS":123}`, `{"IDS":["123",456,0]}`, `{"IDS":[1,2]}`, `{"IDS":2.3}`, // not support `{"IDS":-1}`, // not support `{"IDS":"abc"}`, // not support `{"IDS":"abc,def,1"}`, // not support `{"IDS":err}`, // not support `{"IDS":1.0}`, // not support `{"IDS":"1.0"}`, // not support } printUser := func(u *user) string { if u == nil { return "nil" } if u.IDS == nil { return "&{IDS:nil}" } return fmt.Sprintf("%+v", u) } for i := 0; i < len(txtList); i++ { txt := txtList[i] var u *user err := json.Unmarshal([]byte(txt), &u) fmt.Printf("%-30s -> err=%v user=%s\n", txt, err != nil, printUser(u)) } }
Output: {} -> err=false user=&{IDS:nil} {"IDS":""} -> err=false user=&{IDS:nil} {"IDS":null} -> err=false user=&{IDS:nil} {"IDS":123} -> err=false user=&{IDS:[123]} {"IDS":["123",456,0]} -> err=false user=&{IDS:[123 456 0]} {"IDS":[1,2]} -> err=false user=&{IDS:[1 2]} {"IDS":2.3} -> err=true user=&{IDS:nil} {"IDS":-1} -> err=true user=&{IDS:nil} {"IDS":"abc"} -> err=true user=&{IDS:nil} {"IDS":"abc,def,1"} -> err=true user=&{IDS:nil} {"IDS":err} -> err=true user=nil {"IDS":1.0} -> err=true user=&{IDS:nil} {"IDS":"1.0"} -> err=true user=&{IDS:nil}
type Uint8Slice ¶
type Uint8Slice []uint8
Uint8Slice 扩展支持 JSON 的 []uint8 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1" value: [123,"456",1,-1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*Uint8Slice) UnmarshalJSON ¶
func (ns *Uint8Slice) UnmarshalJSON(bf []byte) error
UnmarshalJSON 实现了自定义的 json.Unmarshaler
type UintSlice ¶
type UintSlice []uint
UintSlice 扩展支持 JSON 的 []uint 类型 其实际值可以是多种格式,比如:
value: "" value: "123,456,-1" value: [123,"456",1,-1] value: null value: 123 不支持 float 类型,如 "1.2"、1.3 都会失败
func (*UintSlice) UnmarshalJSON ¶
UnmarshalJSON 实现了自定义的 json.Unmarshaler