Example usage for com.fasterxml.jackson.core JsonParser getCodec

List of usage examples for com.fasterxml.jackson.core JsonParser getCodec

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getCodec.

Prototype

public abstract ObjectCodec getCodec();

Source Link

Document

Accessor for ObjectCodec associated with this parser, if any.

Usage

From source file:com.optimizely.ab.config.parser.AudienceJacksonDeserializer.java

@Override
public Audience deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = parser.getCodec().readTree(parser);

    String id = node.get("id").textValue();
    String name = node.get("name").textValue();
    List<Object> rawObjectList = (List<Object>) mapper.readValue(node.get("conditions").textValue(),
            List.class);
    Condition conditions = parseConditions(rawObjectList);

    return new Audience(id, name, conditions);
}

From source file:de.upb.wdqa.wdvd.datamodel.oldjson.jackson.OldAliasListDeserializer.java

@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    List<String> result;

    ObjectCodec codec = p.getCodec();

    if (p.getCurrentToken().equals(JsonToken.START_ARRAY)) {
        result = codec.readValue(p, new TypeReference<List<String>>() {
        });//from  w  w  w  .  j  a v  a  2s.c  o m
    } else {
        LinkedHashMap<Integer, String> map = codec.readValue(p,
                new TypeReference<LinkedHashMap<Integer, String>>() {
                });

        result = new ArrayList<String>(map.values());
    }
    return result;
}

From source file:gaffer.serialisation.simple.json.hyperloglogplus.HyperLogLogPlusJsonDeserialiser.java

@Override
public HyperLogLogPlus deserialize(final JsonParser jsonParser,
        final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

    final TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);

    final TreeNode coreHyperLogLogPlusObject = treeNode.get("hyperLogLogPlus");
    if (coreHyperLogLogPlusObject != null) {
        final TextNode jsonNodes = (TextNode) coreHyperLogLogPlusObject
                .get(HyperLogLogPlusJsonConstants.HYPER_LOG_LOG_PLUS_SKETCH_BYTES_FIELD);

        final byte[] nodeAsString = jsonNodes.binaryValue();
        final HyperLogLogPlus hyperLogLogPlus = HyperLogLogPlus.Builder.build(nodeAsString);

        return hyperLogLogPlus;
    } else {/*from   ww  w. j  a va2  s.c o  m*/
        throw new IllegalArgumentException("Recieved null or empty HyperLogLogPlus sketch");
    }
}

From source file:com.pkrete.locationservice.admin.deserializers.CollectionJSONDeserializer.java

@Override
public LibraryCollection deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {

    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);
    // Create new LibraryCollection object
    LibraryCollection collection = new LibraryCollection();
    // Deserialize name, locationCode, floor, staffNote1, staffNote2, 
    // map and image variables
    LocationJSONDeserializerHelper.deserializeBasicGroup1(collection, node);
    // Deserialize descriptions and notes variables
    LocationJSONDeserializerHelper.deserializeDescriptionsAndNotes(collection, node);
    // Deserialize areas variable
    LocationJSONDeserializerHelper.deserializeAreas(collection, node);
    // Deserialize subject matters variable
    LocationJSONDeserializerHelper.deserializeSubjectMatters(collection, node);

    // Deserialize collectionCode
    String collectionCode = node.get("collection_code") == null ? "" : node.get("collection_code").textValue();
    // Deserialize shelfNumber
    String shelfNumber = node.get("shelf_number") == null ? "" : node.get("shelf_number").textValue();
    // Set values
    collection.setCollectionCode(collectionCode);
    collection.setShelfNumber(shelfNumber);

    // Deserialize isSubstring (= match_beginning)
    if (node.get("match_beginning") != null) {
        boolean isSubstring = node.get("match_beginning").asBoolean();
        collection.setIsSubstring(isSubstring);
    }// w w  w  .  j  ava 2s  .c o m

    // Deserialize library id
    int libraryId = node.get("library_id") == null ? 0 : node.get("library_id").intValue();
    if (libraryId != 0) {
        collection.setLibrary(new Library(libraryId));
    }
    // Return the collection
    return collection;
}

From source file:io.gravitee.definition.jackson.datatype.api.deser.ProxyDeserializer.java

@Override
public Proxy deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);

    Proxy proxy = new Proxy();
    final JsonNode contextPath = node.get("context_path");
    if (contextPath != null) {
        String sContextPath = formatContextPath(contextPath.asText());
        proxy.setContextPath(sContextPath);
    } else {/*  w  w  w. j  a v  a  2s. com*/
        throw ctxt.mappingException("[api] API must have a valid context path");
    }

    final JsonNode nodeEndpoints = node.get("endpoints");

    if (nodeEndpoints != null && nodeEndpoints.isArray()) {
        nodeEndpoints.elements().forEachRemaining(jsonNode -> {
            try {
                Endpoint endpoint = jsonNode.traverse(jp.getCodec()).readValueAs(Endpoint.class);
                proxy.getEndpoints().add(endpoint);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    JsonNode stripContextNode = node.get("strip_context_path");
    if (stripContextNode != null) {
        proxy.setStripContextPath(stripContextNode.asBoolean(false));
    }

    JsonNode loadBalancingNode = node.get("load_balancing");
    if (loadBalancingNode != null) {
        LoadBalancer loadBalancer = loadBalancingNode.traverse(jp.getCodec()).readValueAs(LoadBalancer.class);
        proxy.setLoadBalancer(loadBalancer);
    }

    JsonNode failoverNode = node.get("failover");
    if (failoverNode != null) {
        Failover failover = failoverNode.traverse(jp.getCodec()).readValueAs(Failover.class);
        proxy.setFailover(failover);
    }

    JsonNode dumpRequestNode = node.get("dumpRequest");
    if (dumpRequestNode != null) {
        boolean dumpRequest = dumpRequestNode.asBoolean(Proxy.DEFAULT_DUMP_REQUEST);
        proxy.setDumpRequest(dumpRequest);
    } else {
        proxy.setDumpRequest(Proxy.DEFAULT_DUMP_REQUEST);
    }

    return proxy;
}

From source file:com.optimizely.ab.config.parser.GroupJacksonDeserializer.java

@Override
public Group deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = parser.getCodec().readTree(parser);

    String id = node.get("id").textValue();
    String policy = node.get("policy").textValue();
    List<TrafficAllocation> trafficAllocations = mapper.readValue(node.get("trafficAllocation").toString(),
            new TypeReference<List<TrafficAllocation>>() {
            });/*from ww w .  j  a  v a  2 s  .  c o  m*/

    JsonNode groupExperimentsJson = node.get("experiments");
    List<Experiment> groupExperiments = new ArrayList<Experiment>();
    if (groupExperimentsJson.isArray()) {
        for (JsonNode groupExperimentJson : groupExperimentsJson) {
            groupExperiments.add(parseExperiment(groupExperimentJson, id));
        }
    }

    return new Group(id, policy, groupExperiments, trafficAllocations);
}

From source file:org.hawkular.rest.json.RelationshipJacksonDeserializer.java

@Override
public Relationship deserialize(JsonParser jp, DeserializationContext deserializationContext)
        throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    String id = node.get(FIELD_ID) != null ? node.get(FIELD_ID).asText() : null;

    // other fields are not compulsory, e.g. when deleting the relationship {id: foo} is just fine
    String name = "";
    if (node.get(FIELD_NAME) != null) {
        name = node.get(FIELD_NAME).asText();
    }//  w w w . j  a  va 2 s . com
    CanonicalPath source = null, target = null;
    if (node.get(FIELD_SOURCE) != null && !node.get(FIELD_SOURCE).asText().isEmpty()) {
        String sourcePath = node.get(FIELD_SOURCE).asText();
        source = CanonicalPath.fromString(sourcePath);
    }
    if (node.get(FIELD_TARGET) != null && !node.get(FIELD_TARGET).asText().isEmpty()) {
        String targetPath = node.get(FIELD_TARGET).asText();
        target = CanonicalPath.fromString(targetPath);
    }

    JsonNode properties = node.get(FIELD_PROPERTIES);
    Map<String, Object> relProperties = null;
    if (properties != null) {
        try {
            Stream<Map.Entry<String, JsonNode>> stream = StreamSupport.stream(
                    Spliterators.spliteratorUnknownSize(properties.fields(), Spliterator.ORDERED), false);

            relProperties = stream.collect(Collectors.toMap(Map.Entry::getKey,
                    ((Function<Map.Entry<String, JsonNode>, JsonNode>) Map.Entry::getValue)
                            .andThen(x -> (Object) x.asText())));
        } catch (Exception e) {
            throw new IllegalArgumentException("Error during relationship deserialization,"
                    + " unable to recognize properties: " + properties);
        }
    }

    return new Relationship(id, name, source, target, relProperties);
}

From source file:com.btmatthews.atlas.core.domain.i18n.LocalizedDeserializer.java

@Override
public Localized deserialize(final JsonParser parser, final DeserializationContext context) throws IOException {
    final LocalizedBuilder<Object> builder = new LocalizedBuilder<>();
    final ObjectCodec codec = parser.getCodec();
    final JsonNode node = codec.readTree(parser);
    final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
    while (iterator.hasNext()) {
        final Map.Entry<String, JsonNode> entry = iterator.next();
        builder.setValue(Locale.forLanguageTag(entry.getKey()), entry.getValue().textValue());
    }/*  w w w  . jav  a2 s .c o  m*/
    return builder.build();
}

From source file:io.gravitee.definition.jackson.datatype.api.deser.EndpointDeserializer.java

@Override
public Endpoint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);

    Endpoint endpoint = new Endpoint();
    endpoint.setTarget(node.get("target").asText());

    JsonNode nameNode = node.get("name");
    if (nameNode != null) {
        String name = nameNode.asText();
        endpoint.setName(name);//from   ww  w .  java 2  s. c  o  m
    } else {
        throw ctxt.mappingException("Endpoint name is required");
    }

    JsonNode weightNode = node.get("weight");
    if (weightNode != null) {
        int weight = weightNode.asInt(Endpoint.DEFAULT_WEIGHT);
        endpoint.setWeight(weight);
    } else {
        endpoint.setWeight(Endpoint.DEFAULT_WEIGHT);
    }

    JsonNode backupNode = node.get("backup");
    if (backupNode != null) {
        boolean backup = backupNode.asBoolean(false);
        endpoint.setBackup(backup);
    } else {
        endpoint.setBackup(false);
    }

    JsonNode healthcheckNode = node.get("healthcheck");
    if (healthcheckNode != null) {
        boolean healthcheck = healthcheckNode.asBoolean(true);
        endpoint.setHealthcheck(healthcheck);
    } else {
        endpoint.setHealthcheck(true);
    }

    JsonNode httpProxyNode = node.get("proxy");
    if (httpProxyNode != null) {
        HttpProxy httpProxy = httpProxyNode.traverse(jp.getCodec()).readValueAs(HttpProxy.class);
        endpoint.setHttpProxy(httpProxy);
    }

    JsonNode httpClientOptionsNode = node.get("http");
    if (httpClientOptionsNode != null) {
        HttpClientOptions httpClientOptions = httpClientOptionsNode.traverse(jp.getCodec())
                .readValueAs(HttpClientOptions.class);
        endpoint.setHttpClientOptions(httpClientOptions);
    } else {
        endpoint.setHttpClientOptions(new HttpClientOptions());
    }

    JsonNode httpClientSslOptionsNode = node.get("ssl");
    if (httpClientSslOptionsNode != null) {
        HttpClientSslOptions httpClientSslOptions = httpClientSslOptionsNode.traverse(jp.getCodec())
                .readValueAs(HttpClientSslOptions.class);
        endpoint.setHttpClientSslOptions(httpClientSslOptions);
    }

    return endpoint;
}

