『Go 内置库第一季:json』

518

WOMEN_WHO_GO_SEATTLE.png

大家好,我叫谢伟,是一名程序员。

近期我会持续更新内置库的学习笔记,主要参考的是文档 godoc 和 内置库的源码

在日常开发过程中,使用最频繁的当然是内置库,无数的开源项目,无不是在内置库的基础之上进行衍生、开发,所以其实是有很大的必要进行梳理学习。

本节的主题:内置库 json

大纲:

  • 自己总结的使用方法
  • 官方支持的API
  • 学到了什么

自己总结的用法

既然是 json 操作,那么核心应该是包括两个方面:

  • 序列化:go 数据类型转换为 json
  • 反序列化:json 转换为 go 数据类型

对应的方法:

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

  2. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

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

具体如何使用呢?

布尔类型

func boolToJson(ok bool) []byte {

	jsonResult, _ := json.Marshal(ok)
	return jsonResult
}

func jsonToBool(value []byte) bool {

	var goResult bool
	json.Unmarshal(value, &goResult)
	return goResult
}

func main(){
    fmt.Println(string(boolToJson(1 == 1)))
	fmt.Println(jsonToBool([]byte(`true`)))
}
>>
true
true

数值型


func intToJson(value int) []byte {
	jsonInt, _ := json.Marshal(value)
	return jsonInt
}

func main(){
    fmt.Println(string(intToJson(12)))
}
>> 
12

结构体

结构体 转换为 json


type Info struct {
	Name    string `json:"name,omitempty"`
	Age     int    `json:"age,string"`
	City    string `json:"city_shanghai"`
	Company string `json:"-"`
}

func (i Info) MarshalOp() []byte {
	jsonResult, _ := json.Marshal(i)
	return jsonResult
}

func main(){
	var info Info
	info = Info{
		Name:    "XieWei",
		Age:     100,
		City:    "shangHai",
		Company: "Only Me",
	}
	fmt.Println(string(info.MarshalOp()))

	var otherInfo Info
	otherInfo.Name = ""
	otherInfo.Age = 20
	otherInfo.City = "BeiJing"
	otherInfo.Company = "Only You"
	fmt.Println(string(otherInfo.MarshalOp()))

}
>>
{"name":"XieWei","age":"100","city_shanghai":"shangHai"}
{"age":"20","city_shanghai":"BeiJing"}

还记得我们之间讲的 反射章节 结构体的 tag 吗?

  • info 结构体的 tag
  • omitempty 表示该字段为空时,不序列化
  • - 表示忽略该字段
  • json 内定义了该字段序列化时显示的字段,比如 Name 最后序列化 为 name;比如 City 最后序列化为 city_shanghai
  • json 内还可以转换类型,比如 age 原本 int 类型,最后转化为 string 类型

json 转换为 结构体:

func UnMarshalExample(value []byte) (result Info) {
	json.Unmarshal(value, &result)
	return result

}

func main(){

   fmt.Println(UnMarshalExample([]byte(`{"name":"xieWei", "age": "20", "city_shanghai": "GuangDong"}`)))

}
>>
{xieWei 20 GuangDong }

好,至此,我们常用的 json 操作就这些,主要两个方面:Marshal 和 UnMarshal

大概讲述了下 结构体的 tag 的作用:

  • 比如如何定义字段名称
  • 比如如何忽略字段
  • 比如如何更改类型
  • 比如如何零值忽略

官方文档

列举几个再常用的:

  • func Valid(data []byte) bool
  • type Marshaler 接口,可以自己定义序列化的返回值
  • type Unmarshaler 接口,可以自己定义反序列化的返回值

Valid

判断是否是有效的 json 格式的数据

func main(){
	fmt.Println(json.Valid([]byte(`{"name":1, 2}`)))
}
>>
false
  • 表示不是标准的 json 格式的数据

Marshaler 接口,需要实现 MarshalJSON 方法

自定义序列化返回值

type Marshaler interface {
    MarshalJSON() ([]byte, error)
}
type SelfMarshal struct {
	Name string
	Age  int
	City string
}

func (self SelfMarshal) MarshalJSON() ([]byte, error) {
	result := fmt.Sprintf("name:--%s,age:--%d,city:--%s", self.Name, self.Age, self.City)
	if !json.Valid([]byte(result)) {
		fmt.Println("invalid")
		return json.Marshal(result)
	}
	return []byte(result), nil
}

func main(){
    
    var self = SelfMarshal{}
	self.Age = 20
	self.Name = "XieWei"
	self.City = "HangZhou"

	selfJsonMarshal, err := json.Marshal(self)
	fmt.Println(err, string(selfJsonMarshal))
}

>>
<nil> "name:--XieWei,age:--20,city:--HangZhou"
  • 返回了自定义的序列化的格式

type jsonTime time.Time

//实现它的json序列化方法
func (this jsonTime) MarshalJSON() ([]byte, error) {
	var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01-02 15:04:05"))
	return []byte(stamp), nil
}

type Test struct {
	Date  jsonTime `json:"date"`
	Name  string   `json:"name"`
	State bool     `json:"state"`
}

func main(){
	var t = Test{}
	t.Date = jsonTime(time.Now())
	t.Name = "Hello World"
	t.State = true
	body, _ := json.Marshal(t)
	fmt.Println(string(body))
}
>>
{"date":"2018-11-06 22:23:19","name":"Hello World","state":true}
  • 返回了自定义的序列化格式数据

总结

  • 友好的 API
  • 日常的序列化反序列化,内置的库其实已经满足要求,但是对于复杂的嵌套的数据类型,想要获取某个字段的值则相当费劲
  • 所以衍生了各种各样的号称高性能的 json 解析库

各 json 解析库性能比对 | 各 json 解析库性能比对

收获:

  • 可以自己定义序列化、反序列化的格式
  • 可以检测 是否符合 json 类型
func (self SelfMarshal) MarshalJSON() ([]byte, error) {
	result := fmt.Sprintf("name:--%s,age:--%d,city:--%s", self.Name, self.Age, self.City)
	return []byte(result), nil
}

上文代码会报错,为什么?因为不是标准 json 格式的数据。

所以通常建议这么做:

func (self SelfMarshal) MarshalJSON() ([]byte, error) {
	result := fmt.Sprintf("name:--%s,age:--%d,city:--%s", self.Name, self.Age, self.City)
	if !json.Valid([]byte(result)) {
		fmt.Println("invalid")
		return json.Marshal(result)
	}
	return []byte(result), nil
}

即将字符串 marshal 处理。


<完>