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.hp.autonomy.frontend.find.hod.search.HodQueryRestrictionsDeserializer.java

@Override
public QueryRestrictions<ResourceIdentifier> deserialize(final JsonParser jsonParser,
        final DeserializationContext deserializationContext) throws IOException {
    final ObjectMapper objectMapper = createObjectMapper();

    final JsonNode node = jsonParser.getCodec().readTree(jsonParser);

    return new HodQueryRestrictions.Builder().setQueryText(parseAsText(objectMapper, node, "queryText"))
            .setFieldText(parseAsText(objectMapper, node, "fieldText"))
            .setDatabases(parseDatabaseArray(node, "databases"))
            .setMinDate(parseDate(objectMapper, node, "minDate"))
            .setMaxDate(parseDate(objectMapper, node, "maxDate"))
            .setLanguageType(parseAsText(objectMapper, node, "languageType")).build();
}

From source file:com.sg2net.utilities.ListaCAP.json.ComuneDeserializer.java

@Override
public Comune deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(jsonParser);
    JsonNode codiceIstatNode = root.get("codiceIstat");
    String codiceIstat = codiceIstatNode.asText();
    JsonNode codiceCatastaleNode = root.get("codiceCatastale");
    String codiceCatastale = codiceCatastaleNode.asText();
    JsonNode nomeNode = root.get("nome");
    String nome = nomeNode.asText();
    JsonNode provinciaNode = root.get("provincia");
    String provincia = provinciaNode.asText();
    JsonNode codiciCapNode = root.get("codiciCap");
    Collection<String> codiciCap = new ArrayList<>();
    Iterator<JsonNode> capNodes = codiciCapNode.elements();
    while (capNodes.hasNext()) {
        JsonNode codiceCapNode = capNodes.next();
        String codiceCap = codiceCapNode.asText();

        codiciCap.add(codiceCap);//from   ww w.ja  va  2 s  .c  om
    }
    Comune comune = new Comune(codiceIstat, codiceCatastale, nome, provincia);
    comune.setCodiciCap(codiciCap);
    logger.trace("comune =" + comune + " deserializzato from json");
    return comune;
}

From source file:com.hp.autonomy.frontend.find.idol.search.IdolQueryRestrictionsDeserializer.java

@Override
public QueryRestrictions<String> deserialize(final JsonParser jsonParser,
        final DeserializationContext deserializationContext) throws IOException {
    final ObjectMapper objectMapper = createObjectMapper();

    final JsonNode node = jsonParser.getCodec().readTree(jsonParser);

    return new IdolQueryRestrictions.Builder().setQueryText(parseAsText(objectMapper, node, "queryText"))
            .setFieldText(parseAsText(objectMapper, node, "fieldText"))
            .setDatabases(parseDatabaseArray(node, "databases"))
            .setMinDate(parseDate(objectMapper, node, "minDate"))
            .setMaxDate(parseDate(objectMapper, node, "maxDate"))
            .setLanguageType(parseAsText(objectMapper, node, "languageType"))
            .setStateMatchId(parseStringArray(node, "stateMatchId"))
            .setStateDontMatchId(parseStringArray(node, "stateDontMatchId"))
            .setAnyLanguage(parseAsBoolean(objectMapper, node, "anyLanguage")).build();
}

From source file:org.wikidata.wdtk.datamodel.json.jackson.datavalues.JacksonValueDeserializer.java

@Override
public JacksonValue deserialize(JsonParser jsonParser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
    JsonNode root = mapper.readTree(jsonParser);
    Class<? extends JacksonValue> valueClass = getValueClass(root);

    return mapper.treeToValue(root, valueClass);
}

From source file:eu.bittrade.libs.steemj.base.models.deserializer.GuestBloggerPairDeserializer.java

