Documentation ¶
Overview ¶
The jsontools package provides several functions which parse a json 'blob' into either a one dimensional map, or into a tree structure which may be 'walked'.
Index ¶
- Constants
- func Defrock_2_jif(iblob interface{}) (jif interface{}, err error)
- func Frock_jmap(jif interface{}) (json string)
- func Jif2map(hashtab map[string]interface{}, ithing interface{}, depth int, ptag string, ...)
- func Json2blob(json_blob []byte, root_tag *string, printit bool) (jif interface{}, err error)
- func Json2map(json_blob []byte, root_tag *string, printit bool) (symtab map[string]interface{}, err error)
- func Print(stuff interface{}, ptag string, dotted bool)
- func Refrock(jblob interface{}) (string, error)
- type Jsoncache
- type Jtree
- func (j *Jtree) Dump()
- func (j *Jtree) Frock() (jstr string)
- func (j *Jtree) Get_bool(name string) (bv bool, ok bool)
- func (j *Jtree) Get_ele_bool(name string, idx int) (bv bool, ok bool)
- func (j *Jtree) Get_ele_count(name string) int
- func (j *Jtree) Get_ele_float(name string, idx int) (float64, bool)
- func (j *Jtree) Get_ele_if(name string, idx int) *interface{}
- func (j *Jtree) Get_ele_int(name string, idx int) (int64, bool)
- func (j *Jtree) Get_ele_string(name string, idx int) *string
- func (j *Jtree) Get_ele_subtree(name string, idx int) (*Jtree, bool)
- func (j *Jtree) Get_field_if(ifname interface{}) interface{}
- func (j *Jtree) Get_float(name string) (float64, bool)
- func (j *Jtree) Get_fnames() (fnames []string)
- func (j *Jtree) Get_int(name string) (int64, bool)
- func (j *Jtree) Get_string(name string) *string
- func (j *Jtree) Get_subtree(name string) (*Jtree, bool)
- func (j *Jtree) Has_field(name string) bool
- func (j *Jtree) List_fields() (flist string)
- func (jt *Jtree) Pretty_print(target io.Writer)
Constants ¶
const ( PRINT bool = true NOPRINT bool = false )
users can pass on calls to make their code more readable
Variables ¶
This section is empty.
Functions ¶
func Defrock_2_jif ¶
func Defrock_2_jif(iblob interface{}) (jif interface{}, err error)
Defrock_2_jif takes either json input (as string, *string or []byte), or a map[]interface{} and completely defrock it into a json inteface (jif). This assumes that some strings may be json which should be recursively defrocked. Yes, what system might actually embed json as a string in json.... Openstack; grrrr.
func Frock_jmap ¶
func Frock_jmap(jif interface{}) (json string)
Frock_jmap accepts a jmap (keyed interface values) and generates the corresponding formatted (frocked) json in a string.
func Jif2map ¶
func Jif2map(hashtab map[string]interface{}, ithing interface{}, depth int, ptag string, printit bool)
Builds a flat map from the interface 'structure', aka jif, (ithing) passed in putting into hashtab. The map names generated for hashtab are dot separated. For example, the json {foo:"foo-val", bar:["bar1", "bar2"] } would generate the following names in the map:
root.foo, root.bar[0], root.bar[1] root.bar where 'root' is the initial ptag passed in.
If an array element is an object, e.g. bar:[ {a:valuea,b:valueb}, "bar2" ], then the following keys in the map are generated:
root.bar[0], root.bar[0].a, root.bar[0].b, root.bar[1]
The root.bar[0] key references the actual interface for the element which is an object allowing that element to be teased out by the caller if needed (it can be passed to Jif2map() to generate a map just of that object.
Alternateive: see jsontree.go
func Json2blob ¶
Accepts the json "blob" as a byte string, probably just read off of the wire. A generic interface is returned and if the printit parameter is true, then a representation of the json is printed to the standard output device.
func Json2map ¶
func Json2map(json_blob []byte, root_tag *string, printit bool) (symtab map[string]interface{}, err error)
Accepts the json blob as a byte string, probably just read off of the wire, and builds a symbol table. If the printit parm is true, then the representation of the json is written to the standard output in addition to the generation of the map.
func Print ¶
A simple method that allows an interface representation to easily be printed in either hierarchical or "dotted" format.
func Refrock ¶
--------------------- public ----------------------------------------------------------------------
Refrock takes json in the form of string, *string or []byte and unencapsulate any embedded strings which are json. The result is a string with valid json but with any embedded json strings in the original made into a part of the resulting 'hierarchy'. This is a two step process which simply uses the Defrock_2_jif() and Frock_jmap() functions below.
Types ¶
type Jsoncache ¶
type Jsoncache struct {
// contains filtered or unexported fields
}
func Mk_jsoncache ¶
func Mk_jsoncache() (jc *Jsoncache)
type Jtree ¶
type Jtree struct {
// contains filtered or unexported fields
}
We return a simple struct which points to the interface that was generated by the unmarshal process. This allows for object function calls rather than passing the object as parm 1.
func Json2tree ¶
Json2tree accepts the 'raw' json interface created by unmarshal, convert it into our base struct and return a pointer to the struct.
func (*Jtree) Get_bool ¶
Get_bool returns the value associated with a boolean; ok is false if the value isn't booliean or doesn't exist and the value returned is undefined.
func (*Jtree) Get_ele_bool ¶
Get_ele_bool returns the value associated with a boolean; ok is false if the value isn't booliean or doesn't exist or there is a range error.
func (*Jtree) Get_ele_count ¶
Get_ele_count returns the number of elements in the array name or -1 if not an array.
func (*Jtree) Get_ele_float ¶
Get_ele_float returns the float element from the array. ok is false if:
name isn't defined in the tree OR name isn't an array OR idx is out of range for name OR a[idx] cannot be converted into a float
func (*Jtree) Get_ele_if ¶
Get_ele_if fetches the element in the named array if not out of range. Returns nil if:
name is not in the tree OR name is not an array OR idx is out of range
func (*Jtree) Get_ele_int ¶
Get_ele_int returns the int element from the array. ok is false if:
name isn't defined in the tree OR name isn't an array OR idx is out of range for name OR a[idx] cannot be converted into an integer
func (*Jtree) Get_ele_string ¶
Get_ele_string returns the *string element from the array. Nil is returned if:
name isn't defined in the tree OR name isn't an array OR idx is out of range for name OR a[idx] isn't a string
func (*Jtree) Get_ele_subtree ¶
Get_ele_subtree returns the element of an array as a pointer to a subtree. Returns nil and !ok if:
name is not known OR name is not an array OR index is out of range OR element is not an 'object'
func (*Jtree) Get_field_if ¶
func (j *Jtree) Get_field_if(ifname interface{}) interface{}
Get_field_if is a generic getvalue -- it returns the value as an interface; caller must figure out type.
func (*Jtree) Get_float ¶
Get_float looks up the name and if it's a float return the value. bool in return is set to true if found.
func (*Jtree) Get_fnames ¶
Get_keys returns an array of field names which are available at the current top level of the tree.
func (*Jtree) Get_int ¶
Get_int looks up name and return integer value. We assume unmarshall saves all values as float.
func (*Jtree) Get_string ¶
Look up name and return a pointer to it if it is a string.
func (*Jtree) Get_subtree ¶
Get_subtree returns the Jtree associated with the named field.
func (*Jtree) Has_field ¶
Has_field confirms that the field exists. Returns true if the named field exists in the json mess.
func (*Jtree) List_fields ¶
---- debugging ---------------------------------------------------------
Generate a list of fields in the current tree.
func (*Jtree) Pretty_print ¶
Pretty_print will pretty print the json tree. Arrays printed in order, rest is up to the whim of the hash function that delivers things from the map.