About me
The author blog | start
Golang is really uncomfortable with json serialization and deserialization, so switching to these more native forms of writing after getting used to high-level language features can be difficult.
No more BB, start recording.
Serialization library selection
When writing a small demo or small tool without a large scale usage scenario, it doesn’t matter which library you use because the performance is not obvious. However, if you are using jSON-iterator in real projects with high concurrency, high volume, etc., I recommend using jSON-iterator.
Go comes with a JSON library
"Encoding /json" comes with the official licenseCopy the code
json-iterator
The fastest GO JSON parser. It’s compatible with the official script, and I use it pretty much all the time.
Github.com/json-iterat…
The efficiency of contrast
Ns Nanosecond op operation
ns/op | allocation bytes | allocation times | |
---|---|---|---|
std decode | 35510 ns/op | 1960 B/op | 99 allocs/op |
easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op |
jsoniter decode | 5623 ns/op | 160 B/op | 3 allocs/op |
std encode | 2213 ns/op | 712 B/op | 5 allocs/op |
easyjson encode | 883 ns/op | 576 B/op | 3 allocs/op |
jsoniter encode | 837 ns/op | 384 B/op | 4 allocs/op |
Encoding case
type Hero struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
Copy the code
serialization
Hero := hero {Birthday: 21-02-23, Age: 20, Birthday: 21-02-23, Skill: = Skill: } jsonStu, err := json.Marshal if err! = nil {fmt.println (" Generate JSON word} fmt.println (string(jsonStu)))Copy the code
deserialization
Structure struct
STR: = "{\" Name \ ": \" zhang sanfeng \ ", \ "Age \" : 98, \ "Birthday \" : \ "2001-09-21 \", \ "Sal \" : 3800.85, \ "Skill \" : \ "wudang sword \"} "var hero hero err := json.Unmarshal([]byte(str), &hero) if err ! = nil { fmt.Printf("unmarshal err=%v\n", err) }Copy the code
Struct array
Map [string]interface{}
Struct array
str := ` [{" Name ":" zhang sanfeng ", "Age" : 98, "Birthday" : "2001-09-21", "Sal" : 3800.85, "Skill" : "wudang sword"}, {" Name ":" zhang mowgli ", "Age" : 28, "Birthday" : "2004-0 9-21","Sal":300.85,"Skill": 300.85 "}] 'hero [] hero := json.Unmarshal([]byte(STR), &hero) if err! Printf("unmarshal err=%v\n", err)} FMT.Printf(" deserialized hero=%v", hero)Copy the code
slice
str := ` [{" Name ":" zhang sanfeng ", "Age" : 98, "Birthday" : "2001-09-21", "Sal" : 3800.85, "Skill" : "wudang sword"}, {" Name ":" zhang mowgli ", "Age" : 28, "Birthday" : "2004-0 }] 'define a slice var slice []map[string]interface{} Err := json.Unmarshal([]byte(STR), &slice) if err! Printf("unmarshal err=%v\n", err)} FMT.Printf(" Deserialized slice=%v\n", slice)Copy the code
Recommended reading
New open source has emerged after Redis tools were charged
The highest engineer skills chart on GitHub
Chinese programmers are most prone to mispronounce words
END
Welcome to pay attention to the public number programmer tool set 👍👍 is committed to sharing excellent open source projects, learning resources, common tools
Reply keyword “attention package”, send you a complete map of programmer skills.
Reply keyword “wX” add personal wechat, hook up with the author, welcome to chat ^-^.