pax_global_header 0000666 0000000 0000000 00000000064 14273067517 0014526 g ustar 00root root 0000000 0000000 52 comment=b279807a1bad9fa155988667d97a3d2fff4061b5 sjson-1.2.5/ 0000775 0000000 0000000 00000000000 14273067517 0012667 5 ustar 00root root 0000000 0000000 sjson-1.2.5/.github/ 0000775 0000000 0000000 00000000000 14273067517 0014227 5 ustar 00root root 0000000 0000000 sjson-1.2.5/.github/workflows/ 0000775 0000000 0000000 00000000000 14273067517 0016264 5 ustar 00root root 0000000 0000000 sjson-1.2.5/.github/workflows/go.yml 0000664 0000000 0000000 00000001222 14273067517 0017411 0 ustar 00root root 0000000 0000000 name: Go on: push: branches: [ master ] pull_request: branches: [ master ] jobs: build: name: Build runs-on: ubuntu-latest steps: - name: Set up Go 1.x uses: actions/setup-go@v2 with: go-version: ^1.13 - name: Check out code into the Go module directory uses: actions/checkout@v2 - name: Get dependencies run: | go get -v -t -d ./... if [ -f Gopkg.toml ]; then curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh dep ensure fi - name: Build run: go build -v . - name: Test run: go test -v . sjson-1.2.5/LICENSE 0000664 0000000 0000000 00000002066 14273067517 0013700 0 ustar 00root root 0000000 0000000 The MIT License (MIT) Copyright (c) 2016 Josh Baker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. sjson-1.2.5/README.md 0000664 0000000 0000000 00000014451 14273067517 0014153 0 ustar 00root root 0000000 0000000
set a json value quickly
SJSON is a Go package that provides a [very fast](#performance) and simple way to set a value in a json document. For quickly retrieving json values check out [GJSON](https://github.com/tidwall/gjson). For a command line interface check out [JJ](https://github.com/tidwall/jj). Getting Started =============== Installing ---------- To start using SJSON, install Go and run `go get`: ```sh $ go get -u github.com/tidwall/sjson ``` This will retrieve the library. Set a value ----------- Set sets the value for the specified path. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed and validated. Invalid json will not panic, but it may return back unexpected results. Invalid paths may return an error. ```go package main import "github.com/tidwall/sjson" const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}` func main() { value, _ := sjson.Set(json, "name.last", "Anderson") println(value) } ``` This will print: ```json {"name":{"first":"Janet","last":"Anderson"},"age":47} ``` Path syntax ----------- A path is a series of keys separated by a dot. The dot and colon characters can be escaped with ``\``. ```json { "name": {"first": "Tom", "last": "Anderson"}, "age":37, "children": ["Sara","Alex","Jack"], "fav.movie": "Deer Hunter", "friends": [ {"first": "James", "last": "Murphy"}, {"first": "Roger", "last": "Craig"} ] } ``` ``` "name.last" >> "Anderson" "age" >> 37 "children.1" >> "Alex" "friends.1.last" >> "Craig" ``` The `-1` key can be used to append a value to an existing array: ``` "children.-1" >> appends a new value to the end of the children array ``` Normally number keys are used to modify arrays, but it's possible to force a numeric object key by using the colon character: ```json { "users":{ "2313":{"name":"Sara"}, "7839":{"name":"Andy"} } } ``` A colon path would look like: ``` "users.:2313.name" >> "Sara" ``` Supported types --------------- Pretty much any type is supported: ```go sjson.Set(`{"key":true}`, "key", nil) sjson.Set(`{"key":true}`, "key", false) sjson.Set(`{"key":true}`, "key", 1) sjson.Set(`{"key":true}`, "key", 10.5) sjson.Set(`{"key":true}`, "key", "hello") sjson.Set(`{"key":true}`, "key", []string{"hello", "world"}) sjson.Set(`{"key":true}`, "key", map[string]interface{}{"hello":"world"}) ``` When a type is not recognized, SJSON will fallback to the `encoding/json` Marshaller. Examples -------- Set a value from empty document: ```go value, _ := sjson.Set("", "name", "Tom") println(value) // Output: // {"name":"Tom"} ``` Set a nested value from empty document: ```go value, _ := sjson.Set("", "name.last", "Anderson") println(value) // Output: // {"name":{"last":"Anderson"}} ``` Set a new value: ```go value, _ := sjson.Set(`{"name":{"last":"Anderson"}}`, "name.first", "Sara") println(value) // Output: // {"name":{"first":"Sara","last":"Anderson"}} ``` Update an existing value: ```go value, _ := sjson.Set(`{"name":{"last":"Anderson"}}`, "name.last", "Smith") println(value) // Output: // {"name":{"last":"Smith"}} ``` Set a new array value: ```go value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.2", "Sara") println(value) // Output: // {"friends":["Andy","Carol","Sara"] ``` Append an array value by using the `-1` key in a path: ```go value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.-1", "Sara") println(value) // Output: // {"friends":["Andy","Carol","Sara"] ``` Append an array value that is past the end: ```go value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.4", "Sara") println(value) // Output: // {"friends":["Andy","Carol",null,null,"Sara"] ``` Delete a value: ```go value, _ := sjson.Delete(`{"name":{"first":"Sara","last":"Anderson"}}`, "name.first") println(value) // Output: // {"name":{"last":"Anderson"}} ``` Delete an array value: ```go value, _ := sjson.Delete(`{"friends":["Andy","Carol"]}`, "friends.1") println(value) // Output: // {"friends":["Andy"]} ``` Delete the last array value: ```go value, _ := sjson.Delete(`{"friends":["Andy","Carol"]}`, "friends.-1") println(value) // Output: // {"friends":["Andy"]} ``` ## Performance Benchmarks of SJSON alongside [encoding/json](https://golang.org/pkg/encoding/json/), [ffjson](https://github.com/pquerna/ffjson), [EasyJSON](https://github.com/mailru/easyjson), and [Gabs](https://github.com/Jeffail/gabs) ``` Benchmark_SJSON-8 3000000 805 ns/op 1077 B/op 3 allocs/op Benchmark_SJSON_ReplaceInPlace-8 3000000 449 ns/op 0 B/op 0 allocs/op Benchmark_JSON_Map-8 300000 21236 ns/op 6392 B/op 150 allocs/op Benchmark_JSON_Struct-8 300000 14691 ns/op 1789 B/op 24 allocs/op Benchmark_Gabs-8 300000 21311 ns/op 6752 B/op 150 allocs/op Benchmark_FFJSON-8 300000 17673 ns/op 3589 B/op 47 allocs/op Benchmark_EasyJSON-8 1500000 3119 ns/op 1061 B/op 13 allocs/op ``` JSON document used: ```json { "widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, "style": "bold", "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" } } } ``` Each operation was rotated though one of the following search paths: ``` widget.window.name widget.image.hOffset widget.text.onMouseUp ``` *These benchmarks were run on a MacBook Pro 15" 2.8 GHz Intel Core i7 using Go 1.7 and can be be found [here](https://github.com/tidwall/sjson-benchmarks)*. ## Contact Josh Baker [@tidwall](http://twitter.com/tidwall) ## License SJSON source code is available under the MIT [License](/LICENSE). sjson-1.2.5/go.mod 0000664 0000000 0000000 00000000172 14273067517 0013775 0 ustar 00root root 0000000 0000000 module github.com/tidwall/sjson go 1.14 require ( github.com/tidwall/gjson v1.14.2 github.com/tidwall/pretty v1.2.0 ) sjson-1.2.5/go.sum 0000664 0000000 0000000 00000000771 14273067517 0014027 0 ustar 00root root 0000000 0000000 github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= sjson-1.2.5/logo.png 0000664 0000000 0000000 00000040752 14273067517 0014345 0 ustar 00root root 0000000 0000000 PNG IHDR S ?y tEXtSoftware Adobe ImageReadyqe<