From source file:io.gravitee.definition.jackson.datatype.api.deser.HttpClientOptionsDeserializer.java

@Override
public HttpClientOptions deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);

    HttpClientOptions httpClientOptions = new HttpClientOptions();

    JsonNode connectTimeoutNode = node.get("connectTimeout");
    if (connectTimeoutNode != null) {
        long connectTimeout = connectTimeoutNode.asLong(HttpClientOptions.DEFAULT_CONNECT_TIMEOUT);
        httpClientOptions.setConnectTimeout(connectTimeout);
    } else {/*  w w  w .j a  va2  s  .com*/
        httpClientOptions.setConnectTimeout(HttpClientOptions.DEFAULT_CONNECT_TIMEOUT);
    }

    JsonNode readTimeoutNode = node.get("readTimeout");
    if (readTimeoutNode != null) {
        long readTimeout = readTimeoutNode.asLong(HttpClientOptions.DEFAULT_READ_TIMEOUT);
        httpClientOptions.setReadTimeout(readTimeout);
    } else {
        httpClientOptions.setReadTimeout(HttpClientOptions.DEFAULT_READ_TIMEOUT);
    }

    JsonNode idleTimeoutNode = node.get("idleTimeout");
    if (idleTimeoutNode != null) {
        long idleTimeout = idleTimeoutNode.asLong(HttpClientOptions.DEFAULT_IDLE_TIMEOUT);
        httpClientOptions.setIdleTimeout(idleTimeout);
    } else {
        httpClientOptions.setIdleTimeout(HttpClientOptions.DEFAULT_IDLE_TIMEOUT);
    }

    JsonNode keepAliveNode = node.get("keepAlive");
    if (keepAliveNode != null) {
        boolean keepAlive = keepAliveNode.asBoolean(HttpClientOptions.DEFAULT_KEEP_ALIVE);
        httpClientOptions.setKeepAlive(keepAlive);
    } else {
        httpClientOptions.setKeepAlive(HttpClientOptions.DEFAULT_KEEP_ALIVE);
    }

    JsonNode pipeliningNode = node.get("pipelining");
    if (pipeliningNode != null) {
        boolean pipelining = pipeliningNode.asBoolean(HttpClientOptions.DEFAULT_PIPELINING);
        httpClientOptions.setPipelining(pipelining);
    } else {
        httpClientOptions.setPipelining(HttpClientOptions.DEFAULT_PIPELINING);
    }

    JsonNode maxConcurrentConnectionsNode = node.get("maxConcurrentConnections");
    if (maxConcurrentConnectionsNode != null) {
        int maxConcurrentConnections = maxConcurrentConnectionsNode
                .asInt(HttpClientOptions.DEFAULT_MAX_CONCURRENT_CONNECTIONS);
        httpClientOptions.setMaxConcurrentConnections(maxConcurrentConnections);
    } else {
        httpClientOptions.setMaxConcurrentConnections(HttpClientOptions.DEFAULT_MAX_CONCURRENT_CONNECTIONS);
    }

    JsonNode useCompressionNode = node.get("useCompression");
    if (useCompressionNode != null) {
        boolean useCompression = useCompressionNode.asBoolean(HttpClientOptions.DEFAULT_USE_COMPRESSION);
        httpClientOptions.setUseCompression(useCompression);
    } else {
        httpClientOptions.setUseCompression(HttpClientOptions.DEFAULT_USE_COMPRESSION);
    }

    return httpClientOptions;
}