Skip to content

benitogf/jsonpatch

 
 

Repository files navigation

jsonpatch

Test

As per http://jsonpatch.com/ JSON Patch is specified in RFC 6902 from the IETF.

an attempt to join the efforts made by mattbaird and evanphx

using jsondiff on the tests

Create Patch

package main

import (
	"fmt"
	"github.com/benitogf/jsonpatch"
)

var simpleA = `{"a":100, "b":200, "c":"hello"}`
var simpleB = `{"a":100, "b":200, "c":"goodbye"}`

func main() {
	patch, e := jsonpatch.CreatePatch([]byte(simpleA), []byte(simpleA))
	if e != nil {
		fmt.Printf("Error creating JSON patch:%v", e)
		return
	}
	for _, operation := range patch {
		fmt.Printf("%s\n", operation.Json())
	}
}

Apply Patch

package main

import (
	"fmt"
	"github.com/benitogf/jsonpatch"
)

func main() {
	original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
	patchJSON := []byte(`[
		{"op": "replace", "path": "/name", "value": "Jane"},
		{"op": "remove", "path": "/height"}
	]`)

	patch, err := jsonpatch.DecodePatch(patchJSON)
	if err != nil {
		panic(err)
	}

	modified, err := patch.Apply(original)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Original document: %s\n", original)
	fmt.Printf("Modified document: %s\n", modified)
}

About

As per http://jsonpatch.com/ JSON Patch is specified in RFC 6902 from the IETF.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%