use of com.fasterxml.jackson.core.TreeNode in project bson4jackson by michel-kraemer.
the class BsonObjectIdDeserializer method deserialize.
@Override
@SuppressWarnings("deprecation")
public ObjectId deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
if (jp instanceof BsonParser) {
BsonParser bsonParser = (BsonParser) jp;
if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_OBJECTID) {
throw ctxt.mappingException(ObjectId.class);
}
return (ObjectId) bsonParser.getEmbeddedObject();
} else {
TreeNode tree = jp.getCodec().readTree(jp);
int time = ((ValueNode) tree.get("$time")).asInt();
int machine = ((ValueNode) tree.get("$machine")).asInt();
int inc = ((ValueNode) tree.get("$inc")).asInt();
return new ObjectId(time, machine, inc);
}
}
use of com.fasterxml.jackson.core.TreeNode in project bson4jackson by michel-kraemer.
the class BsonTimestampDeserializer method deserialize.
@Override
@SuppressWarnings("deprecation")
public Timestamp deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
if (jp instanceof BsonParser) {
BsonParser bsonParser = (BsonParser) jp;
if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_TIMESTAMP) {
throw ctxt.mappingException(Timestamp.class);
}
return (Timestamp) bsonParser.getEmbeddedObject();
} else {
TreeNode tree = jp.getCodec().readTree(jp);
int time = ((ValueNode) tree.get("$time")).asInt();
int inc = ((ValueNode) tree.get("$inc")).asInt();
return new Timestamp(time, inc);
}
}
use of com.fasterxml.jackson.core.TreeNode in project beam by apache.
the class PipelineOptionsTranslation method toProto.
/**
* Converts the provided {@link PipelineOptions} to a {@link Struct}.
*/
public static Struct toProto(PipelineOptions options) {
Struct.Builder builder = Struct.newBuilder();
try {
// TODO: Officially define URNs for options and their scheme.
JsonNode treeNode = MAPPER.valueToTree(options);
JsonNode rootOptions = treeNode.get("options");
Iterator<Map.Entry<String, JsonNode>> optionsEntries = rootOptions.fields();
if (!optionsEntries.hasNext()) {
// (observed with version 2.2.3)
throw new RuntimeException("Unable to convert pipeline options, please check for outdated jackson-core version in the classpath.");
}
Map<String, TreeNode> optionsUsingUrns = new HashMap<>();
while (optionsEntries.hasNext()) {
Map.Entry<String, JsonNode> entry = optionsEntries.next();
optionsUsingUrns.put("beam:option:" + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entry.getKey()) + ":v1", entry.getValue());
}
// The JSON format of a Protobuf Struct is the JSON object that is equivalent to that struct
// (with values encoded in a standard json-codeable manner). See Beam PR 3719 for more.
JsonFormat.parser().merge(MAPPER.writeValueAsString(optionsUsingUrns), builder);
return builder.build();
} catch (IOException e) {
throw new RuntimeException("Failed to convert PipelineOptions to Protocol", e);
}
}
use of com.fasterxml.jackson.core.TreeNode in project beam by apache.
the class PipelineOptionsTranslation method fromProto.
/**
* Converts the provided {@link Struct} into {@link PipelineOptions}.
*/
public static PipelineOptions fromProto(Struct protoOptions) {
try {
Map<String, TreeNode> mapWithoutUrns = new HashMap<>();
TreeNode rootOptions = MAPPER.readTree(JsonFormat.printer().print(protoOptions));
Iterator<String> optionsKeys = rootOptions.fieldNames();
while (optionsKeys.hasNext()) {
String optionKey = optionsKeys.next();
TreeNode optionValue = rootOptions.get(optionKey);
mapWithoutUrns.put(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, optionKey.substring("beam:option:".length(), optionKey.length() - ":v1".length())), optionValue);
}
return MAPPER.readValue(MAPPER.writeValueAsString(ImmutableMap.of("options", mapWithoutUrns)), PipelineOptions.class);
} catch (IOException e) {
throw new RuntimeException("Failed to read PipelineOptions from Protocol", e);
}
}
use of com.fasterxml.jackson.core.TreeNode in project graylog2-server by Graylog2.
the class AsValueReferenceTypeDeserializer method _deserialize.
protected Object _deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
final TreeNode treeNode = p.readValueAsTree();
final TreeNode typeNode = treeNode.path("type");
if (!typeNode.isObject()) {
ctxt.reportWrongTokenException(typeNode.traverse(), JsonToken.START_OBJECT, "expected START_OBJECT before the type information and deserialized value");
}
final TreeNode valueNode = typeNode.path("@value");
if (!valueNode.isValueNode()) {
ctxt.reportWrongTokenException(typeNode.traverse(), JsonToken.VALUE_STRING, "expected VALUE_STRING as type information and deserialized value");
}
final JsonParser jsonParser = valueNode.traverse();
final String typeId = jsonParser.nextTextValue();
final JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
final JsonParser newParser = treeNode.traverse();
if (newParser.nextToken() != JsonToken.START_OBJECT) {
ctxt.reportWrongTokenException(newParser, JsonToken.START_OBJECT, "expected START_OBJECT");
}
return deser.deserialize(newParser, ctxt);
}
Aggregations