Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. /**
  2. * Iterates over json1 which is intended to be a full representation of a complete JSON
  3. * structure. It compares nodes on json1 against nodes on json2 which should contain
  4. * either the same identical structure of json1 or a subset of JSON structure contained
  5. * in json1.
  6. *
  7. * If identically named nodes on json1 and json2 vary in type (ObjectNode vs ArrayNode
  8. * for example) then an exception is thrown since json2 must not contain any additional
  9. * structure than what is found in json1.
  10. *
  11. * Explicit Null Node Handling Regardless of Node type:
  12. *
  13. * This pertains to the value of a node being explicity equal to null. See further below
  14. * for handling of non-existent nodes
  15. *
  16. * If a node is null on json1 and not null on json2 then the node on json1 is set to the
  17. * value of the node on json2.
  18. *
  19. * If a node is not null on json1 and is null on json2 then the node on json1 is made null.
  20. *
  21. * Non-existent Node Handling:
  22. *
  23. * Since json1 is intended to be a full representation of a complete JSON structure
  24. * nodes on json2 that don't exist on json1 are completely ignored. Only if the same
  25. * node exists on both json1 and json2 will the structures be merged.
  26. *
  27. * ArrayNode Handling
  28. *
  29. * If the node being compared is an ArrayNode then the elements on json2 are iterated
  30. * over. If the index on json1 exists on json1 then the two elements are merged. If the
  31. * index doesn't exist on json1 then the element is added to the ArrayNode on json1.
  32. * Note: The existence of the element on json1 is determined by index and when an
  33. * element is added to json1 it's index increases by one. That shouldn't be a problem
  34. * though as for there to ever be more elements in json2, the index pointer will always
  35. * be one larger than the max index of json1.
  36. *
  37. * ArrayNode Handling when json1 contains more elements than json2:
  38. *
  39. * If
  40. *
  41. * @param json1
  42. * @param json2
  43. * @return
  44. * @throws com.blackberry.bdp.common.exception.JsonMergeException
  45. */
  46. public static JsonNode merge(JsonNode json1, JsonNode json2) throws JsonMergeException {
  47. Iterator<String> json1FieldNames = json1.fieldNames();
  48. while (json1FieldNames.hasNext()) {
  49. String json1Field = json1FieldNames.next();
  50. JsonNode json1Node = json1.get(json1Field);
  51. if (json1.getNodeType().equals(json2.getNodeType()) == false) {
  52. throw new JsonMergeException(String.format("json1 (%s) cannot be merged with json2 (%s)",
  53. json1.getNodeType(), json2.getNodeType()));
  54. } else if (!json2.has(json1Field)) {
  55. LOG.info("Not comparing {} since it doesn't exist on json2", json1Field);
  56. } else if (json1Node.isNull() && json2.hasNonNull(json1Field)) {
  57. ((ObjectNode) json1).replace(json1Field, json2.get(json1Field));
  58. LOG.info("explicitly null node {} on json1 replaced with non-null node on json2", json1Field);
  59. } else if (json1.hasNonNull(json1Field) && json2.get(json1Field).isNull()) {
  60. ((ObjectNode) json1).replace(json1Field, json2.get(json1Field));
  61. LOG.info("non-null node {} on json1 replaced with explicitly null node on json2", json1Field);
  62. } else if (json1.isObject()) {
  63. LOG.info("Calling merge on Object {}", json1Field);
  64. merge(json1Node, json2.get(json1Field));
  65. } else if (json1 instanceof ObjectNode) {
  66. LOG.info("json1 is an instanceof ObjectNode"); ;
  67. if (json2.get(json1Field) != null) {
  68. ((ObjectNode) json1).replace(json1Field, json2.get(json1Field));
  69. LOG.info("json1 replaced {} with json2's field", json1Field);
  70. }
  71. } else if (json1.isArray()) {
  72. ArrayNode json1ArrayNode = (ArrayNode) json1.get(json1Field);
  73. ArrayNode json2ArrayNode = (ArrayNode) json2.get(json1Field);
  74. LOG.info("ArrayNode {} json1 has {} elements and json2 has {} elements",
  75. json1Field, json1ArrayNode.size(), json2ArrayNode.size());
  76. int indexNo = 0;
  77. Iterator<JsonNode> json2Iter = json2ArrayNode.iterator();
  78. while (json2Iter.hasNext()) {
  79. JsonNode json2Element = json2Iter.next();
  80. if (!json1Node.has(indexNo)) {
  81. LOG.info("Need to merge ArrayNode {} element {}", json1Field, indexNo);
  82. merge(json1Node.get(indexNo), json2Element);
  83. } else {
  84. LOG.info("ArrayNode {} element {} not found on json1, adding", json1Field, indexNo);
  85. json1ArrayNode.add(json2Element);
  86. }
  87. indexNo++;
  88. }
  89. while (json1ArrayNode.size() > json2ArrayNode.size()) {
  90. int indexToRemove = json1ArrayNode.size() - 1;
  91. json1ArrayNode.remove(indexToRemove);
  92. LOG.info("ArrayNode {} index {} on json1 removed since greater than size of json2 ({})",
  93. json1Field, indexToRemove, json2ArrayNode.size());
  94. }
  95. } else {
  96. throw new JsonMergeException(String.format("Cannot merge %s unknown type of %s",
  97. json1Field, json1Node.getNodeType()));
  98. }
  99. }
  100. return json1;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement