Construct a DeltaVisitor
the old value
the new value
the parts of the second JSON value not found in the first JSON value. This map should be empty. It will be filled by the DeltaVisitor.
the parts of the first JSON value not found in the second JSON value. This map should be empty. It will be filled by the DeltaVisitor.
a function to return a JSON object from a part of a BufferSlice.
a function to return a JSON object from a copy of part of a BufferSlice.
Return a JSON object from a part of a BufferSlice. The buffer is copied before being passed to the JSON constructor.
The parts of the second JSON value not found in the first JSON value.
The split structures that are part of the new JSON value not found in the old JSON value
The newParser position where the next match or insert will start,
which is the cumulative length of the match and insert edits. May be
less than or greater than oldOffset. After each edit is processed,
newParser.nextByte() >= newOffset.
The span parser traversing the new value
The new value
The end of the last match edit. After each edit is processed,
oldParser.nextByte() >= oldOffset.
The span parser traversing the old value
The old value
Return a JSON object from a part of a BufferSlice.
A JSON pointer that is part of the new value but hasn't yet been found in the old value.
The value associated with the pendingInsert pointer.
A JSON pointer that is part of the old value but hasn't yet been found in the new value.
The value associated with the pendingRemove pointer.
The parts of the first JSON value not found in the second JSON value.
The split structures that are part of the old JSON value not found in the new JSON value
Add an JSON pointer that is pending an insert. Any previous pending insert is confirmed and placed into the inserted map.
the next pointer pending an insert
the data associated with the insert
Add an JSON pointer that is pending a remove. Any previous pending remove is confirmed and placed into the removed map.
the next pointer pending a remove
the data associated with the remove
Check if an invalid state has occurred
Called by the BinaryDelta when the end of the buffer has been found.
Handle deleted binary data
the starting position of the deleted data in the old buffer
the number of deleted bytes
Handle a match of binary data
the starting position in the old buffer
the number of matching bytes
Called by the BinaryDelta when a inserted binary data has been found.
the inserted data
true
Called by the BinaryDelta when a match of binary data has been found.
the starting position in the old buffer
the number of matching bytes
true
No change
Heuristic post-processing: replace split structures for which inserted includes every entry with a single pointer.
Heuristic post-processing: replace split structures for which removed includes every entry with a single pointer.
A consumer for the span parser that inserts values
Accept a JSON pointer found in the new value
the JSON pointer of the inserted value
the starting position in the newValue buffer
the number of bytes in the newValue buffer
Signal the end of a split structure
the JSON pointer of the inserted value
the number of tokens in the split structure
the starting position in the newValue buffer
the number of bytes in the newValue buffer
A consumer for the span parser that removes values
Accept a JSON pointer found in the old value
the JSON pointer of the removed value
the starting position in the oldValue buffer
the number of bytes in the oldValue buffer
Signal the end of a split structure
the JSON pointer of the inserted value
the number of tokens in the split structure
the starting position in the newValue buffer
the number of bytes in the newValue buffer
Copyright © 2025 DiffusionData Ltd. All Rights Reserved.
Incrementally parse the binary delta to calculate a structural delta.
The result reports all differences, but is not necessarily minimal. In particular there are occasional 'false positive' REMOVE/INSERT pairs.
The Algorithm
We re-constitute the match and insert visitor callbacks into a sequence of delete, insert, and match edits. The binary delta format guarantees there will not be consecutive callbacks of the same type, and that match callbacks are presented in order with no overlaps. Delete and match edits will be contiguous with no gap between the end of one edit and the start of the next.
The processing uses two SpanParsers, one for the old token stream and one for the new token streams. We move forward along each parser driven by the binary delta edits, skipping old values covered by a match, adding a REMOVE change for each old value covered by a delete, and adding an INSERT change for each new value covered by an insert.
The SpanParsers calculate the appropriate JSON pointers to return for a span. A span finishes at the end of the first token found after the target offset, with these exceptions:
If a split is found in a field name, the span continues until the
first pointer is found in the value, potentially consuming one or more structure start tokens. The reporting of a change that affects a field does not distinguish between the field name and its value, except if the value is a structure and the change only affects part of the structure.
End structure tokens are eagerly consumed. This has two benefits.
First, it collapses empty structures to the appropriate parent pointer. Second, the closing tokens of non-empty structures are associated with the last pointer, which simplifies boundary detection.
The implementation relies on the SpanParser not consuming start structure tokens eagerly at a split unless instructed to do so (see SpanParser.spanToNext). This provides a tighter alignment between the detected binary differences and the token parsers, allowing a simpler pairing of tokens between the two streams. I'm not certain there aren't additional edge cases introduced by the eager consumption of structure tokens for field names.
The two token streams are treated symmetrically. Each SpanParser is only moved in a forward direction; there is no backtracking. We maintain the current byte position in each binary value as DeltaVisitor.oldOffset and DeltaVisitor.newOffset. After each edit is processed,
oldParser.nextByte() >= oldOffsetandnewParser.nextByte() >= newOffset.The fun happens at the edges, specifically where a token covers one or more binary edits. We call such a misaligned edge a 'split'.
Splits
The appropriate processing of a split depends on the type of edit in which it is found. For all edit types, we detect and process trailing edge splits; i.e. splits detected at the end of a span because the end of the last parsed token is after the end edge. It turns out we must also consider leading edge splits for match edits.
insert edits
We insert everything in the span. If a trailing edge split is detected, we do one of two things.
oldOffset(i.e. is not at a split token), we consume and add a REMOVE change for the next pointer in the old stream. The old stream is otherwise unaffected by the insert edit, sooldOffsetis the start of the next delete or match edit. Consuming the token might move it further into other delete or match edits. In all cases, correct handling of the insert split requires adding a REMOVE change for the first pointer found.Further split detection is required to detect differences that only affect the CBOR structure. If the parser structure depth is different at the end of the span than it was at start, the last token is treated in the same manner as a trailing edge split. Otherwise comparing 'b' with
['a', 'b', 'c']incorrectly generatesINSERT /0,INSERT /2rather thanINSERT /0,REMOVE /1,INSERT /1,INSERT /2.delete edits
The processing of splits for delete edits is the inverse of that for insert edits. (Swap INSERT and REMOVE, old stream and new stream).
match edits
When processing match edits we move both parsers. Either or both can have a trailing edge split. If both are split, the corresponding pointers are used for the REMOVE/INSERT pair. If neither are split, or if only one is split, we do nothing. As far as I can tell, there can only be a single split if the match ends with a start structure token (
{or[), so differences in the structure content will be detected by later edits.I haven't fully convinced myself that it is correct to do nothing in all single split cases, but it appears to work. I've experimented with a variant where a single split matches the next pointer eagerly consumed from the opposite stream, similar to the approach taken for insert/delete. This produces less satisfactory matching between structures. E.g. for
[]with['a' 'b']it producesREMOVE /0,INSERT /0/0,INSERT/0/1where the current implementation producesINSERT/0/0,INSERT/0/1. It may be worth revisiting this in conjunction with the post-processing fix to collapse complete spans into the parent.Consideration of parser depth is unnecessary for match trailing edge splits. A difference in structure depth must correspond to a binary difference covering structure delimiters, which will be handled by insert or delete processing.
There is a further complication to deal with for matches. A token in one stream can match the end and start of two tokens in the other stream. We deal with that by checking for a leading edge split. If the new stream parser is at the expected offset, and the old stream parser isn't, we've found a token spanning across two matches in the old stream. The previous insert edit will have inserted the first pointer for the new stream. We add the next found in the new stream in the match span (if any). Similar processing is performed if the old stream parser is at the expected offset, but the new stream parser is not.
Heuristic clean up
The basic approach of using the binary delta to identify splits and matching the splits to the appropriate tokens produces a reasonable but imperfect result. Further processing is performed to improve the output.
False positives (redundant REMOVE/INSERT pairs) occur reasonably frequently in the raw results. These are identified and removed by comparing each potential REMOVE with the previous INSERT, and each potential INSERT with the previous REMOVE. If the associated values are equal, and the pointers are compatible, both changes are dropped.
The raw results might contain pointers to the entire contents of a structure. These are replaced with a single pointer to the structure.