Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bserdar/jsonom
JSON Object Model for Go
https://github.com/bserdar/jsonom
golang json
Last synced: 24 days ago
JSON representation
JSON Object Model for Go
- Host: GitHub
- URL: https://github.com/bserdar/jsonom
- Owner: bserdar
- License: apache-2.0
- Created: 2021-10-04T18:03:02.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-07T19:34:29.000Z (9 months ago)
- Last Synced: 2024-10-29T20:25:02.582Z (2 months ago)
- Topics: golang, json
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![GoDoc](https://godoc.org/github.com/bserdar/jsonom?status.svg)](https://godoc.org/github.com/bserdar/jsonom)
[![Go Report Card](https://goreportcard.com/badge/github.com/bserdar/jsonom)](https://goreportcard.com/report/github.com/bserdar/jsonom)# JSON Object Model
This is a Go package to process JSON documents with an API similar to
XML DOM API. It represents the JSON document as a tree of Nodes. The
basic JSON types are:* Object is a key-value pair, where value is another node,
* Array is an ordered value list,
* Value is a JSON value node containing null, string, boolean, or a
number
When a JSON document is unmarshaled, primitive values are converted to
the following Go types:
* JSON number values are converted json.Number
* JSON strings are converted to Go strings
* JSON boolean values are converted to Go boolean
* Null is converted to nilThis library preserves the ordering of keys in a JSON object.
## Key Interning
In JSON documents, the objects keys tend to repeat. For large JSON
documents, it makes sense to "intern" these keys in a hashmap so a
single string copy is used for all instances of a string value.JSON unmarshaling functions get an `Interner` argument. This can be
used to keep a common interner when processing multiple documents. If
a `nil` interner is passed to these functions, an new interner will be
used to store the keys of that document only.## Example
```
func main() {
input:=`{"key":"value", "arr": [ 1,2 ]}`
j, err:=jsonom.Unmarshal([]byte(input),nil)
if err!=nil {
panic(err)
}
obj:=j.(*jsonom.Object)
v,_:=obj.Value("key")
fmt.Println(v.(*jsonom.Value).Value())
a,_:=obj.Value("arr")
arr:=a.(*jsonom.Array)
fmt.Println(arr.Len())
arr.Append(jsonom.NewValue(3))
fmt.Println(arr.Len())
j.Encode(os.Stdout)
}
```