Documentation
¶
Overview ¶
------------------------------------------------------------------------------ 对 jsonIter 的进一步封装
1、默认注册弱类型解释器,支持将 string("123") 解析为 number(123)、将 string("true")|number(!0) 解析为 bool(true) 等。 2、time.Time 默认序列化为 yyyy-MM-dd HH:MM:SS 格式 3、序列化 Map 时默认对 Key 进行排序 4、简化接口方法
------------------------------------------------------------------------------
Index ¶
- Variables
- func Get(data []byte, path ...interface{}) jsonIter.Any
- func GetString(str string, path ...interface{}) jsonIter.Any
- func Marshal(v interface{}) ([]byte, error)
- func MarshalIndent(v interface{}, indentAndPrefix ...string) ([]byte, error)
- func MarshalToString(v interface{}) (string, error)
- func MarshalToStringIndent(v interface{}, indentAndPrefix ...string) (string, error)
- func MustMarshal(v interface{}) []byte
- func MustMarshalIndent(v interface{}, indentAndPrefix ...string) []byte
- func MustMarshalToString(v interface{}) string
- func MustMarshalToStringIndent(v interface{}, indentAndPrefix ...string) string
- func RegisterEncryptField(typ string, encryptKey string, encryptField ...string)
- func RegisterInterfaceCodec(i InterfaceCodec)
- func SetDefaultApi(api *Api)
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalFromObject(src, dest interface{}) error
- func UnmarshalFromString(str string, v interface{}) error
- func Wrap(val interface{}) jsonIter.Any
- type Api
- func (this *Api) Clone() *Api
- func (this *Api) Get(data []byte, path ...interface{}) jsonIter.Any
- func (this *Api) GetString(str string, path ...interface{}) jsonIter.Any
- func (this *Api) IndentApi(indentAndPrefix ...string) *Api
- func (this *Api) Marshal(v interface{}) ([]byte, error)
- func (this *Api) MarshalIndent(v interface{}, indentAndPrefix ...string) ([]byte, error)
- func (this *Api) MarshalToString(v interface{}) (string, error)
- func (this *Api) MarshalToStringIndent(v interface{}, indentAndPrefix ...string) (string, error)
- func (this *Api) MustMarshal(v interface{}) []byte
- func (this *Api) MustMarshalIndent(v interface{}, indentAndPrefix ...string) []byte
- func (this *Api) MustMarshalToString(v interface{}) string
- func (this *Api) MustMarshalToStringIndent(v interface{}, indentAndPrefix ...string) string
- func (this *Api) NoOmitemptyApi() *Api
- func (this *Api) Unmarshal(data []byte, v interface{}) error
- func (this *Api) UnmarshalFromObject(src, dest interface{}) (err error)
- func (this *Api) UnmarshalFromString(str string, v interface{}) error
- type InterfaceCodec
Constants ¶
This section is empty.
Variables ¶
View Source
var (
DefaultTimeFormat = "2006-01-02 15:04:05"
)
Functions ¶
func MarshalIndent ¶
func MarshalToString ¶
func MarshalToStringIndent ¶
func MustMarshal ¶
func MustMarshal(v interface{}) []byte
func MustMarshalIndent ¶
func MustMarshalToString ¶
func MustMarshalToString(v interface{}) string
func RegisterInterfaceCodec ¶
func RegisterInterfaceCodec(i InterfaceCodec)
注册接口类型的序列化与反序列化逻辑。
默认情况下无法将 JSON 字符串反序列化到接口对象中,因为持续不知道对应的具体的类型。 InterfaceCodec 通过在序列化时通过额外的字段把具体的类型信息也序列化到 JSON 字符串中,这样在反序列化时候就能够通过类型名称创建不同类型的对象。
Example:
type testA struct { AAA int `json:"aaa,omitempty"` } type testB struct { BBB int `json:"bbb,omitempty"` } func (this *testA) getType() string { return "a" } func (this *testB) getType() string { return "b" } type testStruct struct { Obj testInterface `json:"obj,omitempty"` } type testInterface interface { getType() string } a := &testStruct{ Obj: &testA{ AAA: 111}, } util.Println(MustMarshalToString(a)) // {"obj":{"type":"a","data":{"aaa":111}}} b := &testStruct{ Obj: &testB{BBB: 222}, } util.Println(MustMarshalToString(b)) // {"obj":{"type":"b","data":{"bbb":222}}}
func SetDefaultApi ¶
func SetDefaultApi(api *Api)
func UnmarshalFromObject ¶
func UnmarshalFromObject(src, dest interface{}) error
func UnmarshalFromString ¶
Types ¶
type Api ¶
------------------------------------------------------------------------------ Api
func DefaultApi ¶
func DefaultApi() *Api
func NoOmitemptyApi ¶
func NoOmitemptyApi() *Api
func SortMapKeysApi ¶
func (*Api) MarshalIndent ¶
func (*Api) MarshalToString ¶
func (*Api) MarshalToStringIndent ¶
func (*Api) MustMarshal ¶
func (*Api) MustMarshalIndent ¶
func (*Api) MustMarshalToString ¶
func (*Api) MustMarshalToStringIndent ¶
func (*Api) UnmarshalFromObject ¶
func (*Api) UnmarshalFromString ¶
type InterfaceCodec ¶
type InterfaceCodec struct { Type reflect.Type // 接口类型 TypeField string // 序列化时用来存储类型名称的字段 DataField string // 序列化时用来存储数据的字段 Encode func(obj interface{}) (typ string, data interface{}) // 序列化方法,指定接口对象、返回具体的类型名称和数据对象。 Decode func(typ string, data interface{}) (interface{}, error) // 反序列化方法,通过类型名称和数据对象返回接口对象。 }
接口类型的编解码选项。
Type: 接口类型 TypeField: 序列化时用来存储类型名称的字段 DataField: 序列化时用来存储数据的字段 Encode: 序列化方法,指定接口对象、返回具体的类型名称和数据对象。 Decode: 反序列化方法,通过类型名称和数据对象返回接口对象。
Click to show internal directories.
Click to hide internal directories.