Java Code Examples for org.ojai.Document#setArray()

The following examples show how to use org.ojai.Document#setArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetBooleanArray() {
  Document document = Json.newDocument();
  document.set(FIELD_MAP_INT, 111);
  boolean[] boolArray = new boolean[3];
  boolArray[0] = true;
  boolArray[1] = false;
  boolArray[2] = true;
  document.setArray("map.boolarray", boolArray);

  assertEquals(false, document.getBoolean("map.boolarray[1]"));
  assertEquals(true, document.getBoolean("map.boolarray[2]"));
}
 
Example 2
Source File: MapRJsonDocumentLoader.java    From datacollector with Apache License 2.0 4 votes vote down vote up
/**
 * Map mode
 */
private void writeFieldToDocumentMap(Document doc, Field field, String name) throws IOException {
  if (field.getValue() == null) {
    // On the Map mode just set the null on the document using the name.
    doc.setNull(name);
  } else {
    switch (field.getType()) {
      case FILE_REF:
        throw new IOException("Cannot serialize FileRef fields.");
      case MAP:
      case LIST_MAP:
        Document newDoc = loader.createNewEmptyDocument();
        Map<String, Field> map = field.getValueAsMap();
        for (Map.Entry<String, Field> fieldEntry : map.entrySet()) {
          String fieldName = fieldEntry.getKey();
          Field newField = fieldEntry.getValue();
          // recursive call in map mode.
          writeFieldToDocumentMap(newDoc, newField, fieldName);
        }
        // Map the new doc
        doc.set(name, newDoc);
        break;
      case LIST:
        List<Field> listOfFields = field.getValueAsList();
        List<Object> objsList = new ArrayList<>();
        for (Field f : listOfFields) {
          // recursive call in a list mode.
          writeFieldToDocumentList(f, objsList);
        }
        doc.setArray(name, objsList.toArray());
        break;
      case BOOLEAN:
        doc.set(name, field.getValueAsBoolean());
        break;
      case CHAR:
        doc.set(name, String.valueOf(field.getValueAsChar()));
        break;
      case BYTE:
        doc.set(name, new byte[]{field.getValueAsByte()});
        break;
      case SHORT:
        doc.set(name, field.getValueAsShort());
        break;
      case INTEGER:
        doc.set(name, field.getValueAsInteger());
        break;
      case LONG:
        doc.set(name, field.getValueAsLong());
        break;
      case FLOAT:
        doc.set(name, field.getValueAsFloat());
        break;
      case DOUBLE:
        doc.set(name, field.getValueAsDouble());
        break;
      case DATE:
        doc.set(name, field.getValueAsDate().getTime());
        break;
      case DATETIME:
        doc.set(name, field.getValueAsDatetime().getTime());
        break;
      case TIME:
        doc.set(name, field.getValueAsTime().getTime());
        break;
      case DECIMAL:
        doc.set(name, field.getValueAsDecimal());
        break;
      case STRING:
      case ZONED_DATETIME:
        doc.set(name, field.getValueAsString());
        break;
      case BYTE_ARRAY:
        doc.set(name, field.getValueAsByteArray());
        break;
      default:
        throw new IllegalStateException(String.format("Unrecognized field type (%s) in field: %s",
            field.getType().name(),
            field.toString()
        ));
    }
  }
}