|
| 1 | +<p align="center"><img width="350px" src="jstream.png" alt="jstream"/></p> |
| 2 | + |
| 3 | +# |
| 4 | + |
| 5 | +[](https://godoc.org/github.com/bcicen/jstream) |
| 6 | + |
| 7 | + |
| 8 | +`jstream` is a streaming JSON parser and value extraction library for Go. |
| 9 | + |
| 10 | +Unlike most JSON parsers, `jstream` is document position- and depth-aware -- this enables the extraction of values at a specified depth, eliminating the overhead of allocating encompassing arrays or objects; e.g: |
| 11 | + |
| 12 | +Using the below example document: |
| 13 | +<img width="85%" src="https://bradley.codes/static/img/jstream-levels.gif" alt="jstream"/> |
| 14 | + |
| 15 | +we can choose to extract and act only the objects within the top-level array: |
| 16 | +```go |
| 17 | +f, _ := os.Open("input.json") |
| 18 | +decoder := jstream.NewDecoder(f, 1) // extract JSON values at a depth level of 1 |
| 19 | +for mv := range decoder.Stream() { |
| 20 | + fmt.Printf("%v\n ", mv.Value) |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +output: |
| 25 | +``` |
| 26 | +map[desc:RGB colors:[red green blue]] |
| 27 | +map[desc:CMYK colors:[cyan magenta yellow black]] |
| 28 | +``` |
| 29 | + |
| 30 | +likewise, increasing depth level to `3` yields: |
| 31 | +``` |
| 32 | +red |
| 33 | +green |
| 34 | +blue |
| 35 | +cyan |
| 36 | +magenta |
| 37 | +yellow |
| 38 | +black |
| 39 | +``` |
| 40 | + |
| 41 | +optionally, kev:value pairs can be emitted as an individual struct: |
| 42 | +```go |
| 43 | +decoder := jstream.NewDecoder(f, 2).EmitKV() // enable KV streaming at a depth level of 2 |
| 44 | +``` |
| 45 | + |
| 46 | +``` |
| 47 | +jstream.KV{desc RGB} |
| 48 | +jstream.KV{colors [red green blue]} |
| 49 | +jstream.KV{desc CMYK} |
| 50 | +jstream.KV{colors [cyan magenta yellow black]} |
| 51 | +``` |
| 52 | + |
| 53 | +## Installing |
| 54 | + |
| 55 | +```bash |
| 56 | +go get github.com/bcicen/jstream |
| 57 | +``` |
| 58 | + |
| 59 | +## Commandline |
| 60 | + |
| 61 | +`jstream` comes with a cli tool for quick viewing of parsed values from JSON input: |
| 62 | + |
| 63 | +```bash |
| 64 | +jstream -d 1 < input.json |
| 65 | +``` |
| 66 | + |
| 67 | +```json |
| 68 | +{"colors":["red","green","blue"],"desc":"RGB"} |
| 69 | +{"colors":["cyan","magenta","yellow","black"],"desc":"CMYK"} |
| 70 | +``` |
| 71 | + |
| 72 | +detailed output with `-v` option: |
| 73 | +```bash |
| 74 | +cat input.json | jstream -v -d -1 |
| 75 | + |
| 76 | +depth start end type | value |
| 77 | +2 018 023 string | "RGB" |
| 78 | +3 041 046 string | "red" |
| 79 | +3 048 055 string | "green" |
| 80 | +3 057 063 string | "blue" |
| 81 | +2 039 065 array | ["red","green","blue"] |
| 82 | +1 004 069 object | {"colors":["red","green","blue"],"desc":"RGB"} |
| 83 | +2 087 093 string | "CMYK" |
| 84 | +3 111 117 string | "cyan" |
| 85 | +3 119 128 string | "magenta" |
| 86 | +3 130 138 string | "yellow" |
| 87 | +3 140 147 string | "black" |
| 88 | +2 109 149 array | ["cyan","magenta","yellow","black"] |
| 89 | +1 073 153 object | {"colors":["cyan","magenta","yellow","black"],"desc":"CMYK"} |
| 90 | +0 000 155 array | [{"colors":["red","green","blue"],"desc":"RGB"},{"colors":["cyan","magenta","yellow","black"],"desc":"CMYK"}] |
| 91 | +``` |
| 92 | + |
| 93 | +### Options |
| 94 | + |
| 95 | +Opt | Description |
| 96 | +--- | --- |
| 97 | +-d \<n\> | emit values at depth n. if n < 0, all values will be emitted |
| 98 | +-kv | output inner key value pairs as newly formed objects |
| 99 | +-v | output depth and offset details for each value |
| 100 | +-h | display help dialog |
| 101 | + |
| 102 | +## Benchmarks |
| 103 | + |
| 104 | +Obligatory benchmarks performed on files with arrays of objects, where the decoded objects are to be extracted. |
| 105 | + |
| 106 | +Two file sizes are used -- regular (1.6mb, 1000 objects) and large (128mb, 100000 objects) |
| 107 | + |
| 108 | +input size | lib | MB/s | Allocated |
| 109 | +--- | --- | --- | --- |
| 110 | +regular | standard | 97 | 3.6MB |
| 111 | +regular | jstream | 175 | 2.1MB |
| 112 | +large | standard | 92 | 305MB |
| 113 | +large | jstream | 404 | 69MB |
| 114 | + |
| 115 | +In a real world scenario, including initialization and reader overhead from varying blob sizes, performance can be expected as below: |
| 116 | +<img src="https://bradley.codes/static/img/bench.svg" alt="jstream"/> |
0 commit comments