Coder Social home page Coder Social logo

beckjiang / go-json Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 149 KB

基于simdjson-go的json处理库,针对decode方法做了优化,实现了对象复用。在高并发场景下,能够有效减少内存分配,以降低gc带来的压力。

Go 100.00%

go-json's Introduction

go-json

基于simdjson-go的json解析库,旨在优化使用map[string]interface{}的json处理场景,与标准库encoding/json不兼容。

基准测试

benchmark

allocationtimes

Raw Result

ns/op allocation bytes allocation times
go-json decode with schema 15796 ns/op 2529 B/op 25 allocs/op
std decode 18924 ns/op 6597 B/op 110 allocs/op
go-json decode 26578 ns/op 2800 B/op 67 allocs/op
jsoniter decode 36709 ns/op 15349 B/op 323 allocs/op
ns/op allocation bytes allocation times
go-json encode (after decode with schema) 803 ns/op 1328 B/op 3 allocs/op
go-json encode 3693 ns/op 1464 B/op 4 allocs/op
std encode 21914 ns/op 8482 B/op 179 allocs/op
jsoniter encode 21997 ns/op 8482 B/op 179 allocs/op

用法

与标准库的用法不兼容,decode以后使用的库中的Value对象,替换用法如下:

标准库:

import "encoding/json"

var testData = []byte("{\"object\":{\"a\":555,\"b\":false},\"success\":true}")
//decode
var res map[string]interface
err := json.Unmarshal(testData, &res)

//读取数据
res["success"].(bool) // true
object := res["object"].(map[string]interface{}) // {\"a\":555,\"b\":false}

object["a"].(int) // 555
object["b"].(bool) // false

//设置数据
object["new_key"] = 123

//encode
result, err := json.Marshal(res)

替代为:

import "github.com/beckjiang/go-json"

var testData = []byte("{\"object\":{\"a\":555,\"b\":false},\"success\":true}")
//解析数据
val := NewNull()
err := val.Decode(testData)

//读取数据
val.ObjectVal([]byte("success")).Bool() // true
found, object := val.ObjectVal([]byte("object"))
object.ObjectVal([]byte("a")).Int() // 555
object.ObjectVal([]byte("b")).Bool() // false

//设置数据
object.SetObjectKV([]byte("new_key"), NewInt(123))

//encode
result := object.Encode(nil)
//用完释放,对象复用
val.DecRef()

如何获取

go get github.com/beckjiang/go-json

go-json's People

Contributors

beckjiang avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

go-json's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.