jsonptr

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: BSD-3-Clause Imports: 7 Imported by: 8

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 Put added in v0.0.4

func Put(objOrArr ObjOrArr, ptr Ptr, value Value) error

Put tries to resolve the given pointer to put the given value. If ptr addresses an array, an index of -1 will just append the value. Otherwise, the array is filled up with nil values. Note that any key which looks like an integer is considered to be an array index and not an object key.

func Unescape

func Unescape(str PtrToken) string

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

Types

type Arr added in v0.0.4

type Arr struct {
	// contains filtered or unexported fields
}

func NewArr added in v0.0.4

func NewArr(slice ...Value) *Arr

func (*Arr) Append added in v0.0.4

func (a *Arr) Append(v ...Value) *Arr

func (*Arr) Bool added in v0.0.4

func (a *Arr) Bool() bool

func (*Arr) Float64 added in v0.0.4

func (a *Arr) Float64() float64

func (*Arr) Get added in v0.0.4

func (a *Arr) Get(idx int) Value

func (*Arr) Len added in v0.0.4

func (a *Arr) Len() int

func (*Arr) MarshalJSON added in v0.0.4

func (a *Arr) MarshalJSON() ([]byte, error)

func (*Arr) SetAt added in v0.0.4

func (a *Arr) SetAt(idx int, v Value)

func (*Arr) String added in v0.0.4

func (a *Arr) String() string

func (*Arr) UnmarshalJSON added in v0.0.4

func (a *Arr) UnmarshalJSON(bytes []byte) error

type Bool added in v0.0.4

type Bool bool

func (Bool) Bool added in v0.0.4

func (n Bool) Bool() bool

func (Bool) Float64 added in v0.0.4

func (n Bool) Float64() float64

func (Bool) String added in v0.0.4

func (n Bool) String() string

type Null added in v0.0.4

type Null struct {
}

func (Null) Bool added in v0.0.4

func (n Null) Bool() bool

func (Null) Float64 added in v0.0.4

func (n Null) Float64() float64

func (Null) MarshalJSON added in v0.0.4

func (n Null) MarshalJSON() ([]byte, error)

func (Null) String added in v0.0.4

func (n Null) String() string

func (Null) UnmarshalJSON added in v0.0.4

func (n Null) UnmarshalJSON(bytes []byte) error

type Number added in v0.0.4

type Number float64

func (Number) Bool added in v0.0.4

func (n Number) Bool() bool

func (Number) Float64 added in v0.0.4

func (n Number) Float64() float64

func (Number) String added in v0.0.4

func (n Number) String() string

type Obj added in v0.0.4

type Obj struct {
	// contains filtered or unexported fields
}

func NewObj added in v0.0.6

func NewObj(m map[string]Value) *Obj

func (*Obj) All added in v0.0.5

func (o *Obj) All() iter.Seq2[string, Value]

func (*Obj) Bool added in v0.0.4

func (o *Obj) Bool() bool

func (*Obj) Float64 added in v0.0.4

func (o *Obj) Float64() float64

func (*Obj) Get added in v0.0.5

func (o *Obj) Get(k string) (Value, bool)

func (*Obj) GetOr added in v0.0.6

func (o *Obj) GetOr(k string, defaultValue Value) Value

func (*Obj) Keys added in v0.0.5

func (o *Obj) Keys() []string

func (*Obj) Len added in v0.0.5

func (o *Obj) Len() int

func (*Obj) MarshalJSON added in v0.0.6

func (o *Obj) MarshalJSON() ([]byte, error)

func (*Obj) Put added in v0.0.5

func (o *Obj) Put(k string, v Value)

func (*Obj) String added in v0.0.4

func (o *Obj) String() string

func (*Obj) UnmarshalJSON added in v0.0.4

func (o *Obj) UnmarshalJSON(bytes []byte) error

type ObjOrArr added in v0.0.4

type ObjOrArr interface {
	Value
	// contains filtered or unexported methods
}

type Primitive added in v0.0.4

type Primitive interface {
	// contains filtered or unexported methods
}

type Ptr added in v0.0.4

type Ptr = string

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

type PtrToken added in v0.0.4

type PtrToken = string

A PtrToken is a single element or token of a Ptr

func Escape

func Escape(str string) PtrToken

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

type String added in v0.0.4

type String string

func (String) Bool added in v0.0.4

func (n String) Bool() bool

func (String) Float64 added in v0.0.4

func (n String) Float64() float64

func (String) String added in v0.0.4

func (n String) String() string

type Value added in v0.0.4

type Value interface {
	String() string
	Bool() bool
	Float64() float64
	// contains filtered or unexported methods
}

func Eval added in v0.0.4

func Eval(objOrArr ObjOrArr, ptr Ptr) (Value, error)

Eval 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 MustEval added in v0.0.4

func MustEval(objOrArr ObjOrArr, ptr Ptr) Value

func ValueOf added in v0.0.4

func ValueOf(from any) Value

ValueOf deep copies any basic go value type into the according json type. Unsupported values are mapped to Null. Any Value can be marshaled directly to JSON.

Jump to

Keyboard shortcuts

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