go源码分析json的序列化与反序列化

json的数据类型

Go json
bool bool
浮点数/整数 常规数字
string(utf-8) string(Unicode)
array/slice array
[]byte string(Base64)
struct JSON对象
map[string]T JSON对象

string

简单的来说字符串是一系列8位字节的集合,通常但不一定代表UTF-8编码的文本。字符串可以为空,但不能为nil。而且字符串的值是不能改变的。

  • 不同的语言字符串有不同的实现,在go的源码中src/runtime/string.gostring的定义如下:
// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.

type stringStruct struct {
	str unsafe.Pointer
	len int
}

可以看到str其实是个指针,指向某个数组的首地址,另一个字段是len长度。其实指向的就是byte数组。

  • string的实例化:注意string其实就是个struct。在实例化这个stringStruct的时候,源码如下
func gostringnocopy(str *byte) string {
	ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
	s := *(*string)(unsafe.Pointer(&ss))
	return s
}
  • string 底层是一个包含多个字节(1字节=8bit)的集合。
  • string 类型的值是不可改变的(这里的值不变指的是变量的数据地址,即 str 的值不变)
  • string结构体的str指针指向的是一个字符常量的地址, 这个地址里面的内容是不可以被改变的,因为它是只读的,但是这个指针可以指向不同的地址。

关联

  • string 可以被拆分为一个包含多个字节的序列,如:

    str := "ben生而平凡"
    fmt.Println([]byte(str))
    
    [98 101 110 231 148 159 232 128 140 229 185 179 229 135 161]
    
  • string 可以被拆分为一个包含多个字符的序列,如:

    str := "ben生而平凡"
    fmt.Println([]rune(str))
    
    [98 101 110 29983 32780 24179 20961]
    
  • 我们通常说的字符是指 Unicode 字符。‘G’, ‘o’, ‘菜’, ‘鸟’ 都是一个字符。一个字符可以是只包含一个字节(像:‘G’, ‘o’),也可以是包含多个字节(像:‘菜’, ‘鸟’)。

  • byte:[]byte和string的差别是更改变量的时候array的内容可以被更改。

    // byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
    // used, by convention, to distinguish byte values from 8-bit unsigned
    // integer values.
    type byte = uint8
    
  • rune

    // rune is an alias for int32 and is equivalent to int32 in all ways. It is
    // used, by convention, to distinguish character values from integer values
    type rune = int32
    
  • slice : slice结构在go的源码中src/runtime/slice.go定义:

    type slice struct {
    	array unsafe.Pointer
    	len   int
    	cap   int
    }
    

    array是数组的指针,len表示长度,cap表示容量。除了cap,其他看起来和string的结构很像。
    但其实他们差别真的很大。

转换

  • 将string转为[]byte,语法[]byte(string)源码如下:可以看到b是新分配的,然后再将s复制给b

    func stringtoslicebyte(buf *tmpBuf, s string) []byte {
    	var b []byte
    	if buf != nil && len(s) <= len(buf) {
    		*buf = tmpBuf{}
    		b = buf[:len(s)]
    	} else {
    		b = rawbyteslice(len(s))
    	}
    	copy(b, s)
    	return b
    }
    
  • 将[]byte转为string,语法string([]byte)源码如下:

    func slicebytetostring(buf *tmpBuf, b []byte) string {
    	l := len(b)
    	if l == 0 {
    		// Turns out to be a relatively common case.
    		// Consider that you want to parse out data between parens in "foo()bar",
    		// you find the indices and convert the subslice to string.
    		return ""
    	}
    	if raceenabled && l > 0 {
    		racereadrangepc(unsafe.Pointer(&b[0]),
    			uintptr(l),
    			getcallerpc(unsafe.Pointer(&buf)),
    			funcPC(slicebytetostring))
    	}
    	if msanenabled && l > 0 {
    		msanread(unsafe.Pointer(&b[0]), uintptr(l))
    	}
    	s, c := rawstringtmp(buf, l)
    	copy(c, b)
    	return s
    }
    
    func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
    	if buf != nil && l <= len(buf) {
    		b = buf[:l]
    		s = slicebytetostringtmp(b)
    	} else {
    		s, b = rawstring(l)
    	}
    	return
    }
    
    func rawstring(size int) (s string, b []byte) {
    	p := mallocgc(uintptr(size), nil, false)
    
    	stringStructOf(&s).str = p
    	stringStructOf(&s).len = size
    
    	*(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
    
    	return
    }
    
  • interface{}

    1、 marshal空接口:interface{}类型其实是个空接口,即没有方法的接口。go的每一种类型都实现了该接口。因此,任何其他类型的数据都可以赋值给interface{}类型。无论是string,int,bool,还是指针类型等,都可赋值给interface{}类型,在marshal序列化的时候且正常编码。

    type Stu struct {
        Name  interface{} `json:"name"`
        Age   interface{}
        HIgh  interface{}
        sex   interface{}
        Class interface{} `json:"class"`
    }
    type Class struct {
        Name  string
        Grade int
    }
    //实例化一个数据结构,用于生成json字符串
    stu := Stu{
        Name: "张三",
        Age:  18,
        HIgh: true,
        sex:  "男",
    }
    cla := new(Class)
    cla.Name = "1班"
    cla.Grade = 3
    stu.Class=cla
    //Marshal失败时err!=nil
    jsonStu, err := json.Marshal(stu)
    if err != nil {
        fmt.Println("生成json字符串错误")
    }
    
    //jsonStu是[]byte类型,转化成string类型便于查看
    fmt.Println(string(jsonStu))
    //输出 {"name":"张三","Age":18,"HIgh":true,"class":{"Name":"1班","Grade":3}}
    

    2、 unMarshal空接口: 对于”复合数据”,在unmarshal的时候,如果接收体中配的项被声明为interface{}类型,go都会默认解析成map[string]interface{}类型。如果我们想直接解析到struct Class对象中,可以将接受体对应的项定义为该struct类型。

    type httpResp struct {
    	RetCode int      `json:"ret_code"`
    	Data interface{} `json:"data"`
    	Message string   `json:"message"`
    }
    
    //若response中的字段Data为复合数据,则response的data字段经过unMarshal后会变成map[string]interface{}的type形式。
    
    json.Unmarshal([]byte(response), &httpRsp)
    
    

json的序列化

1、Marshal方法 :用于将struct、map、slice序列化为json数据。

定义结构体的时候,只有字段名是大写的,才会被编码到json当中。

//将struct对象序列化成json,将map序列化成json将slice序列化成json
func Marshal(v interface{}) ([]byte, error) {
	e := newEncodeState()

	err := e.marshal(v, encOpts{escapeHTML: true})
	if err != nil {
		return nil, err
	}
	buf := append([]byte(nil), e.Bytes()...)

	e.Reset()
	encodeStatePool.Put(e)

	return buf, nil
}
  • 标签: 每个结构字段的编码可以通过结构字段标签中“json”键下存储的格式字符串来定制。格式字符串给出字段的名称,可能后跟逗号分隔的选项列表。名称可能为空,以指定选项而不覆盖默认字段名称。
  1. “omitempty”选项指定如果字段具有空值,定义为false,0,零指针,nil接口值以及任何空数组,切片,映射或字符串,则该字段应从编码中省略。
    Addr string json:"addr,omitempty"

  2. 作为特殊情况,如果字段标签是“ - ”,则该字段总是被省略。请注意,名称为“ - ”的字段仍然可以使用标签“ - ,”生成。json: "-"


json的反序列化

1、Unmarshal方法: 将json数据反序列化为其它数据结构

//将json反序列化成struct对象将json 反序列化到map中将json反序列化到slice中
func Unmarshal(data []byte, v interface{}) error {
	// Check for well-formedness.
	// Avoids filling out half a data structure
	// before discovering a JSON syntax error.
	var d decodeState
	err := checkValid(data, &d.scan)
	if err != nil {
		return err
	}

	d.init(data)
	return d.unmarshal(v)
}

json RawMessage

RawMessag是原始编码后的json值。含json.RawMessage字段的结构,在反序列化时会完整的接收json串对象的[]byte形式。延迟解析在实际使用时才解析它的具体类型。使用json.RawMessage方式,将json的字串继续以byte数组方式存在。

// RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawMessage []byte

// MarshalJSON returns m as the JSON encoding of m.
func (m RawMessage) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	return m, nil
}

// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) error {
	if m == nil {
		return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
	}
	*m = append((*m)[0:0], data...)
	return nil
}

代码分析

// test ...
type test struct {
	WorkLoadTemplate json.RawMessage `json:"workload_template"`
}

// test2 ...
type test2 struct {
	WorkLoadTemplate string  `json:"workload_template"`
}

testjson := 
`{
"workload_template":{"kind":"Deployment","apiVersion":"apps/v1","metadata":{"name":"tars.tarsnotify.test3","namespace":"center","creationTimestamp":null},"spec":{"replicas":1,"selector":null,"template":{"metadata":{"creationTimestamp":null},"spec":{"containers":[{"name":"application","image":"tars.dockerhub.com:5000/darren/tarsnotify:1.0.0","resources":{},"imagePullPolicy":"IfNotPresent"}]}},"strategy":{"type":"RollingUpdate","rollingUpdate":{"maxUnavailable":0,"maxSurge":1}}},"status":{}}
}`
  • testjson串 反序列化到test结构体会成功,json.RawMessage使得该字段保留原始的json串。

  • testjson串反序列化到test2结构体失败,因为在json串中workload_template对应的是一个json对象即go中的结构体,map等。


testjson2 := `{
				"workload_template":"{\"kind\":\"Deployment\",\"apiVersion\":\"apps/v1\",\"metadata\":{\"name\":\"tars.tarsnotify.test3\",\"namespace\":\"center\",\"creationTimestamp\":null},\"spec\":{\"replicas\":1,\"selector\":null,\"template\":{\"metadata\":{\"creationTimestamp\":null},\"spec\":{\"containers\":[{\"name\":\"application\",\"image\":\"tars.dockerhub.com:5000/darren/tarsnotify:1.0.0\",\"resources\":{},\"imagePullPolicy\":\"IfNotPresent\"}]}},\"strategy\":{\"type\":\"RollingUpdate\",\"rollingUpdate\":{\"maxUnavailable\":0,\"maxSurge\":1}}},\"status\":{}}"
			}`
  • testjson串反序列到test2结构体成功。因为在json串中workload_template对应的是一个string对象即go中的string类型。


var d test
var d2 test2

testjson="1"

t.WorkLoadTemplate=[]byte(testjson)
t2.WorkLoadTemplate=testjson

fmt.Printf("d: %v", d)  // 字节的ascii码形式 {[49]}

fmt.Printf("d2: %v", d2) //结构体字符串  {1}

jsonstr, err = json.Marshal(d) 
fmt.Printf("d: %v", string(jsonstr))  //  {"workload_template":1}

jsonstr, err = json.Marshal(d2)
fmt.Printf("d2: %v", string(jsonstr)) // {"workload_template":"1"}

  • 基于同一个string串“1”的序列化结果不一致,因为在两个结构体中的workload_template数据类型不一致,不同类型的编码器不同,导致编码结果不同。

发布了84 篇原创文章 · 获赞 7 · 访问量 3906

猜你喜欢

转载自blog.csdn.net/u014618114/article/details/105115857