Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)

生命不止,继续go go go!!!

号外号外,差不一条广告,我的博客六月到十一月UV的地域分布:
Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)_第1张图片

golang中的使用json一点也不陌生,golang为我们提供了官方的encoding/json:

Go语言学习之encoding/json包(The way to go)

Go实战–net/http中JSON的使用(The way to go)

今天,跟大家分享一个github上比较活的golang json解析器,也许是全世界最快的。

jsoniter ( json-iterator )是一款快且灵活的 JSON 解析器,同时提供 Java 和 Go 两个版本

Jsoniter 是最快的 JSON 解析器。它最多能比普通的解析器快 10 倍之多,即使在数据绑定的用法下也有同样的性能优势。无耻地献上自己的 跑分

非常易于使用的 api ,允许你使用任何风格或者混搭的方式来解析 JSON 。给你前所未有的灵活性。来看看这些 API 们 是不是真的有那么好用吧

独特的 iterator api 能够直接遍历 JSON ,极致性能! 0 内存分配!这样的 iterator 你绝对没有用过

golang中的跑分:
Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)_第2张图片

github地址:
https://github.com/json-iterator/go

获取:
go get github.com/json-iterator/go

简单应用Marshal

json.Marshal(&data)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data)

完整代码:

package main

import (
    "encoding/json"
    "fmt"
    "os"

    "github.com/json-iterator/go"
)

func main() {
    type ColorGroup struct {
        ID     int
        Name   string
        Colors []string
    }
    group := ColorGroup{
        ID:     1,
        Name:   "Reds",
        Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
    }
    b, err := json.Marshal(group)
    if err != nil {
        fmt.Println("error:", err)
    }
    os.Stdout.Write(b)

    var json_iterator = jsoniter.ConfigCompatibleWithStandardLibrary
    b, err = json_iterator.Marshal(group)
    os.Stdout.Write(b)
}

输出:
{“ID”:1,”Name”:”Reds”,”Colors”:[“Crimson”,”Red”,”Ruby”,”Maroon”]}{“ID”:1,”Name”:”Reds”,”Colors”:[“Crimson”,”Red”,”Ruby”,”Maroon”]}

简单应用Unmarshal

json.Unmarshal(input, &data)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data)

完整代码:

package main

import (
    "encoding/json"
    "fmt"

    "github.com/json-iterator/go"
)

func main() {
    var jsonBlob = []byte(`[
        {"Name": "Platypus", "Order": "Monotremata"},
        {"Name": "Quoll",    "Order": "Dasyuromorphia"}
    ]`)
    type Animal struct {
        Name  string
        Order string
    }
    var animals []Animal
    err := json.Unmarshal(jsonBlob, &animals)
    if err != nil {
        fmt.Println("error:", err)
    }
    fmt.Printf("%+v", animals)

    var animals2 []Animal
    var json_iterator = jsoniter.ConfigCompatibleWithStandardLibrary
    json_iterator.Unmarshal(jsonBlob, &animals2)
    fmt.Printf("%+v", animals2)
}

输出:

[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}][{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]

简单应用jsoniter.Get
一行代码完成解析:

package main

import (
    "fmt"

    "github.com/json-iterator/go"
)

func main() {

    val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
    str := jsoniter.Get(val, "Colors", 0).ToString()
    fmt.Println(str)
}

简单应用NewDecoder

package main

import (
    "fmt"
    "strings"

    "github.com/json-iterator/go"
)

func main() {
    json := jsoniter.ConfigCompatibleWithStandardLibrary
    reader := strings.NewReader(`{"branch":"beta","change_log":"add the rows{10}","channel":"fros","create_time":"2017-06-13 16:39:08","firmware_list":"","md5":"80dee2bf7305bcf179582088e29fd7b9","note":{"CoreServices":{"md5":"d26975c0a8c7369f70ed699f2855cc2e","package_name":"CoreServices","version_code":"76","version_name":"1.0.76"},"FrDaemon":{"md5":"6b1f0626673200bc2157422cd2103f5d","package_name":"FrDaemon","version_code":"390","version_name":"1.0.390"},"FrGallery":{"md5":"90d767f0f31bcd3c1d27281ec979ba65","package_name":"FrGallery","version_code":"349","version_name":"1.0.349"},"FrLocal":{"md5":"f15a215b2c070a80a01f07bde4f219eb","package_name":"FrLocal","version_code":"791","version_name":"1.0.791"}},"pack_region_urls":{"CN":"https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip","default":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip","local":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip"},"pack_version":"1.5.3.344.393","pack_version_code":393,"region":"all","release_flag":0,"revision":62,"size":38966875,"status":3}`)
    decoder := json.NewDecoder(reader)
    params := make(map[string]interface{})
    err := decoder.Decode(¶ms)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Printf("%+v\n", params)
    }
}

输出:

map[firmware_list: note:map[CoreServices:map[package_name:CoreServices version_code:76 version_name:1.0.76 md5:d26975c0a8c7369f70ed699f2855cc2e] FrDaemon:map[md5:6b1f0626673200bc2157422cd2103f5d package_name:FrDaemon version_code:390 version_name:1.0.390] FrGallery:map[version_code:349 version_name:1.0.349 md5:90d767f0f31bcd3c1d27281ec979ba65 package_name:FrGallery] FrLocal:map[version_name:1.0.791 md5:f15a215b2c070a80a01f07bde4f219eb package_name:FrLocal version_code:791]] pack_version:1.5.3.344.393 pack_version_code:393 status:3 channel:fros pack_region_urls:map[CN:https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip default:http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip local:http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip] release_flag:0 size:3.8966875e+07 md5:80dee2bf7305bcf179582088e29fd7b9 region:all revision:62 change_log:add the rows{10} create_time:2017-06-13 16:39:08 branch:beta]

Go实战--golang中使用号称全世界最快的JSON解析器json-iterator(json-iterator/go)_第3张图片

你可能感兴趣的:(go,Go从不放弃到实战)