@Override
public List<Pair<AccountName, Long>> deserialize(JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException {

    List<Pair<AccountName, Long>> result = new ArrayList<>();

    ObjectCodec codec = jsonParser.getCodec();
    TreeNode rootNode = codec.readTree(jsonParser);

    if (rootNode.isArray()) {
        for (JsonNode node : (ArrayNode) rootNode) {
            // result.put((node.get(0)).asText(), (node.get(0)).asInt());
        }//ww  w.  ja v a 2s.c  o  m

        return result;
    }

    throw new IllegalArgumentException("JSON Node is not an array.");
}

From source file:org.apache.olingo.commons.core.serialization.JsonPropertyDeserializer.java

protected ResWrap<Property> doDeserialize(final JsonParser parser) throws IOException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    final String metadataETag;
    final URI contextURL;
    final PropertyImpl property = new PropertyImpl();

    if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) {
        metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue();
        tree.remove(Constants.JSON_METADATA_ETAG);
    } else {/*from w  ww .j  av a2 s  .co m*/
        metadataETag = null;
    }

    if (tree.hasNonNull(Constants.JSON_CONTEXT)) {
        contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue());
        property.setName(StringUtils.substringAfterLast(contextURL.toASCIIString(), "/"));
        tree.remove(Constants.JSON_CONTEXT);
    } else if (tree.hasNonNull(Constants.JSON_METADATA)) {
        contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
        property.setType(new EdmTypeInfo.Builder()
                .setTypeExpression(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#")).build()
                .internal());
        tree.remove(Constants.JSON_METADATA);
    } else {
        contextURL = null;
    }

    if (tree.has(jsonType)) {
        property.setType(
                new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
        tree.remove(jsonType);
    }

    if (tree.has(Constants.JSON_NULL) && tree.get(Constants.JSON_NULL).asBoolean()) {
        property.setValue(ValueType.PRIMITIVE, null);
        tree.remove(Constants.JSON_NULL);
    }

    if (property.getValue() == null) {
        try {
            value(property, tree.has(Constants.VALUE) ? tree.get(Constants.VALUE) : tree, parser.getCodec());
        } catch (final EdmPrimitiveTypeException e) {
            throw new IOException(e);
        }
        tree.remove(Constants.VALUE);
    }

    // any remaining entry is supposed to be an annotation or is ignored
    for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) {
        final Map.Entry<String, JsonNode> field = itor.next();
        if (field.getKey().charAt(0) == '@') {
            final Annotation annotation = new AnnotationImpl();
            annotation.setTerm(field.getKey().substring(1));

            try {
                value(annotation, field.getValue(), parser.getCodec());
            } catch (final EdmPrimitiveTypeException e) {
                throw new IOException(e);
            }
            property.getAnnotations().add(annotation);
        }
    }

    return new ResWrap<Property>(contextURL, metadataETag, property);
}

From source file:io.alicorn.data.models.CoordinateDeserializer.java

@Override
public GeospatialCoordinate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    GeospatialCoordinate geospatialCoordinate = new GeospatialCoordinate();
    JsonNode jn = jsonParser.getCodec().readTree(jsonParser);
    geospatialCoordinate.setLongitude(jn.get("coordinates").get(0).asDouble());
    geospatialCoordinate.setLatitude(jn.get("coordinates").get(1).asDouble());
    return geospatialCoordinate;
}

From source file:org.createnet.raptor.models.objects.deserializer.RecordSetDeserializer.java

