Several ways to serialize and deserialize golang

  • 2020-11-18 06:17:43
  • OfStack

There are many modules that golang USES to serialize; let's look at three.

json

First up is json, which is almost certainly true.

serialization


package main

import (
 "encoding/json"
 "fmt"
)

type Girl struct {
 Name string
 Age int
 Gender string
 Where string
 Is_married bool
}

func main() {
 g := Girl{"satori", 16, "f"," The Temple of Oriental Earth ", false}

 // You can use it directly json.Marshal But it doesn't look good in print, so I'm going to indent it 
 ret, err := json.MarshalIndent(g, "", " ")
 if err != nil {
 fmt.Println(err)
 } else {
 // The result is a byte array that needs to be converted to string
 fmt.Println(string(ret))
 /*
 {
  "Name": "satori",
  "Age": 16,
  "Gender": "f",
  "Where": " The Temple of Oriental Earth ",
  "Is_married": false
 }
 */
 }
}

Of course the case of golang we know has meaning, if changed to lowercase, then the field cannot be serialized. But this serialized field is also uppercase, what if we want lowercase?


package main

import (
 "encoding/json"
 "fmt"
)

type Girl struct {
 // use `json:"xxx"` It's equivalent to getting up 1 Individual name xxx That's the name of the field that will be serialized, 
 Name string `json:"name"`
 Age int `json:"age"`
 Gender string `json:"gender"`
 Where string `json:"where"`
 Is_married bool `json:"is_married"`
}

func main() {
 g := Girl{"satori", 16, "f"," The Temple of Oriental Earth ", false}

 ret, err := json.MarshalIndent(g, "", " ")
 if err != nil {
 fmt.Println(err)
 } else {
 fmt.Println(string(ret))
 /*
 {
  "name": "satori",
  "age": 16,
  "gender": "f",
  "where": " The Temple of Oriental Earth ",
  "is_married": false
 }
 */
 }
}

deserialization


package main

import (
 "encoding/json"
 "fmt"
)

type Girl struct {
 Name string `json:"name"`
 Age int `json:"age"`
 Gender string `json:"gender"`
 Where string `json:"where"`
 Is_married bool `json:"is_married"`
}

func main() {
 g := Girl{"satori", 16, "f"," The Temple of Oriental Earth ", false}

 ret, err := json.MarshalIndent(g, "", " ")
 if err != nil {
 fmt.Println(err)
 return
 }

 // create 1 A variable 
 g2 := Girl{}
 // The incoming json String, and pointer 
 err = json.Unmarshal(ret, &g2)
 if err != nil {
 fmt.Println(err)
 }
 fmt.Println(g2) //{satori 16 f  The Temple of Oriental Earth  false}
 fmt.Println(g2.Name, g2.Age) // satori 16
}

gob

gob is the "private" encoding and decoding method provided by golang, which is more efficient than json, xml, etc., especially suitable for transferring data between Go language programs.

serialization


package main

import (
 "bytes"
 "encoding/gob"
 "fmt"
)

type Girl struct {
 Name    string
 Age    int  `json:"age"`
 Gender   string `json:"gender"`
 Where   string `json:"where"`
 Is_married bool  `json:"is_married"`
}

func main() {
 g := Girl{"satori", 16, "f", " The Temple of Oriental Earth ", false}

 // To create the cache 
 buf := new(bytes.Buffer)
 // Drop the pointer in 
 enc := gob.NewEncoder(buf)

 // call Encode Serialize 
 if err := enc.Encode(g); err != nil {
 fmt.Println(err)
 return
 } else {
 // Serialized content is put in buf inside 
 fmt.Println(buf.String())
 /*
 G��Girl�� Name Age Gender Where 
 Is_married  !��satori f The Temple of Oriental Earth 
 */
 }
}

It was found to be gibber, as it is similar to python's pickle, which is unique to the language. So it doesn't matter if we don't know each other, golang will do

deserialization


package main

import (
 "bytes"
 "encoding/gob"
 "fmt"
)

type Girl struct {
 Name    string
 Age    int  `json:"age"`
 Gender   string `json:"gender"`
 Where   string `json:"where"`
 Is_married bool  `json:"is_married"`
}

func main() {
 g := Girl{"satori", 16, "f", " The Temple of Oriental Earth ", false}

 buf := new(bytes.Buffer)
 enc := gob.NewEncoder(buf)
 if err := enc.Encode(g);err != nil {
 fmt.Println(err)
 return
 }

 var g1 = Girl{}
 //bytes.NewBuffer and bytes.Buffer Similar, except you can pass it in 1 The initial byte Array, return 1 A pointer to the 
 dec := gob.NewDecoder(bytes.NewBuffer(buf.Bytes()))
 // call Decode methods , Passing in a pointer to a struct object will automatically buf.Bytes() The content inside is converted to a structure 
 if err := dec.Decode(&g1);err != nil {
 fmt.Println(err)
 return
 } else {
 fmt.Println(g1) // {satori 16 f  The Temple of Oriental Earth  false}
 }
}

msgpack

MessagePack is an efficient 2 - base serialization format. It allows you to exchange data between multiple languages such as JSON. But it's faster and smaller.

The installation


go get -u github.com/vmihailenco/msgpack

Serialization and deserialization

The interface and json are 1


package main

import (
 "fmt"
 "github.com/vmihailenco/msgpack"
)

type Girl struct {
 Name    string
 Age    int  `json:"age"`
 Gender   string `json:"gender"`
 Where   string `json:"where"`
 Is_married bool  `json:"is_married"`
}

func main() {
 g := Girl{"satori", 16, "f", " The Temple of Oriental Earth ", false}

 // There is no MarshalIndent
 if ret, err := msgpack.Marshal(g); err != nil {
 fmt.Println(err)
 return
 } else {
 fmt.Println(string(ret)) //��Name�satori�Age�    �Gender�f�Where� The Temple of Oriental Earth �Is_married�
 var g1 = Girl{}
 if err := msgpack.Unmarshal(ret, &g1);err!=nil {
  fmt.Println(err)
  return
 } else {
  fmt.Println(g1) // {satori 16 f  The Temple of Oriental Earth  false}
 }
 }
}

Related articles: