Showing linked changes to JSON Structure in diff3x

For JSON, the issue of handling curly braces (for objects) and square brackets (for arrays) is similar to XML start and end tags. Again, some representation of connected change is needed to maintain syntactic correctness.

Object members and array members are comma separated and this is tricky to get right in some situations. The syntax is shown below.

object = begin-object [ member *( value-separator member ) ]
               end-object
array = begin-array [ value *( value-separator value ) ] end-array

These are the six structural characters:
  begin-array = ws %x5B ws ; [ left square bracket
  begin-object = ws %x7B ws ; { left curly bracket
  end-array = ws %x5D ws ; ] right square bracket
  end-object = ws %x7D ws ; } right curly bracket
  name-separator = ws %x3A ws ; : colon
  value-separator = ws %x2C ws ; , comma

Insignificant whitespace is allowed before or after any of the six
structural characters.
ws = *(
       %x20 / ; Space
       %x09 / ; Horizontal tab
       %x0A / ; Line feed or New line
       %x0D ) ; Carriage return

Here is an example of a change to an array of strings.

Table 3. JSON structural change

A.txtO.txtB.txt
[[12,13,14],20,21,22]
[12,13,14,20,21,22]
[[12,13,14,20,21,22]]


This could be represented in the diff3 format as shown below, but note that this is not how diff3 would process the above files which each have a single line. However the result below could be generated by careful use of line breaks in the input files.

[
<<<<<<<< A.txt
[
||||||| O.txt
 
=======

>>>>>>> B.txt
 
<<<<<<<< A.txt

||||||| O.txt
 
=======
[
>>>>>>> B.txt
12,13,14
<<<<<<<< A.txt
]
||||||| O.txt
 
=======

>>>>>>> B.txt
,20,21,22
<<<<<<<< A.txt

||||||| O.txt
 
=======
]
>>>>>>> B.txt
]

The above will only produce syntactically correct results if the correct choices are made, which is not easy. It is also not easy to see what is going on in the diff3 file because of the extra whitespace lines that have to be inserted to make this work.

We introduce here a way to connect the relevant consistent choices so that if the '[' is selected then the appropriate choice of the end ']' is also made automatically.

A choice consists of one or more options. An option may have an id. An option may also have a select attribute which provides a boolean value made up of one or a combination of other ids and if these are 'true', i.e. have been chosen to be included, then this option is selected automatically.

This could be represented in diff3x as shown below. We show here the id attribute on an option to identify it uniquely within the file. We then use that id to reference that option where another option needs to be linked to it. In this case, the '[' in the A.txt has id="a42" and this is then referenced in the select attribute of the corresponding ']' option later in the file. Similarly the pair of square brackets in B.txt are linked with the id "b44".

<diff3x a="A.txt" b="B.txt" o="O.txt">
[
<choice2>
<a id="a42">[</a>
<ob/></choice2>
<choice2>
<b id="b44">[</b>
<ao/></choice2>
12,13,14
<choice2>
<a select="a42">]</a>
<ob/></choice2>
,20,21,22
<choice2>
<b select="b44">]</b>
<ao/></choice2>
]</diff3x>

Note that the '[' in A could be selected either instead of or as well as the '[' in B even though they are at the same position in the array. Whichever choice is made, the other choices with a select attribute identifying the same id are chosen and the result is syntactically correct. This is a powerful way to represent connected choices.