@Override
public RecordSet deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {

    JsonNode tree = jp.getCodec().readTree(jp);
    RecordSet recordset = new RecordSet();

    if (tree.has("userId")) {
        recordset.userId = tree.get("userId").asText();
    }// www .  jav a 2 s.c  o m
    if (tree.has("objectId")) {
        recordset.objectId = tree.get("objectId").asText();
    }
    if (tree.has("streamId")) {
        recordset.streamId = tree.get("streamId").asText();
    }

    long time = System.currentTimeMillis();

    if (tree.has("channels")) {

        if (tree.has("timestamp")) {
            long time1 = tree.get("timestamp").asLong();
            if (time1 > 0) {
                time = time1 * 1000;
            }
        } else if (tree.has("lastUpdate")) {
            long time1 = tree.get("lastUpdate").asLong();
            if (time1 > 0) {
                time = time1 * 1000;
            }
        }
        tree = tree.get("channels");
    }

    recordset.setTimestamp(new Date(time));

    if (tree.isObject()) {

        Iterator<String> it = tree.fieldNames();
        while (it.hasNext()) {

            String channelName = it.next();
            JsonNode channelNode = tree.get(channelName);

            if (channelNode.isObject()) {
                if (channelNode.has("current-value")) {
                    channelNode = channelNode.get("current-value");
                }
            }

            for (Map.Entry<String, Record> item : TypesManager.getTypes().entrySet()) {
                try {

                    Record recordType = item.getValue();
                    Object val = recordType.parseValue(channelNode);

                    Record instance = (Record) recordType.getClass().newInstance();
                    instance.setValue(val);
                    Channel channel = new Channel();

                    channel.name = channelName;
                    channel.type = instance.getType();
                    instance.setChannel(channel);

                    recordset.channels.put(channelName, instance);
                    break;

                } catch (RaptorComponent.ParserException | InstantiationException | IllegalAccessException e) {
                }
            }

        }
    }

    return recordset;
}

From source file:com.strandls.alchemy.rest.client.exception.ExceptionPayloadDeserializer.java

@SuppressWarnings("unchecked")
@Override/*from   w w w .j  a va 2 s. c  om*/
public ExceptionPayload deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    final ObjectMapper sourceObjectMapper = ((ObjectMapper) jp.getCodec());

    final ObjectNode tree = jp.readValueAsTree();

    String message = null;
    if (tree.has("exceptionMessage")) {
        message = tree.get("exceptionMessage").asText();
    }

    Throwable exception = null;
    try {
        final String className = tree.get("exceptionClassFQN").asText();
        final Class<? extends Throwable> clazz = (Class<? extends Throwable>) ReflectionUtils
                .forName(className);
        exception = sourceObjectMapper.treeToValue(tree.get("exception"), clazz);
    } catch (final Throwable t) {
        log.warn("Error deserializing exception class", t);
        exception = new InternalServerErrorException(message);
    }

    return new ExceptionPayload(exception.getClass().getName(), message, exception);
}

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

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

    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);
    // Get username
    String username = node.get("username") == null ? "" : node.get("username").textValue();
    // Get first name
    String firstName = node.get("first_name") == null ? "" : node.get("first_name").textValue();
    // Get last name
    String lastName = node.get("last_name") == null ? "" : node.get("last_name").textValue();
    //  Get email address
    String email = node.get("email") == null ? "" : node.get("email").textValue();
    // Get organization
    String organization = node.get("organization") == null ? "" : node.get("organization").textValue();
    // Get user group
    String groupStr = node.get("group") == null ? "" : node.get("group").textValue();
    // Get owner id
    int ownerId = node.get("owner_id") == null ? 0 : node.get("owner_id").intValue();
    // Get password
    String password = node.get("password") == null ? "" : node.get("password").textValue();
    // Get control password
    String passwordControl = node.get("password_control") == null ? ""
            : node.get("password_control").textValue();

    // Get usersService bean from Application Context
    ConverterService converterService = (ConverterService) ApplicationContextUtils.getApplicationContext()
            .getBean("converterService");
    // Get UserGroup
    UserGroup group = (UserGroup) converterService.convert(groupStr, UserGroup.class, null);
    // Get usersService bean from Application Context
    OwnersService ownersService = (OwnersService) ApplicationContextUtils.getApplicationContext()
            .getBean("ownersService");
    // Get Owner//  ww  w.  j a v a 2  s. com
    Owner owner = ownersService.getOwner(ownerId);

    // Create new UserFull object
    UserFull user = new UserFull();
    user.setUsername(username);
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setEmail(email);
    user.setOrganization(organization);
    user.setPasswordUi(password);
    user.setPasswordControl(passwordControl);
    user.setOwner(owner);

    // Create new UserInfo object
    UserInfo info = new UserInfo();
    // Set user group
    info.setGroup(group);
    // Set user
    info.setUser(user);

    return info;
}