美文网首页
8、go第三方json解析器

8、go第三方json解析器

作者: 灵魂深灵 | 来源:发表于2019-08-09 14:13 被阅读0次

    go原生的json解析器:encoding/json 和第三方的json解析器在 Marshal 上基本没有差别,但是在Unmarshal上差别还是很大的,听说go 1.10版本原生 json 和 第三方json解析器:github.com/json-iterator/go 性能持平,已经用不到第三方 json 解析器了。
    注:test.json 文件下载(1569308行),(下载之后放在和 main.go 同一个文件夹下)
    https://pan.baidu.com/s/1lZ9KN2H6lILiEymaW321sw

    测试结果:

    Golang的原生json解析器:encoding/json
    第三方json解析器:github.com/json-iterator/go

    Unmarshal:
    解析文件:test.json(1569308行)
    原生: 677.887527(671.383077ms 684.148791ms 678.130712ms平均值)
    Json_Iterator: 448.29660(469.585036ms 433.741485ms 441.563305ms平均值)
    性能提升:51.21%

    Marshal:
    原生: 65.6363(70.146µs 64.2µs 62.563µs平均值)
    Json_Iterator: 62.38(62.03µs 64.223µs 60.888µs平均值)
    性能提升:5.22%

    1、原生:
    package common
    
    import (
        "encoding/json"
        "io/ioutil"
    )
    
    func HandleJsonWithDefault(flieName string) {
        byteValue, err := ioutil.ReadFile(flieName)
        if err != nil{
            panic(err)
        }
        var s interface{}
        err = json.Unmarshal([]byte(byteValue),&s)
        if err != nil{
            panic(err)
        }
    }
    
    func MarshalWithDefault(){
    
        const jsonStream = ` 
            { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" }
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
        `
        type Message struct {
            Name , Text string
        }
    
        _,err := json. Marshal (jsonStream)
        if err != nil{
            panic(err)
        }
    }
    
    
    2、第三方:
    package common
    
    import (
        jsoniter "github.com/json-iterator/go"
        "io/ioutil"
    )
    
    func HandleJsonWithJsoniter(jsonFile string) {
        byteValue, err := ioutil.ReadFile(jsonFile)
        if err != nil{
            panic(err)
        }
        var s interface{}
        var json = jsoniter.ConfigCompatibleWithStandardLibrary
        err = json.Unmarshal([]byte(byteValue),&s)
        if err != nil{
            panic(err)
        }
        //fmt.Println(s)
    
    }
    
    func MarshalWithJsoniter(){
        const jsonStream = ` 
            { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" }
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    { "Name" : "Ed" , "Text" : "Knock knock." } 
            { "Name" : "Sam" , "Text" : "Who's there?" } 
            { "Name" : "Ed" , "Text" : "Go fmt." } 
            { "Name" : "Sam" , "Text" : "Go fmt who?" } 
            { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
        `
        type Message struct {
            Name , Text string
        }
    
        _,err := jsoniter.Marshal (jsonStream)
        if err != nil{
            panic(err)
        }
    }
    
    
    3、main函数:
    package main
    
    import (
        common "JsonAnalyse/jsonDemo/Common"
        "fmt"
        "time"
    
    )
    
    func main() {
        //1.golang的原生json解析器:encoding/json:
        //t1 := time.Now()
        //common.MarshalWithDefault()
        //common.HandleJsonWithDefault("test.json")
        //elapsed := time.Since(t1)
        //fmt.Printf("执行消耗的时间为:%v", elapsed)
    
        //2.第三方json解析器:github.com/json-iterator/go:
        t1 := time.Now()
        common.HandleJsonWithJsoniter("test.json")
        //common.MarshalWithDefault()
        elapsed := time.Since(t1)
        fmt.Printf("执行消耗的时间为:%v", elapsed)
    
    }
    

    相关文章

      网友评论

          本文标题:8、go第三方json解析器

          本文链接:https://www.haomeiwen.com/subject/kcbkdctx.html