项目作者: ohler55

项目描述 :
Optimized JSON for Go
高级语言: Go
项目地址: git://github.com/ohler55/ojg.git
创建时间: 2020-04-12T17:17:31Z
项目社区:https://github.com/ohler55/ojg

开源协议:MIT License

下载


{}j

Build Status
Go Report Card

Optimized JSON for Go is a high performance parser with a variety of
additional JSON tools. OjG is optimized to processing huge data sets
where data does not necessarily conform to a fixed structure.

Features

  • Fast JSON parser. Check out the cmd/benchmarks app in this repo.
  • Full JSONPath implemenation that operates on simple types as well as structs.
  • Generic types. Not the proposed golang generics but type safe JSON elements.
  • Fast JSON validator (7 times faster with io.Reader).
  • Fast JSON writer with a sort option (4 times faster).
  • JSON builder from JSON sources using a simple assembly plan.
  • Simple data builders using a push and pop approach.
  • Object encoding and decoding using an approach similar to that used with Oj for Ruby.
  • Simple Encoding Notation, a lazy way to write JSON omitting commas and quotes.

Using

A basic Parse:

  1. obj, err := oj.ParseString(`{
  2. "a":[
  3. {"x":1,"y":2,"z":3},
  4. {"x":2,"y":4,"z":6}
  5. ]
  6. }`)

Using JSONPath expressions:

  1. x, err := jp.ParseString("a[?(@.x > 1)].y")
  2. ys := x.Get(obj)
  3. // returns [4]

The oj command (cmd/oj) uses JSON path for filtering and
extracting JSON elements. It also includes sorting, reformatting, and
colorizing options.

  1. $ oj -m "(@.name == 'Pete')" myfile.json

More complete examples are available in the go docs for most
functions. The example for Unmarshalling
interfaces
demonstrates a feature that
allows interfaces to be marshalled and unmarshalled.

Installation

  1. go get github.com/ohler55/ojg
  2. go get github.com/ohler55/ojg/cmd/oj

or just import in your .go files.

  1. import (
  2. "github.com/ohler55/ojg/alt"
  3. "github.com/ohler55/ojg/asm"
  4. "github.com/ohler55/ojg/gen"
  5. "github.com/ohler55/ojg/jp"
  6. "github.com/ohler55/ojg/oj"
  7. "github.com/ohler55/ojg/sen"
  8. )

To build and install the oj application:

  1. go install ./...

The oj application can be installed with brew.

  1. brew install oj

Benchmarks

Higher numbers (longer bars) are better.

  1. Parse string/[]byte
  2. json.Unmarshal 55916 ns/op 17776 B/op 334 allocs/op
  3. oj.Parse 39570 ns/op 18488 B/op 429 allocs/op
  4. oj-reuse.Parse 17881 ns/op 5691 B/op 364 allocs/op
  5. oj-reuse.Parse █████████████████████▉ 3.13
  6. oj.Parse █████████▉ 1.41
  7. json.Unmarshal ▓▓▓▓▓▓▓ 1.00
  8. Parse io.Reader
  9. json.Decode 63029 ns/op 32449 B/op 344 allocs/op
  10. oj.ParseReader 34289 ns/op 22583 B/op 430 allocs/op
  11. oj-reuse.ParseReader 25094 ns/op 9788 B/op 365 allocs/op
  12. oj.TokenizeLoad 13610 ns/op 6072 B/op 157 allocs/op
  13. oj.TokenizeLoad ████████████████████████████████▍ 4.63
  14. oj-reuse.ParseReader █████████████████▌ 2.51
  15. oj.ParseReader ████████████▊ 1.84
  16. json.Decode ▓▓▓▓▓▓▓ 1.00
  17. to JSON with indentation
  18. json.Marshal 78762 ns/op 26978 B/op 352 allocs/op
  19. oj.JSON 7662 ns/op 0 B/op 0 allocs/op
  20. sen.Bytes 9053 ns/op 0 B/op 0 allocs/op
  21. oj.JSON ███████████████████████████████████████████████████████████████████████▉ 10.28
  22. sen.Bytes ████████████████████████████████████████████████████████████▉ 8.70
  23. json.Marshal ▓▓▓▓▓▓▓ 1.00

See all benchmarks

Compare Go JSON parsers

Releases

See CHANGELOG.md

Contributing

  • Provide a Pull Request off the develop branch.
  • Report a bug
  • Suggest an idea