write

fun JsonObject.write(pointer: JsonPointer, value: JsonElement): JsonObject

value written at pointer, returning a new data model.

The semantics are the specification's, not JSON Patch's:

  • Upsert. A pointer that already exists is replaced; one that does not is created, along with any missing containers on the way to it.

  • Null deletes. An explicit null removes what pointer addresses — a member from an object, an element from an array (shifting the rest down). There is therefore no way for an updateDataModel to store a JSON null; the specification spends null on the delete verb. A delete of something pointer does not address changes nothing at all: it neither creates the containers on the way to it nor disturbs whatever stands in the path.

  • Root. A JsonPointer.ROOT write replaces the whole data model, and a root delete empties it. The replacement must be an object, because that is what the schema types a data model as.

Missing containers are created as objects — on a write that stores a value; see the delete rule above — even when the next token looks like an array index. /items/0 against an absent items is genuinely ambiguous, and choosing an object keeps the write reversible: a later write of an actual array at /items replaces the object outright, whereas guessing an array would make {"0": ...} unrepresentable.