Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
RFC 6902 references RFC 6901 where it chooses to define the paths in the patch operations as JSON Pointers. JSON Pointers can be encoded as strings, but they are conceptually a separate type, with very particular set of invariants that have to be maintained. This motivates defining a separate Rust type to represent them, as was done in the
jsonptr
crate, so that manipulations over these paths can be performed safely and with reusable code.While use of
String
s is sufficient to implement the required JSON Patch logic in this crate with minimal boilerplate, it makes it inconvenient for crate users to manipulate the patch operations outside of thediff
andmerge
APIs. For example, a manual implementation ofmerge
for a type other than aserde_json::Value
would have to either implement its own string manipulation utilities for extracting tokens from the paths for traversal, or convert to ajsonptr::Pointer
, which requires a clone. The reverse may also be required if the user crate intends to produce its own JSON Patches.By embracing the
jsonptr::Pointer
type as the representation for the operation paths in this crate, not only we make things simpler and more efficient at the consumer side, we also avoid implementing some error-prone logic in this crate, as it can take advantage of the useful APIs provided by thejsonptr
crate. The resulting code ends up being simpler, more elegant and easier to reason about on both sides.In short, why reinvent the wheel? If there's a perfectly good representation for JSON Pointers in the Rust open-source ecosystem, may as well converge on it.
Side changes
PatchOperation::path
method here for user convenience, since every operation has this field and it's useful for directing the operation to its appropriate location without need for matching explicitly on the operation variant.jsonptr
messages),