Categorygithub.com/bserdar/jsonom
modulepackage
1.2.0
Repository: https://github.com/bserdar/jsonom.git
Documentation: pkg.go.dev

# README

GoDoc Go Report Card

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 nil

This 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)
}

# Functions

BoolValue creates a new value from a boolean value.
Decode a JSON object using the given decoder.
No description provided by the author
NewArray returns an array with the given nodes.
NewKeyValue returns a new key-value pair.
NewObject creates a new object from the given key-value pairs.
NewValue returns a new value.
NullValue returns a new null value.
ParseAndFind parses the path and finds matching nodes in the doc.
Search iterates all document nodes depth-first, and calls `capture` for those document nodes that `path` matches.
StringValue creates a new Value from a string.
Unmarshal the given byte slice to a json object model node.
UnmarshalIntf creates a JSON object model from a tree of objects, as output from json.Marshal.
UnmarshalReader unmarshals the input to a json object model node.

# Structs

Array represents a JSON array.
KeyValue represents a JSON key-value pair in an Object.
MapInterner uses a map to keep single copies of strings.
Object represents a JSON object, an ordered set of key-value pairs.
PathModel is the adapter for Node to use in JSON Path lookups.
A Value is one of: nil, string, bool, json.Number.

# Interfaces

Node is a node in a JSON object model.
StringInterner is used to reduce the memory footprint of a json document by interning the keys, so the same copy of string is used throughout the file.