Exemple #1
0
  void delete(Iterator<FieldSegment> iter) {
    FieldSegment field = iter.next();
    if (field == null) return;

    int index = field.getIndexSegment().getIndex();
    JsonValue kv = getJsonValueAt(index);

    // if value doesn't exist in array then return null
    if (kv == null) {
      return;
    }

    // if this is the last path then return the value at this key in map
    if (field.isLastPath()) {
      list.remove(kv.key);
    }

    if (field.isMap()) {
      if (kv.getType() != Type.MAP) {
        return;
      }
      ((JsonDocument) kv).delete(iter);
    }

    if (kv.getType() != Type.ARRAY) {
      return;
    }
    JsonList l = (JsonList) kv;
    l.delete(iter);
  }
Exemple #2
0
  JsonValue getJsonValueAt(Iterator<FieldSegment> iter) {
    FieldSegment field = iter.next();
    if (field == null) return null;

    int index = field.getIndexSegment().getIndex();

    JsonValue kv = getJsonValueAt(index);
    // if value doesn't exist in map then return null
    if (kv == null) {
      return null;
    }

    // if this is the last path then return the value at this index
    if (field.isLastPath()) {
      return kv;
    }

    if (field.isMap()) {
      if (kv.getType() != Type.MAP) {
        return null;
      }
      return ((JsonDocument) kv).getKeyValueAt(iter);
    }
    if (kv.getType() != Type.ARRAY) {
      return null;
    }
    return ((JsonList) kv).getJsonValueAt(iter);
  }
Exemple #3
0
  JsonValue createOrInsert(Iterator<FieldSegment> iter, JsonValue inJsonValue) {
    /* if the field is null then get the next field from the iterator */
    FieldSegment field;
    field = iter.next();

    int index = field.getIndexSegment().getIndex();
    JsonValue oldJsonValue = getJsonValueAt(index);
    /*
     * If this is the last element in the path then just
     * overwrite the previous value for the same key with
     * new value
     */
    if (field.isLastPath()) {
      if (index >= list.size()) {
        list.add(inJsonValue);
      } else {
        list.set(index, inJsonValue);
      }
      return this;
    }

    if (field.isMap()) {
      /*
       * if the new value for the same field is not
       * a map then delete the existing value and write new
       */
      JsonDocument newDocument;
      if ((oldJsonValue == null) || (oldJsonValue.getType() != Type.MAP)) {
        newDocument = new JsonDocument();
        newDocument.createOrInsert(iter, inJsonValue);
        if (index >= list.size()) {
          list.add(newDocument);
        } else {
          list.set(index, newDocument);
        }
        return this;
      }
      newDocument = (JsonDocument) oldJsonValue;
      return newDocument.createOrInsert(iter, inJsonValue);
    }

    /* next field is an array element - like a[3][5] */
    JsonList newList;
    if (oldJsonValue == null || ((oldJsonValue.getType() != Type.ARRAY))) {
      newList = new JsonList();
      newList.createOrInsert(iter, inJsonValue);
      if (index >= list.size()) {
        list.add(newList);
      } else {
        list.set(index, newList);
      }
      return this;
    }
    // Existing element is already an array so insert the element inside it
    newList = (JsonList) oldJsonValue;
    return newList.createOrInsert(iter, inJsonValue);
  }