gojsonpointer

package module
v0.0.0-...-02993c4 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2019 License: Apache-2.0 Imports: 5 Imported by: 40

README

gojsonpointer

An implementation of JSON Pointer - Go language

Usage

jsonText := `{
	"name": "Bobby B",
	"occupation": {
		"title" : "King",
		"years" : 15,
		"heir" : "Joffrey B"			
	}
}`

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

//create a JSON pointer
pointerString := "/occupation/title"
pointer, _ := NewJsonPointer(pointerString)

//SET a new value for the "title" in the document     
pointer.Set(jsonDocument, "Supreme Leader of Westeros")

//GET the new "title" from the document
title, _, _ := pointer.Get(jsonDocument)
fmt.Println(title) //outputs "Supreme Leader of Westeros"

//DELETE the "heir" from the document
deletePointer := NewJsonPointer("/occupation/heir")
deletePointer.Delete(jsonDocument)

b, _ := json.Marshal(jsonDocument)
fmt.Println(string(b))
//outputs `{"name":"Bobby B","occupation":{"title":"Supreme Leader of Westeros","years":15}}`

References

https://tools.ietf.org/html/rfc6901

Note

The 4.Evaluation part of the previous reference, starting with 'If the currently referenced value is a JSON array, the reference token MUST contain either...' is not implemented.

Documentation

Index

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
}

func NewJsonPointer

func NewJsonPointer(jsonPointerString string) (p JsonPointer, err error)

NewJsonPointer parses the given string JSON pointer and returns an object

func (*JsonPointer) Delete

func (p *JsonPointer) Delete(document interface{}) (interface{}, error)

Uses the pointer to delete a value from a JSON document

func (*JsonPointer) Get

func (p *JsonPointer) Get(document interface{}) (interface{}, reflect.Kind, error)

Uses the pointer to retrieve a value from a JSON document

func (*JsonPointer) Set

func (p *JsonPointer) Set(document interface{}, value interface{}) (interface{}, error)

Uses the pointer to update a value from a JSON document

func (*JsonPointer) String

func (p *JsonPointer) String() string

Pointer to string representation function

Jump to

Keyboard shortcuts

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