jsonUtil

package
v0.0.0-...-e50112c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

对 json-iterator 的封装

  • 默认开启 “弱类型解析”
    • 比如一个字段声明的是 Id int,但 json 字符串中格式为 "id": "123",仍然能够成功的将其解析为 Id=int(123)。其他类型同理。
  • 针对常用的场景,对函数做了封装

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

Constants

This section is empty.

Variables

View Source
var (
	DefaultTimeFormat = "2006-01-02 15:04:05"
)

Functions

func Get

func Get(data []byte, path ...interface{}) jsonIter.Any

func GetString

func GetString(str string, path ...interface{}) jsonIter.Any

func Marshal

func Marshal(v interface{}) ([]byte, error)

func MarshalIndent

func MarshalIndent(v interface{}, indentAndPrefix ...string) ([]byte, error)

func MarshalToString

func MarshalToString(v interface{}) (string, error)

func MarshalToStringIndent

func MarshalToStringIndent(v interface{}, indentAndPrefix ...string) (string, error)

func MustMarshal

func MustMarshal(v interface{}) []byte

func MustMarshalIndent

func MustMarshalIndent(v interface{}, indentAndPrefix ...string) []byte

func MustMarshalToString

func MustMarshalToString(v interface{}) string

func MustMarshalToStringIndent

func MustMarshalToStringIndent(v interface{}, indentAndPrefix ...string) string

func RegisterEncryptField

func RegisterEncryptField(typ string, encryptKey string, encryptField ...string)

注册加密字段。

加密字段会使用 AES 加密算法在 JSON 序列化和反序列化时自动加解密。

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 Unmarshal

func Unmarshal(data []byte, v interface{}) error

func UnmarshalFromObject

func UnmarshalFromObject(src, dest interface{}) error

func UnmarshalFromString

func UnmarshalFromString(str string, v interface{}) error

func Wrap

func Wrap(val interface{}) jsonIter.Any

Types

type Api

type Api struct {
	jsonIter.API
	// contains filtered or unexported fields
}

------------------------------------------------------------------------------ Api

func DefaultApi

func DefaultApi() *Api

func NewApi

func NewApi(config *jsonIter.Config) *Api

func NoOmitemptyApi

func NoOmitemptyApi() *Api

func SortMapKeysApi

func SortMapKeysApi(sort ...bool) *Api

func (*Api) Clone

func (this *Api) Clone() *Api

func (*Api) Get

func (this *Api) Get(data []byte, path ...interface{}) jsonIter.Any

func (*Api) GetString

func (this *Api) GetString(str string, path ...interface{}) jsonIter.Any

func (*Api) IndentApi

func (this *Api) IndentApi(indentAndPrefix ...string) *Api

func (*Api) Marshal

func (this *Api) Marshal(v interface{}) ([]byte, error)

func (*Api) MarshalIndent

func (this *Api) MarshalIndent(v interface{}, indentAndPrefix ...string) ([]byte, error)

func (*Api) MarshalToString

func (this *Api) MarshalToString(v interface{}) (string, error)

func (*Api) MarshalToStringIndent

func (this *Api) MarshalToStringIndent(v interface{}, indentAndPrefix ...string) (string, error)

func (*Api) MustMarshal

func (this *Api) MustMarshal(v interface{}) []byte

func (*Api) MustMarshalIndent

func (this *Api) MustMarshalIndent(v interface{}, indentAndPrefix ...string) []byte

func (*Api) MustMarshalToString

func (this *Api) MustMarshalToString(v interface{}) string

func (*Api) MustMarshalToStringIndent

func (this *Api) MustMarshalToStringIndent(v interface{}, indentAndPrefix ...string) string

func (*Api) NoOmitemptyApi

func (this *Api) NoOmitemptyApi() *Api

返回一个忽略字段上 omitempty 标记的 Api

func (*Api) Unmarshal

func (this *Api) Unmarshal(data []byte, v interface{}) error

func (*Api) UnmarshalFromObject

func (this *Api) UnmarshalFromObject(src, dest interface{}) (err error)

func (*Api) UnmarshalFromString

func (this *Api) UnmarshalFromString(str string, v interface{}) error

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:    反序列化方法,通过类型名称和数据对象返回接口对象。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL