write method

  1. @override
Object? write(
  1. Object? document,
  2. Object? newValue
)
override

Returns a copy of document with the referenced value set to newValue.

When a non-existing Map key (JSON object member) is referenced, a new key will be created.

When a new index in a List (JSON Array) is referenced, a new element will be added to the list.

All intermediate keys and indexes will be created if possible. Example:

final pointer = JsonPointer('/foo/-/bar');
final doc = pointer.write({}, 42); // {foo:[{bar:42}]}

// However, the next call will fail with [BadRoute] since it's not possible
// to create a string key in an array
pointer.write([], 42);

Implementation

@override
Object? write(Object? document, Object? newValue) {
  final node = parent.read(document, orElse: _reference.emptyDocument);
  try {
    return parent.write(document, _reference.write(node, newValue));
  } on ReferenceFailure {
    throw BadRoute(this, document);
  }
}