dynjson

package module
v0.0.0-...-947a380 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2014 License: MIT Imports: 4 Imported by: 0

README

dynjson

Small library to allow for dynamic JSON access in GO

Make a node object with the encoded []byte data.

Call Node to get things out.

Convert to type with Convert methods.

import (
  "github.com/chrhlnd/dynjson"
  "os"
  "log"
)

func main() {
  dyn := dynjson.NewFromBytes([]byte(data))
  
  var node dynjson.DynNode
  var err error
  
  if node, err = dyn.Node("/some/path/that/is/cool"); err != nil {
    log.Errorf("My path didn't parse error %v", err)
    os.Exit(1)
  }
  
  var val string
  if val, err = node.Str(); err != nil {
    log.Errorf("My value wasn't a string? %v", err)
    os.Exit(1)
  }
}

See the dyn_test.go file for more usage.

n.SetVal will serialize into bytes for you, recomposing data up the Parent chain.

When you call Node (AsNode), it dynamiclly decodes the path. So if you have some big object buried in the hierarchy you should save out the parent node then query off that.

congress_books, err = dyn.Node("/library/congress/collection")

The array object is cached in the node, so it doesn't have to reparse that.

book_name, err = congress_books.Node("/3000/name")
book_name, err = congress_books.Node("/3001/name") 

Better then digging through all objects every time.

Objects are addressed by their key name. Arrays are addressed by their index number.

node.Node("/name/of/keys/in/json/object")
node.Node("/json/array/500")

The As'method's are for convience and panicing.

node.AsNode("/name/of/val").AsI64()

Will panic if path doesn't exist and if the value isn't parsable as I64

Mutation is handeled by SetVal, this back propagates in the doc tree, possibly nullifying other nodes if the data set doesn't contain them anymore. This is done with a version number that is incremented to the root. If an orphaned node is accessed it will find its not the right version then possibly orphan itself upon access.

{
	"name" : "parent"
	"children" : [ "zero", "one", "two" ]
}
child_zero := root.AsNode("/children/0")
child_two := root.AsNode("/children/2")

root.AsNode("/children").SetVal( []string{ "zero", "one" } )

if child_two.Root() == child_two {
	fmt.Printf("I'm an orphan")
}

if child_zero.Root() == root {
	fmt.Printf("I'm still in the graph whee")
}

=======

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DynNode

type DynNode interface {
	LocalPath() string
	FullPath() string
	Parent() DynNode
	Root() DynNode

	//Node Path format is: first rune is the delimter, between delimiters are key names
	//  Arrays use Numeric keys
	//         #0#customer#name
	//         /books/0/name
	//
	Node(path string) (DynNode, error)
	AsNode(path string) DynNode // panic

	IsNull() bool

	U64() (uint64, error)
	I64() (int64, error)
	F64() (float64, error)
	Str() (string, error)
	Bool() (bool, error)
	// Struct Calls json.Unmarshal for you
	Struct(out interface{}) error

	//Obj this node is an object get the cached object
	Obj() (map[string]json.RawMessage, error)
	// Ary if this node is an array get the cached object
	Ary() ([]json.RawMessage, error)

	// Len will return the count of elements if this is a collection type node
	Len() int

	AsU64() uint64  // panic
	AsI64() int64   // panic
	AsF64() float64 // panic
	AsStr() string  // panic
	AsBool() bool   // panic

	SetVal(v interface{}) error
	Data() []byte
}

func New

func New() DynNode

func NewFromBytes

func NewFromBytes(bytes []byte) DynNode

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL