28
28
import com .github .fge .jackson .NodeType ;
29
29
import com .github .fge .jackson .jsonpointer .JsonPointer ;
30
30
import com .github .fge .jsonpatch .JsonPatch ;
31
+ import com .github .fge .jsonpatch .JsonPatchMessages ;
32
+ import com .github .fge .msgsimple .bundle .MessageBundle ;
33
+ import com .github .fge .msgsimple .load .MessageBundles ;
31
34
import com .google .common .annotations .VisibleForTesting ;
32
35
import com .google .common .base .Equivalence ;
33
36
import com .google .common .collect .Maps ;
50
53
* patch for any other source/target combination than the one used to generate
51
54
* the patch.</p>
52
55
*
56
+ * <p>This class always performs operations in the following order: removals,
57
+ * additions and replacements. It then factors removal/addition pairs into
58
+ * move operations, or copy operations if a common element exists, at the same
59
+ * {@link JsonPointer pointer}, in both the source and destination.</p>
60
+ *
61
+ * <p>You can obtain a diff either as a {@link JsonPatch} directly or, for
62
+ * backwards compatibility, as a {@link JsonNode}.</p>
63
+ *
53
64
* @since 1.2
54
65
*/
55
66
@ ParametersAreNonnullByDefault
56
67
public final class JsonDiff
57
68
{
69
+ private static final MessageBundle BUNDLE
70
+ = MessageBundles .getBundle (JsonPatchMessages .class );
58
71
private static final ObjectMapper MAPPER = JacksonUtils .newMapper ();
59
72
60
73
private static final Equivalence <JsonNode > EQUIVALENCE
@@ -77,6 +90,8 @@ private JsonDiff()
77
90
public static JsonPatch asJsonPatch (final JsonNode source ,
78
91
final JsonNode target )
79
92
{
93
+ BUNDLE .checkNotNull (source , "common.nullArgument" );
94
+ BUNDLE .checkNotNull (target , "common.nullArgument" );
80
95
final Map <JsonPointer , JsonNode > unchanged
81
96
= getUnchangedValues (source , target );
82
97
final DiffProcessor processor = new DiffProcessor (unchanged );
@@ -172,17 +187,17 @@ private static void generateArrayDiffs(final DiffProcessor processor,
172
187
final int secondSize = target .size ();
173
188
final int size = Math .min (firstSize , secondSize );
174
189
175
- for (int index = 0 ; index < size ; index ++)
176
- generateDiffs (processor , pointer .append (index ), source .get (index ),
177
- target .get (index ));
178
-
179
190
/*
180
191
* Source array is larger; in this case, elements are removed from the
181
192
* target; the index of removal is always the original arrays's length.
182
193
*/
183
194
for (int index = size ; index < firstSize ; index ++)
184
195
processor .valueRemoved (pointer .append (size ), source .get (index ));
185
196
197
+ for (int index = 0 ; index < size ; index ++)
198
+ generateDiffs (processor , pointer .append (index ), source .get (index ),
199
+ target .get (index ));
200
+
186
201
// Deal with the destination array being larger...
187
202
for (int index = size ; index < secondSize ; index ++)
188
203
processor .valueAdded (pointer .append ("-" ), target .get (index ));
0 commit comments