|
8 | 8 | [](https://opensource.org/licenses/MIT)
|
9 | 9 |
|
10 | 10 | `jsonpatch` is a Go library to create JSON patches ([RFC6902](http://tools.ietf.org/html/rfc6902)) directly from arbitrary Go objects and facilitates the implementation of sophisticated custom (e.g. filtered, validated) patch creation.
|
| 11 | + |
| 12 | +## Basic Example |
| 13 | + |
| 14 | +```go |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/snorwin/jsonpatch" |
| 21 | +) |
| 22 | + |
| 23 | +type Person struct { |
| 24 | + Name string `json:"name"` |
| 25 | + Age int `json:"age"` |
| 26 | +} |
| 27 | + |
| 28 | +func main() { |
| 29 | + original := &Person{ |
| 30 | + Name: "John Doe", |
| 31 | + Age: 42, |
| 32 | + } |
| 33 | + updated := &Person{ |
| 34 | + Name: "Jane Doe", |
| 35 | + Age: 21, |
| 36 | + } |
| 37 | + |
| 38 | + patch, _, _ := jsonpatch.CreateJSONPatch(updated, original) |
| 39 | + fmt.Println(patch.String()) |
| 40 | +} |
| 41 | +``` |
| 42 | +```json |
| 43 | +[{"op":"replace","path":"/name","value":"Jane Doe"},{"op":"replace","path":"/age","value":21}] |
| 44 | +``` |
| 45 | + |
| 46 | +## Options |
| 47 | +### Filter patches using Predicates |
| 48 | +The option `WithPredicate` set a patch `Predicate` which can be used to filter or validate the patch creation. |
| 49 | +For each kind (`add`, `remove` and `replace`) of a patch a dedicated filter function can be configured. The |
| 50 | +predicate will be checked before a patch is created, or the JSON object is processed further. |
| 51 | + |
| 52 | +#### Example |
| 53 | +```go |
| 54 | +package main |
| 55 | + |
| 56 | +import ( |
| 57 | + "fmt" |
| 58 | + |
| 59 | + "github.com/snorwin/jsonpatch" |
| 60 | +) |
| 61 | + |
| 62 | +type Job struct { |
| 63 | + Position string `json:"position"` |
| 64 | + Company string `json:"company"` |
| 65 | + Volunteer bool `json:"volunteer"` |
| 66 | +} |
| 67 | + |
| 68 | +func main() { |
| 69 | + original := []Job{ |
| 70 | + {Position: "IT Trainer", Company: "Powercoders", Volunteer: true}, |
| 71 | + {Position: "Software Engineer", Company: "Github"}, |
| 72 | + } |
| 73 | + updated := []Job{ |
| 74 | + {Position: "Senior IT Trainer", Company: "Powercoders", Volunteer: true}, |
| 75 | + {Position: "Senior Software Engineer", Company: "Github"}, |
| 76 | + } |
| 77 | + |
| 78 | + patch, _, _ := jsonpatch.CreateJSONPatch(updated, original, jsonpatch.WithPredicate(jsonpatch.Funcs{ |
| 79 | + ReplaceFunc: func(pointer jsonpatch.JSONPointer, value, _ interface{}) bool { |
| 80 | + // only update volunteering jobs |
| 81 | + if job, ok := value.(Job); ok { |
| 82 | + return job.Volunteer |
| 83 | + } |
| 84 | + return true |
| 85 | + }, |
| 86 | + })) |
| 87 | + fmt.Println(patch.String()) |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +```json |
| 92 | +[{"op":"replace","path":"/0/position","value":"Senior IT Trainer"}] |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +### Create partial patches |
| 97 | +The option `WithPrefix` is used to specify a JSON pointer prefix if only a sub part of JSON structure needs to be patched, |
| 98 | +but the patch still need to be applied on the entire JSON object. |
| 99 | + |
| 100 | +#### Example |
| 101 | +```go |
| 102 | +package main |
| 103 | + |
| 104 | +import ( |
| 105 | + "fmt" |
| 106 | + |
| 107 | + "github.com/snorwin/jsonpatch" |
| 108 | +) |
| 109 | + |
| 110 | +type Person struct { |
| 111 | + Name string `json:"name"` |
| 112 | + Age int `json:"age"` |
| 113 | + Jobs []Job `json:"jobs"` |
| 114 | +} |
| 115 | + |
| 116 | +type Job struct { |
| 117 | + Position string `json:"position"` |
| 118 | + Company string `json:"company"` |
| 119 | + Volunteer bool `json:"volunteer"` |
| 120 | +} |
| 121 | + |
| 122 | +func main() { |
| 123 | + original := &Person{ |
| 124 | + Name: "John Doe", |
| 125 | + Age: 42, |
| 126 | + Jobs: []Job{{Position: "IT Trainer", Company: "Powercoders"}}, |
| 127 | + } |
| 128 | + updated := []Job{ |
| 129 | + {Position: "Senior IT Trainer", Company: "Powercoders", Volunteer: true}, |
| 130 | + {Position: "Software Engineer", Company: "Github"}, |
| 131 | + } |
| 132 | + |
| 133 | + patch, _, _ := jsonpatch.CreateJSONPatch(updated, original.Jobs, jsonpatch.WithPrefix(jsonpatch.ParseJSONPointer("/jobs"))) |
| 134 | + fmt.Println(patch.String()) |
| 135 | +} |
| 136 | +``` |
| 137 | +```json |
| 138 | +[{"op":"replace","path":"/jobs/0/position","value":"Senior IT Trainer"},{"op":"replace","path":"/jobs/0/volunteer","value":true},{"op":"add","path":"/jobs/1","value":{"position":"Software Engineer","company":"Github","volunteer":false}}] |
| 139 | +``` |
| 140 | + |
| 141 | +### Ignore slice order |
| 142 | +There are two options to ignore the slice order: |
| 143 | +- `IgnoreSliceOrder` will ignore the order of all slices of built-in in types (e.g. `int`, `string`) during the patch creation |
| 144 | + and will use instead the value itself in order to match and compare the current and modified JSON. |
| 145 | +- `IgnoreSliceOrderWithPattern` allows to specify for which slices the order should be ignored using JSONPointer patterns (e.g. `/jobs`, `/jobs/*`). |
| 146 | + Furthermore, the slice order of structs (and pointer of structs) slices can be ignored by specifying a JSON field which should be used |
| 147 | + to match the struct values. |
| 148 | + |
| 149 | +> NOTE: Ignoring the slice order only works if the elements (or the values used to match structs) are unique |
| 150 | +
|
| 151 | +#### Example |
| 152 | +```go |
| 153 | +package main |
| 154 | + |
| 155 | +import ( |
| 156 | + "fmt" |
| 157 | + |
| 158 | + "github.com/snorwin/jsonpatch" |
| 159 | +) |
| 160 | + |
| 161 | +type Person struct { |
| 162 | + Name string `json:"name"` |
| 163 | + Pseudonyms []string `json:"pseudonyms"` |
| 164 | + Jobs []Job `json:"jobs"` |
| 165 | +} |
| 166 | + |
| 167 | +type Job struct { |
| 168 | + Position string `json:"position"` |
| 169 | + Company string `json:"company"` |
| 170 | + Volunteer bool `json:"volunteer"` |
| 171 | +} |
| 172 | + |
| 173 | +func main() { |
| 174 | + original := Person{ |
| 175 | + Name: "John Doe", |
| 176 | + Pseudonyms: []string{"Jo", "JayD"}, |
| 177 | + Jobs: []Job{ |
| 178 | + {Position: "Software Engineer", Company: "Github"}, |
| 179 | + {Position: "IT Trainer", Company: "Powercoders"}, |
| 180 | + }, |
| 181 | + } |
| 182 | + updated := Person{ |
| 183 | + Name: "John Doe", |
| 184 | + Pseudonyms: []string{"Jonny", "Jo"}, |
| 185 | + Jobs: []Job{ |
| 186 | + {Position: "IT Trainer", Company: "Powercoders", Volunteer: true}, |
| 187 | + {Position: "Senior Software Engineer", Company: "Github"}, |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + patch, _, _ := jsonpatch.CreateJSONPatch(updated, original, |
| 192 | + jsonpatch.IgnoreSliceOrderWithPattern([]jsonpatch.IgnorePattern{{Pattern: "/*", JSONField: "company"}}), |
| 193 | + jsonpatch.IgnoreSliceOrder(), |
| 194 | + ) |
| 195 | + fmt.Println(patch.String()) |
| 196 | +} |
| 197 | +``` |
| 198 | +```json |
| 199 | +[{"op":"add","path":"/pseudonyms/2","value":"Jonny"},{"op":"remove","path":"/pseudonyms/1"},{"op":"replace","path":"/jobs/1/volunteer","value":true},{"op":"replace","path":"/jobs/0/position","value":"Senior Software Engineer"}] |
| 200 | +``` |
0 commit comments