下面列出了com.fasterxml.jackson.databind.node.ArrayNode#addNull ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
@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));
}
}
}
/** 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;
}
/**
* 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;
}
}