Zum Hauptinhalt springen

Anhang A. Beispiele (Appendix A. Examples)

A.1. Hinzufügen eines Objektmitglieds (Adding an Object Member)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": "bar"}

JSON Patch-Dokument:

[
{ "op": "add", "path": "/baz", "value": "qux" }
]

Resultierendes JSON-Dokument:

{
"baz": "qux",
"foo": "bar"
}

A.2. Hinzufügen eines Array-Elements (Adding an Array Element)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": [ "bar", "baz" ] }

JSON Patch-Dokument:

[
{ "op": "add", "path": "/foo/1", "value": "qux" }
]

Resultierendes JSON-Dokument:

{ "foo": [ "bar", "qux", "baz" ] }

A.3. Entfernen eines Objektmitglieds (Removing an Object Member)

Beispiel eines Ziel-JSON-Dokuments:

{
"baz": "qux",
"foo": "bar"
}

JSON Patch-Dokument:

[
{ "op": "remove", "path": "/baz" }
]

Resultierendes JSON-Dokument:

{ "foo": "bar" }

A.4. Entfernen eines Array-Elements (Removing an Array Element)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": [ "bar", "qux", "baz" ] }

JSON Patch-Dokument:

[
{ "op": "remove", "path": "/foo/1" }
]

Resultierendes JSON-Dokument:

{ "foo": [ "bar", "baz" ] }

A.5. Ersetzen eines Werts (Replacing a Value)

Beispiel eines Ziel-JSON-Dokuments:

{
"baz": "qux",
"foo": "bar"
}

JSON Patch-Dokument:

[
{ "op": "replace", "path": "/baz", "value": "boo" }
]

Resultierendes JSON-Dokument:

{
"baz": "boo",
"foo": "bar"
}

A.6. Verschieben eines Werts (Moving a Value)

Beispiel eines Ziel-JSON-Dokuments:

{
"foo": {
"bar": "baz",
"waldo": "fred"
},
"qux": {
"corge": "grault"
}
}

JSON Patch-Dokument:

[
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
]

Resultierendes JSON-Dokument:

{
"foo": {
"bar": "baz"
},
"qux": {
"corge": "grault",
"thud": "fred"
}
}

A.7. Verschieben eines Array-Elements (Moving an Array Element)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": [ "all", "grass", "cows", "eat" ] }

JSON Patch-Dokument:

[
{ "op": "move", "from": "/foo/1", "path": "/foo/3" }
]

Resultierendes JSON-Dokument:

{ "foo": [ "all", "cows", "eat", "grass" ] }

A.8. Testen eines Werts: Erfolg (Testing a Value: Success)

Beispiel eines Ziel-JSON-Dokuments:

{
"baz": "qux",
"foo": [ "a", 2, "c" ]
}

JSON Patch-Dokument, das zu einer erfolgreichen Auswertung führt:

[
{ "op": "test", "path": "/baz", "value": "qux" },
{ "op": "test", "path": "/foo/1", "value": 2 }
]

A.9. Testen eines Werts: Fehler (Testing a Value: Error)

Beispiel eines Ziel-JSON-Dokuments:

{ "baz": "qux" }

JSON Patch-Dokument, das zu einer Fehlerbedingung führt:

[
{ "op": "test", "path": "/baz", "value": "bar" }
]

A.10. Hinzufügen eines verschachtelten Mitgliedsobjekts (Adding a Nested Member Object)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": "bar" }

JSON Patch-Dokument:

[
{ "op": "add", "path": "/child", "value": { "grandchild": { } } }
]

Resultierendes JSON-Dokument:

{
"foo": "bar",
"child": {
"grandchild": {
}
}
}

A.11. Ignorieren nicht erkannter Elemente (Ignoring Unrecognized Elements)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": "bar" }

JSON Patch-Dokument:

[
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
]

Resultierendes JSON-Dokument:

{
"foo": "bar",
"baz": "qux"
}

A.12. Hinzufügen zu einem nicht existierenden Ziel (Adding to a Nonexistent Target)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": "bar" }

JSON Patch-Dokument:

[
{ "op": "add", "path": "/baz/bat", "value": "qux" }
]

Dieses JSON Patch-Dokument, angewendet auf das obige Ziel-JSON-Dokument, würde zu einem Fehler führen (daher würde es nicht angewendet), da die Zielstelle der "add"-Operation weder die Wurzel des Dokuments noch ein Mitglied eines vorhandenen Objekts noch ein Mitglied eines vorhandenen Arrays referenziert.

A.13. Ungültiges JSON Patch-Dokument (Invalid JSON Patch Document)

JSON Patch-Dokument:

[
{ "op": "add", "path": "/baz", "value": "qux", "op": "remove" }
]

Dieses JSON Patch-Dokument kann nicht als "add"-Operation behandelt werden, da es ein späteres "op":"remove"-Element enthält. JSON erfordert, dass Objektmitgliedsnamen mit einer "SHOULD"-Anforderung eindeutig sind, und es gibt keine standardmäßige Fehlerbehandlung für Duplikate.

A.14. ~ Escape-Reihenfolge (~ Escape Ordering)

Beispiel eines Ziel-JSON-Dokuments:

{
"/": 9,
"~1": 10
}

JSON Patch-Dokument:

[
{"op": "test", "path": "/~01", "value": 10}
]

Resultierendes JSON-Dokument:

{
"/": 9,
"~1": 10
}

A.15. Vergleich von Zeichenketten und Zahlen (Comparing Strings and Numbers)

Beispiel eines Ziel-JSON-Dokuments:

{
"/": 9,
"~1": 10
}

JSON Patch-Dokument:

[
{"op": "test", "path": "/~01", "value": "10"}
]

Dies führt zu einem Fehler, da der Test fehlschlägt. Der Dokumentwert ist numerisch, während der getestete Wert eine Zeichenkette ist.

A.16. Hinzufügen eines Array-Werts (Adding an Array Value)

Beispiel eines Ziel-JSON-Dokuments:

{ "foo": ["bar"] }

JSON Patch-Dokument:

[
{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] }
]

Resultierendes JSON-Dokument:

{ "foo": ["bar", ["abc", "def"]] }