ptr

package module
v0.0.0-...-49fc53f Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2020 License: Apache-2.0 Imports: 5 Imported by: 2

README

JavaScript Object Notation (JSON) Pointer

JSON Pointer defines a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document.


A JSON Pointer is a Unicode string (see RFC4627, Section 3) containing a sequence of zero or more reference tokens, each prefixed by a / (%x2F) character.

Source: RFC6901

Usage

The example below return the first element of the array foo contained within the (unmarshalled) JSON document.

var jsonDocument map[string]interface{}
_ = json.Unmarshal([]byte(JSON), &jsonDocument)

jsonPtr, _ := ptr.New("/foo/0")
value, err := jsonPtr.Get(jsonDocument)

ABNF Representation
json-pointer    = *( "/" reference-token )
reference-token = *( unescaped / escaped )
unescaped       = %x00-2E / %x30-7D / %x7F-10FFFF
escaped         = "~" ( "0" / "1" )

array-index     = %x30 / ( %x31-39 *(%x30-39) )

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONPointer

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

JSONPointer represents a JavaScript Object Notation (JSON) Pointer.

func New

func New(ptr string) (JSONPointer, error)

New creates a JSON Pointer based on the given string.

A JSON Pointer can be represented in a JSON string value. All instances of quotation mark '"' (%x22), reverse solidus '\' (%x5C), and control (%x00-1F) characters MUST be escaped.

func (*JSONPointer) Delete

func (ptr *JSONPointer) Delete(document interface{}) (interface{}, error)

Delete removes the value corresponding with the JSON Pointer.

If the document is an array and the pointer removes an element at the root level, an error will be returned. Since the document can not be updated since the array needs te be recreated. (see examples)

Example
package main

import (
	"fmt"

	ptr "github.com/oas3/json-pointer"
)

func main() {
	doc := map[string]interface{}{
		"foo": []interface{}{"bar", "baz"},
	}
	p, _ := ptr.New("/foo/0")
	r, _ := p.Delete(doc)
	fmt.Println(doc)
	fmt.Println(r)

}
Output:
map[foo:[baz]]
bar
Example (Error)
package main

import (
	"fmt"

	ptr "github.com/oas3/json-pointer"
)

func main() {
	doc := []interface{}{"bar", "baz"}
	p, _ := ptr.New("/0")
	_, err := p.Delete(doc)
	fmt.Println(err)

}
Output:
can not delete from an array at root level

func (*JSONPointer) Get

func (ptr *JSONPointer) Get(document interface{}) (interface{}, error)

Get returns the value corresponding with the JSON Pointer.

Example
package main

import (
	"fmt"

	ptr "github.com/oas3/json-pointer"
)

func main() {
	doc := map[string]interface{}{
		"foo": []interface{}{"bar", "baz"},
	}

	var p ptr.JSONPointer

	p, _ = ptr.New("")
	r0, _ := p.Get(doc)
	fmt.Println(r0)

	p, _ = ptr.New("/foo")
	r1, _ := p.Get(doc)
	fmt.Println(r1)

	p, _ = ptr.New("/foo/0")
	r2, _ := p.Get(doc)
	fmt.Println(r2)

}
Output:
map[foo:[bar baz]]
[bar baz]
bar

func (*JSONPointer) Set

func (ptr *JSONPointer) Set(value, document interface{}) (interface{}, reflect.Kind, error)

Set assigns a the given value to the JSON Pointers location.

func (*JSONPointer) String

func (ptr *JSONPointer) String() string

Jump to

Keyboard shortcuts

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