jsonptr

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 28, 2019 License: BSD-3-Clause Imports: 7 Imported by: 9

README

jsonptr Travis-CI Go Report Card GoDoc Sourcegraph Coverage

An implementation of rfc6901 (json pointer) in Go (golang)

example

Example json

{
	"details":{
		"name":"hello",
		"id":123,
		"num":3.14,
		"flag":true,
		"author":"A Name",
		"nested":{
			"even":"more",
			"id":4,
			"list":["1","2","3"]
	
		},
		"nice":null
	},
	"msg":"hello"
}

Api usage

// get an array entry
num,err := jsonptr.Evaluate(obj, "/details/nested/list/2") // returns string value 3

// get an array entry and parse it
i, err := jsonptr.AsInt(obj,"/details/nested/list/0") // returns int value 1

In case of evaluation errors while resolving the json pointer, you will get nice error messages.

     key 'abc' not found:
        /abc/asd
         ^~~~~~~ available keys: (details|msg)
     key 'asd' not found:
        /details/asd
                 ^~~~ available keys: (author|flag|id|name|nested|nice|num)
     key 'x' not found:
        /details/nested/x
                        ^~ available keys: (even|id|list)
     index out of bounds:
        /details/nested/list/4
                             ^~ index must be in [0...3[
     expected integer index:
        /details/nested/list/a4
                             ^~~ index must be in [0...3[
     key 'a' not found:
        /a/b/c/d
         ^~~~~ object is nil
     key 'x' not found:
        /details/nice/x
                      ^~ object is nil

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsArray

func AsArray(objOrArr interface{}, ptr JSONPointer) ([]interface{}, error)

AsArray evaluates the JSONPointer and unwraps either []interface{} or *[]interface{} slices. Any other slice types are unboxed to []interface{}. If value is not a slice type at all, the value is inserted into a one-element interface slice.

  • only evaluation errors are returned as errors.

func AsBool

func AsBool(objOrArr interface{}, ptr JSONPointer) (bool, error)

AsBool takes a JSONPointer and tries to interpret the result as a bool. Anything else is interpreted as false.

  • only evaluation errors are returned as errors.

func AsFloat32 added in v0.0.2

func AsFloat32(objOrArr interface{}, ptr JSONPointer) (float32, error)

AsFloat32 takes a JSONPointer and tries to interpret the result as a float. The following rules are applied:

  • numbers are converted to a float
  • booleans are converted to 1|0
  • null is converted NaN
  • a non-resolvable value, returns also NaN
  • arrays and objects are converted to NaN
  • String() method is invoked, if available, and output parsed. Returns NaN if not parsable.
  • Anything else is converted using sprintf and %v directive and tried to be parsed. Returns NaN if not parsable.
  • only evaluation errors are returned as errors.

func AsFloat64 added in v0.0.2

func AsFloat64(objOrArr interface{}, ptr JSONPointer) (float64, error)

AsFloat64 takes a JSONPointer and tries to interpret the result as a float. The following rules are applied:

  • numbers are converted to a float
  • booleans are converted to 1|0
  • null is converted NaN
  • a non-resolvable value, returns also NaN
  • arrays and objects are converted to NaN
  • String() method is invoked, if available, and output parsed. Returns NaN if not parsable.
  • Anything else is converted using sprintf and %v directive and tried to be parsed. Returns NaN if not parsable.
  • only evaluation errors are returned as errors.

func AsFloat64Array added in v0.0.2

func AsFloat64Array(objOrArr interface{}, ptr JSONPointer) ([]float64, error)

AsFloat64Array evaluates the JSONPointer and tries to interpret any slice value as float (see AsFloat) for rules

  • only evaluation errors are returned as errors.

func AsInt

func AsInt(objOrArr interface{}, ptr JSONPointer) (int, error)

AsInt takes a JSONPointer and tries to interpret the result as an int using the rules of AsFloat. NaN is treated as 0.

  • only evaluation errors are returned as errors.

func AsInt32 added in v0.0.2

func AsInt32(objOrArr interface{}, ptr JSONPointer) (int32, error)

AsInt32 takes a JSONPointer and tries to interpret the result as an int using the rules of AsFloat. NaN is treated as 0.

  • only evaluation errors are returned as errors.

func AsInt64 added in v0.0.2

func AsInt64(objOrArr interface{}, ptr JSONPointer) (int64, error)

AsInt64 takes a JSONPointer and tries to interpret the result as an int using the rules of AsFloat. NaN is treated as 0.

  • only evaluation errors are returned as errors.

func AsIntArray

func AsIntArray(objOrArr interface{}, ptr JSONPointer) ([]int, error)

AsIntArray evaluates the JSONPointer and uses the AsFloatArray conversion rules but truncates to int. JSON does not support integers at all, just floats anyway. NaN values are treated as 0.

  • only evaluation errors are returned as errors.

func AsObject added in v0.0.2

func AsObject(objOrArr interface{}, ptr JSONPointer) (map[string]interface{}, error)

AsArray evaluates the JSONPointer and unwraps map[string]interface{}. Any other slice types are unboxed to []interface{}. If value is not a map type at all, the value is inserted into a one-element map with the key value.

  • only evaluation errors are returned as errors.

func AsString

func AsString(objOrArr interface{}, ptr JSONPointer) (string, error)

AsString takes a JSONPointer and tries to interpret the result as a string. The following rules are applied:

  • numbers are converted to the according string representation
  • booleans are converted to true|false
  • null is converted to the empty string
  • a non-resolvable value, returns the empty string
  • arrays and objects are converted into a json string
  • String() methods are used, if available
  • Anything else is converted using sprintf and %v directive
  • only evaluation errors are returned as errors.

func AsStringArray

func AsStringArray(objOrArr interface{}, ptr JSONPointer) ([]string, error)

AsStringArray evaluates the JSONPointer and tries to interpret any slice value as string (see AsString) for rules

  • only evaluation errors are returned as errors.

func Evaluate

func Evaluate(objOrArr interface{}, ptr JSONPointer) (interface{}, error)

Evaluate takes the json pointer and applies it to the given json object or array. Returns an error if the json pointer cannot be resolved.

func MustArray added in v0.0.2

func MustArray(v []interface{}, err error) []interface{}

MustArray is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustBool added in v0.0.2

func MustBool(v bool, err error) bool

MustBool is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustFloat32 added in v0.0.2

func MustFloat32(v float32, err error) float32

MustFloat32 is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustFloat64 added in v0.0.2

func MustFloat64(v float64, err error) float64

MustFloat64 is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustInt added in v0.0.2

func MustInt(v int, err error) int

MustInt is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustInt32 added in v0.0.2

func MustInt32(v int32, err error) int32

MustInt32 is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustInt64 added in v0.0.2

func MustInt64(v int64, err error) int64

MustInt32 is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustObject added in v0.0.2

func MustObject(v map[string]interface{}, err error) map[string]interface{}

MustObject is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustString added in v0.0.2

func MustString(v string, err error) string

MustString is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func MustStringArray added in v0.0.2

func MustStringArray(v []string, err error) []string

MustStringArray is a go-like "expect macro" pattern. Panics in case of error, otherwise returns v.

func TryArray added in v0.0.2

func TryArray(v []interface{}, err error) []interface{}

TryArray is a go-like "try macro" pattern. Returns nil in case of error, otherwise v.

func TryBool added in v0.0.2

func TryBool(v bool, err error) bool

TryBool is a go-like "try macro" pattern. Returns false in case of error, otherwise v.

func TryFloat32 added in v0.0.2

func TryFloat32(v float32, err error) float32

TryFloat32 is a go-like "try macro" pattern. Returns +Inf in case of error, otherwise v.

func TryFloat64 added in v0.0.2

func TryFloat64(v float64, err error) float64

TryFloat64 is a go-like "try macro" pattern. Returns NaN in case of error, otherwise v.

func TryInt added in v0.0.2

func TryInt(v int, err error) int

TryInt is a go-like "try macro" pattern. Returns 0 in case of error, otherwise v.

func TryInt32 added in v0.0.2

func TryInt32(v int32, err error) int32

TryInt32 is a go-like "try macro" pattern. Returns 0 in case of error, otherwise v.

func TryInt64 added in v0.0.2

func TryInt64(v int64, err error) int64

TryInt32 is a go-like "try macro" pattern. Returns 0 in case of error, otherwise v.

func TryObject added in v0.0.2

func TryObject(v map[string]interface{}, err error) map[string]interface{}

TryObject is a go-like "try macro" pattern. Returns empty map in case of error, otherwise v.

func TryString added in v0.0.2

func TryString(v string, err error) string

TryString is a go-like "try macro" pattern. Returns the empty string in case of error, otherwise v.

func TryStringArray added in v0.0.2

func TryStringArray(v []string, err error) []string

TryStringArray is a go-like "try macro" pattern. Returns nil in case of error, otherwise v.

func Unescape

func Unescape(str JSONPointerToken) string

Unescape takes a token and returns the original string. ~0 becomes ~ and ~1 becomes /

Types

type JSONPointer

type JSONPointer = string

A JSONPointer specifies a specific value within a JSON document. See https://tools.ietf.org/html/rfc6901 for the specification.

type JSONPointerToken

type JSONPointerToken = string

A JSONPointerToken is a single element or token of a JSONPointer

func Escape

func Escape(str string) JSONPointerToken

Escape takes any string and returns a token. ~ becomes ~0 and / becomes ~1

Jump to

Keyboard shortcuts

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