json2go

package module
v0.0.0-...-1fd3876 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2025 License: MIT Imports: 10 Imported by: 0

README

json2go

json 转 go 结构体

data, err := json2go.Unmarshal([]byte(testJSON), "StructName")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Abbreviation = map[string]struct{}{
	"API":   Void,
	"ASCII": Void,
	"CPU":   Void,
	"CSS":   Void,
	"DNS":   Void,
	"EOF":   Void,
	"GUID":  Void,
	"HTML":  Void,
	"HTTP":  Void,
	"HTTPS": Void,
	"ID":    Void,
	"IP":    Void,
	"JSON":  Void,
	"LHS":   Void,
	"QPS":   Void,
	"RAM":   Void,
	"RHS":   Void,
	"RPC":   Void,
	"SLA":   Void,
	"SMTP":  Void,
	"SSH":   Void,
	"TCP":   Void,
	"TLS":   Void,
	"TTL":   Void,
	"UDP":   Void,
	"UI":    Void,
	"UID":   Void,
	"UUID":  Void,
	"URI":   Void,
	"URL":   Void,
	"UTF8":  Void,
	"VM":    Void,
	"XML":   Void,
	"XSRF":  Void,
	"XSS":   Void,
}

需要保持全大写的缩写字典 可自行修改

View Source
var DefaultConverter = NewConverter(true, false)
View Source
var ErrInvalidDelim = errors.New("req: invalid delim")
View Source
var ErrInvalidToken = errors.New("req: invalid token")
View Source
var Void = struct{}{}

Functions

func JavaScriptReplace

func JavaScriptReplace(s string, pattern *regexp.Regexp, fn func(match string, submatch ...string) string) string

仿 JavaScript 的字符串替换函数

func NormalizeName

func NormalizeName(name string) string

正规化名称

func ParseNumberType

func ParseNumberType(type1, type2 string) string

解析 array 中类型名

因为 json 的 array 里面允许不同类型的值,所以需要一个函数来判断到底用哪个

如果都是数字就取范围最大的那个,如果有非数字类型则用 any

func ParseType

func ParseType(t any) (rtype string, comment string)

解析类型名

func RegularizeName

func RegularizeName(name string) string

规范化名称

func Unmarshal

func Unmarshal(data []byte, name string) ([]byte, error)

Types

type Converter

type Converter struct {
	// 是否使用 any 代替 interface{}
	Any bool

	// 是否在字段后加注释
	Comment bool
	// contains filtered or unexported fields
}

转换器

func NewConverter

func NewConverter(any bool, comment bool) *Converter

冷知识 any 其实不是关键字 是可以重写的

func (*Converter) String

func (c *Converter) String() string

输出已解析内容

func (*Converter) Struct

func (c *Converter) Struct(name string) ([]byte, error)

输出格式化结构体

func (*Converter) Unmarshal

func (c *Converter) Unmarshal(data []byte, name string) ([]byte, error)

将 JSON 转换成结构体字符串

参数 name 为最外层结构体名字(会进行正规化)

func (*Converter) UnmarshalJSON

func (c *Converter) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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