Package foodev.jsondiff.jsonwrap

Examples of foodev.jsondiff.jsonwrap.JzonObject


    this.factory = factory;
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  boolean accept(Leaf leaf, JzonArray instructions, JzonObject childPatch) {
    JzonObject object = (JzonObject) factory.parse(leaf.val.toString());
    JzonObject patch = factory.createJsonObject();
    patch.add(MOD, instructions);
    if (!childPatch.entrySet().isEmpty()) {
      patch.entrySet().addAll((Collection) childPatch.entrySet());
    }
    apply(object, patch);
    return visitor.shouldCreatePatch(leaf.val.unwrap(), object.unwrap());
  }
View Full Code Here


    return visitor.shouldCreatePatch(leaf.val.unwrap(), object.unwrap());
  }

  void apply(JzonElement origEl, JzonElement patchEl) throws IllegalArgumentException {

    JzonObject patch = (JzonObject) patchEl;
    Set<Entry<String, JzonElement>> memb = new TreeSet<Entry<String, JzonElement>>(INSTRUCTIONS_COMPARATOR);
    memb.addAll(patch.entrySet());
    for (Entry<String, JzonElement> entry : memb) {
      String key = entry.getKey();
      JzonElement value = entry.getValue();
      if (key.startsWith(MOD)) {
        JzonElement partialInstructions = entry.getValue();
View Full Code Here

    }
    if (!toEl.isJsonObject()) {
      throw new IllegalArgumentException("To is not a json object");
    }

    JzonObject from = (JzonObject) fromEl;
    JzonObject to = (JzonObject) toEl;

    Root fromRoot = new Root();
    Root toRoot = new Root();

    ArrayList<Leaf> fromLeaves = new ArrayList<Leaf>();
    ArrayList<Leaf> toLeaves = new ArrayList<Leaf>();

    HashMap<Integer, ArrNode> fromArrs = new HashMap<Integer, ArrNode>();
    HashMap<Integer, ArrNode> toArrs = new HashMap<Integer, ArrNode>();

    findLeaves(fromRoot, from, fromLeaves, fromArrs);
    findLeaves(toRoot, to, toLeaves, toArrs);

    IncavaDiff<Leaf> idiff = new IncavaDiff<Leaf>(fromLeaves, toLeaves);

    List<IncavaEntry> diff = idiff.diff();
    int delta = 0;
    // be careful with direct use of indexOf: need instance equality, not equals!
    for (IncavaEntry incavaEntry : diff) {
      int deletes = Math.max(0, incavaEntry.getDeletedEnd() - incavaEntry.getDeletedStart() + 1);
      int insertionIndex = (incavaEntry.getDeletedStart() > 0) ? incavaEntry.getDeletedStart() + delta - 1 : 0;
      Leaf fromLeaf = (fromLeaves.size() > insertionIndex) ? fromLeaves.get(insertionIndex) : fromLeaves.get(fromLeaves.size() - 1);
      for (int i = incavaEntry.getDeletedStart(); i < incavaEntry.getDeletedEnd() + 1; i++) {
        // ensure not orphan
        fromLeaf.recover(fromLeaves);
        // proceed to delete
        Leaf toLeaf = fromLeaves.get(i + delta);
        fromLeaf.delete(toLeaf, null);
        fromLeaf = toLeaf;
      }
      if (incavaEntry.getAddedEnd() < 0) {
        continue;
      }
      fromLeaf = (fromLeaves.size() > insertionIndex) ? fromLeaves.get(insertionIndex) : fromLeaves.get(fromLeaves.size() - 1);
      while (fromLeaf.oper == Oper.DELETE && insertionIndex > 0) {
        // find a NOT deleted node for set / insertion - parent traversal will be done later
        insertionIndex--;
        fromLeaf = fromLeaves.get(insertionIndex);
      }
      for (int i = incavaEntry.getAddedStart(); i < incavaEntry.getAddedEnd() + 1; i++) {
        // ensure not orphan
        fromLeaf.recover(fromLeaves);

        Leaf toLeaf = toLeaves.get(i);
        if (deletes > 0) {
          deletes--;
          Leaf deleted = fromLeaves.get(incavaEntry.getDeletedStart() + delta + (i - incavaEntry.getAddedStart()));
          deleted.recover(fromLeaves);
          if (!fromLeaf.cancelDelete(deleted, toLeaf)) {
            // couldn't cancel delete (different obj key): INSERT
            fromLeaf.insert(toLeaf, null);
            fromLeaves.add(insertionIndex + 1, toLeaf);
            fromLeaf = toLeaf;
            delta++;
          } else {
            // cancel delete: pure SET
            fromLeaf = deleted;
          }
        } else {
          // regular INSERT
          fromLeaf.insert(toLeaf, null);
          fromLeaves.add(insertionIndex + 1, toLeaf);
          fromLeaf = toLeaf;
          delta++;
        }
        insertionIndex++;
      }
    }
    // recover all pending orphans: this could be easily optimized
    int i = 0;
    for (Leaf fromLeaf : fromLeaves) {
      if (fromLeaf.isOrphan()) {
        fromLeaf.recover(i, fromLeaves);
      }
      i++;
    }
    JzonObject patch = fromLeaves.iterator().next().patch();
    // prints the new structure
    // fromLeaves.iterator().next().print();
    return patch;

  }
View Full Code Here

  public Object diff(Object from, Object to) throws IllegalArgumentException {

    JzonElement fromEl = factory.wrap(from);
    JzonElement toEl = factory.wrap(to);

    JzonObject diff = diff(fromEl, toEl);

    return diff.unwrap();
  }
View Full Code Here

        String reIndexedKey = key;
        if (child.parent instanceof ArrNode) {
          ((ArrNode) child.parent).index = i - deletes;
          reIndexedKey = child.parent.toString();
        }
        JzonObject insert = factory.createJsonObject();
        boolean deeper = true;
        if (child.oper == Oper.INSERT) {
          insert.add("+" + reIndexedKey, child.val);
          instructions.insert(instructions.size(), insert);
          deeper = false;
        } else if (child.oper == Oper.SET) {
          insert.add(reIndexedKey, child.val);
          instructions.insert(instructions.size(), insert);
          deeper = false;
        } else if (child.oper == Oper.DELETE) {
          insert.addProperty("-" + reIndexedKey, 0);
          instructions.insert(instructions.size(), insert);
          deeper = false;
        }
        if (deeper) {
          JzonObject childPatch = factory.createJsonObject();
          JzonArray childInstructions = child.createPatch(childPatch);
          if (childInstructions.size() > 0) {
            if (visitor != null && !child.val.isJsonPrimitive() && !visitor.accept(child, childInstructions, childPatch)) {
              continue;
            }
            patch.add("~" + key, childInstructions);
          }
          if (!childPatch.entrySet().isEmpty()) {
            patch.add(key, childPatch);
          }
        }
        if (child.oper == Oper.DELETE) {
          deletes++;
View Full Code Here

    }
  }

  JzonObject patch() {
    checkCancellations();
    JzonObject patch = factory.createJsonObject();
    if (oper == Oper.INSERT) {
      patch.add("+" + parent.toString(), val);
    } else if (oper == Oper.SET) {
      patch.add(parent.toString(), val);
    } else if (oper == Oper.DELETE) {
      patch.add("-" + parent.toString(), val);
    } else {
      JzonArray childInstructions = createPatch(patch);
      if (childInstructions.size() > 0) {
        patch.add("~", childInstructions);
      }
    }
    return patch;
  }
View Full Code Here

TOP

Related Classes of foodev.jsondiff.jsonwrap.JzonObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.