Search in sources :

Example 11 with TreeNode

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);
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) ObjectId(de.undercouch.bson4jackson.types.ObjectId) TreeNode(com.fasterxml.jackson.core.TreeNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode)

Example 12 with TreeNode

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);
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) TreeNode(com.fasterxml.jackson.core.TreeNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) Timestamp(de.undercouch.bson4jackson.types.Timestamp)

Example 13 with TreeNode

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);
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Struct(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.Struct) TreeNode(com.fasterxml.jackson.core.TreeNode) HashMap(java.util.HashMap) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 14 with TreeNode

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);
    }
}
Also used : HashMap(java.util.HashMap) TreeNode(com.fasterxml.jackson.core.TreeNode) IOException(java.io.IOException)

Example 15 with TreeNode

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);
}
Also used : TreeNode(com.fasterxml.jackson.core.TreeNode) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

TreeNode (com.fasterxml.jackson.core.TreeNode)34 TextNode (com.fasterxml.jackson.databind.node.TextNode)11 JsonParser (com.fasterxml.jackson.core.JsonParser)10 IOException (java.io.IOException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 HashMap (java.util.HashMap)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)4 BsonParser (de.undercouch.bson4jackson.BsonParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)3 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 Sets (com.google.common.collect.Sets)3 Iterator (java.util.Iterator)3 Test (org.junit.Test)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2