Java Code Examples for com.fasterxml.jackson.databind.node.ArrayNode#addNull()

The following examples show how to use com.fasterxml.jackson.databind.node.ArrayNode#addNull() . 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: PubsubMessageToObjectNode.java    From gcp-ingestion with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * A shim into transformForBqSchema for translating tuples into objects and to
 * reconstruct additional properties.
 * @param jsonFieldName The name of the original JSON field.
 * @param fields A list of types for each element of the tuple.
 * @param tuple A list of objects that are mapped into a record.
 * @param additionalProperties A mutable map of elements that aren't captured in the schema.
 * @return A record object with anonymous struct naming.
 */
private ObjectNode processTupleField(String jsonFieldName, List<Field> fields, ArrayNode tuple,
    ObjectNode additionalProperties) {
  final ObjectNode m = Json.createObjectNode();
  for (int i = 0; i < tuple.size(); i++) {
    m.set(String.format("f%d_", i), tuple.get(i));
  }
  final ObjectNode props = additionalProperties == null ? null : Json.createObjectNode();
  transformForBqSchema(m, fields, props);
  if (!Json.isNullOrEmpty(props)) {
    final ArrayNode tupleAdditionalProperties = Json.createArrayNode();
    for (int i = 0; i < tuple.size(); i++) {
      tupleAdditionalProperties.addNull();
    }
    props.fields().forEachRemaining(e -> {
      int index = Integer.parseInt(e.getKey().substring(1, e.getKey().length() - 1));
      tupleAdditionalProperties.set(index, e.getValue());
    });
    additionalProperties.set(jsonFieldName, tupleAdditionalProperties);
  }
  return m;
}
 
Example 2
Source File: JsonSerializable.java    From azure-cosmosdb-java with MIT License 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private <T> void internalSetCollection(String propertyName, Collection<T> collection, ArrayNode targetArray) {
    for (T childValue : collection) {
        if (childValue == null) {
            // Sets null.
            targetArray.addNull();
        } else if (childValue instanceof Collection) {
            // When T is also a Collection, use recursion.
            ArrayNode childArray = targetArray.addArray();
            this.internalSetCollection(propertyName, (Collection) childValue, childArray);
        } else if (childValue instanceof JsonNode) {
            targetArray.add((JsonNode) childValue);
        } else if (childValue instanceof JsonSerializable) {
            // JsonSerializable
            JsonSerializable castedValue = (JsonSerializable) childValue;
            castedValue.populatePropertyBag();
            targetArray.add(castedValue.propertyBag != null ? castedValue.propertyBag : this.getMapper().createObjectNode());
        } else {
            // POJO, JSONObject, Number (includes Int, Float, Double etc),
            // Boolean, and String.
            targetArray.add(this.getMapper().valueToTree(childValue));
        }
    }
}
 
Example 3
Source File: ConversionUtil.java    From native-navigation with MIT License 5 votes vote down vote up
/** Converts a {@link ReadableArray} into an Json {@link ArrayNode} */
static ArrayNode toJsonArray(ReadableArray readableArray) {
  JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
  ArrayNode result = nodeFactory.arrayNode();
  for (int i = 0; i < readableArray.size(); i++) {
    ReadableType indexType = readableArray.getType(i);
    switch (indexType) {
      case Null:
        result.addNull();
        break;
      case Boolean:
        result.add(readableArray.getBoolean(i));
        break;
      case Number:
        result.add(readableArray.getDouble(i));
        break;
      case String:
        result.add(readableArray.getString(i));
        break;
      case Map:
        result.add(toJsonObject(readableArray.getMap(i)));
        break;
      case Array:
        result.add(toJsonArray(readableArray.getArray(i)));
        break;
      default:
        Log.e(TAG, "Could not convert object at index " + i + ".");
    }
  }
  return result;
}
 
Example 4
Source File: RestUtilities.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Add a value to a column.
 *
 * @param row The JSON array representing the column.
 * @param type The type of the value.
 * @param value The (possibly null) value.
 */
public static void addData(final ArrayNode row, final String type, final String value) {
    switch (type) {
        case BooleanAttributeDescription.ATTRIBUTE_NAME:
        case BooleanObjectAttributeDescription.ATTRIBUTE_NAME:
            // A DataFrame will parse [True, False, None] to [1.0, 0.0, Nan],
            // so implicitly convert null to False so the result is all booleans.
            row.add(Boolean.parseBoolean(value));
            break;
        case ColorAttributeDescription.ATTRIBUTE_NAME:
            if (value == null) {
                row.addNull();
            } else {
                final ArrayNode rgb = row.addArray();
                final ConstellationColor cv = ConstellationColor.getColorValue(value);
                rgb.add(cv.getRed());
                rgb.add(cv.getGreen());
                rgb.add(cv.getBlue());
                rgb.add(cv.getAlpha());
            }
            break;
        case ZonedDateTimeAttributeDescription.ATTRIBUTE_NAME:
            // A DataFrame will parse null as NaT.
            if (value == null) {
                row.addNull();
            } else {
                // Remove the trailing tz name if present.
                final int ix = value.lastIndexOf(" [");
                row.add(ix == -1 ? value : value.substring(0, ix));
            }
            break;
        case FloatAttributeDescription.ATTRIBUTE_NAME:
        case FloatObjectAttributeDescription.ATTRIBUTE_NAME:
            // A DataFrame will parse null as NaN.
            if (value == null) {
                row.addNull();
            } else {
                row.add(Float.parseFloat(value));
            }
            break;
        case IntegerAttributeDescription.ATTRIBUTE_NAME:
        case IntegerObjectAttributeDescription.ATTRIBUTE_NAME:
            // A DataFrame will parse null as NaN, but the column will be
            // converted to a float column.
            if (value == null) {
                row.addNull();
            } else {
                row.add(Integer.parseInt(value));
            }
            break;
        default:
            // Everything else we leave as a string; nulls are fine.
            row.add(value);
            break;
    }
}