Example usage for com.fasterxml.jackson.core ObjectCodec readTree

List of usage examples for com.fasterxml.jackson.core ObjectCodec readTree

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core ObjectCodec readTree.

Prototype

public abstract <T extends TreeNode> T readTree(JsonParser jp) throws IOException, JsonProcessingException;

Source Link

Document

Method to deserialize JSON content as tree expressed using set of TreeNode instances.

Usage

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

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

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

    // Deserialize shelfNumber
    String shelfNumber = node.get("shelf_number") == null ? "" : node.get("shelf_number").textValue();
    // Set value/*from   ww w  . j  a  v  a 2  s. c  o m*/
    shelf.setShelfNumber(shelfNumber);

    // Deserialize collection id
    int collectionId = node.get("collection_id") == null ? 0 : node.get("collection_id").intValue();
    if (collectionId != 0) {
        shelf.setCollection(new LibraryCollection(collectionId));
    }
    // Return the collection
    return shelf;
}

From source file:org.springframework.xd.rest.client.impl.support.JobParameterJacksonDeserializer.java

@Override
public JobParameter deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);

    final String value = node.get("value").asText();
    final boolean identifying = node.get("identifying").asBoolean();
    final String type = node.get("type").asText();

    final JobParameter jobParameter;

    if (!type.isEmpty() && !type.equalsIgnoreCase("STRING")) {
        if ("DATE".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(DateTime.parse(value).toDate(), identifying);
        } else if ("DOUBLE".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(Double.valueOf(value), identifying);
        } else if ("LONG".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(Long.valueOf(value), identifying);
        } else {/*from  w ww  .j  a  v a2s  .  c  om*/
            throw new IllegalStateException("Unsupported JobParameter type: " + type);
        }
    } else {
        jobParameter = new JobParameter(value, identifying);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("jobParameter - value: %s (type: %s, isIdentifying: %s)",
                jobParameter.getValue(), jobParameter.getType().name(), jobParameter.isIdentifying()));
    }

    return jobParameter;
}

From source file:org.springframework.cloud.dataflow.rest.client.support.JobParameterJacksonDeserializer.java

@Override
public JobParameter deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);

    final String value = node.get("value").asText();
    final boolean identifying = node.get("identifying").asBoolean();
    final String type = node.get("type").asText();

    final JobParameter jobParameter;

    if (!type.isEmpty() && !type.equalsIgnoreCase("STRING")) {
        if ("DATE".equalsIgnoreCase(type)) {
            // TODO: when upgraded to Java8 use java DateTime
            jobParameter = new JobParameter(DateTime.parse(value).toDate(), identifying);
        } else if ("DOUBLE".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(Double.valueOf(value), identifying);
        } else if ("LONG".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(Long.valueOf(value), identifying);
        } else {/*from w w w  .ja  va  2s. com*/
            throw new IllegalStateException("Unsupported JobParameter type: " + type);
        }
    } else {
        jobParameter = new JobParameter(value, identifying);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("jobParameter - value: %s (type: %s, isIdentifying: %s)",
                jobParameter.getValue(), jobParameter.getType().name(), jobParameter.isIdentifying()));
    }

    return jobParameter;
}

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);
    }//from   www . java2 s. 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:org.springframework.batch.admin.domain.support.JobParameterJacksonDeserializer.java

@Override
public JobParameter deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
    SimpleDateFormat formatter = new SimpleDateFormat();
    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);

    final String value = node.get("value").asText();
    final boolean identifying = node.get("identifying").asBoolean();
    final String type = node.get("type").asText();

    final JobParameter jobParameter;

    if (!type.isEmpty() && !type.equalsIgnoreCase("STRING")) {
        if ("DATE".equalsIgnoreCase(type)) {
            try {
                jobParameter = new JobParameter(formatter.parse(value), identifying);
            } catch (ParseException e) {
                throw new IOException(e);
            }//from   www .  j a v a2  s  . c o  m
        } else if ("DOUBLE".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(Double.valueOf(value), identifying);
        } else if ("LONG".equalsIgnoreCase(type)) {
            jobParameter = new JobParameter(Long.valueOf(value), identifying);
        } else {
            throw new IllegalStateException("Unsupported JobParameter type: " + type);
        }
    } else {
        jobParameter = new JobParameter(value, identifying);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("jobParameter - value: %s (type: %s, isIdentifying: %s)",
                jobParameter.getValue(), jobParameter.getType().name(), jobParameter.isIdentifying()));
    }

    return jobParameter;
}

From source file:piazza.services.ingest.util.GeoJsonDeserializer.java

@Override
public Geometry deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectCodec oc = jp.getCodec();
    JsonNode root = oc.readTree(jp);
    return parseGeometry(root);
}

From source file:org.bonitasoft.web.designer.model.contract.databind.ContractDeserializer.java

@Override
public Contract deserialize(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectCodec oc = parser.getCodec();
    ObjectNode treeNode = oc.readTree(parser);
    Contract contract = new Contract();
    parseNodeContractInput(childInput(treeNode), contract);
    return contract;
}

From source file:things.thing.ThingDeserializer.java

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

    ObjectCodec oc = jp.getCodec();
    JsonNode node = null;/*w  ww  .  java2 s  . c  om*/
    node = oc.readTree(jp);

    JsonNode keyNode = node.get("key");
    String key = null;
    if (keyNode != null) {
        key = keyNode.asText();
    }

    JsonNode typeNode = node.get("type");
    String type = null;
    if (typeNode != null) {
        type = typeNode.asText();
    }

    String id = null;
    JsonNode idNode = node.get("id");
    if (idNode != null) {
        id = idNode.asText();
    }

    Thing t = new Thing();
    t.setKey(key);
    t.setId(id);
    t.setThingType(type);

    JsonNode valueNode = node.get("value");
    if (valueNode != null) {
        Object p = objectMapper.treeToValue(valueNode, tr.getTypeClass(type));
        t.setValue(p);
    }

    return t;
}

From source file:org.apache.unomi.persistence.spi.PropertyTypedObjectDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp.getCurrentTokenId() != JsonTokenId.ID_START_OBJECT) {
        return super.deserialize(jp, ctxt);
    }//  w w w.  j  a  v a2s . co  m
    ObjectCodec codec = jp.getCodec();
    TreeNode treeNode = codec.readTree(jp);
    Class<? extends Object> objectClass = null;
    if (treeNode instanceof ObjectNode) {
        ObjectNode root = (ObjectNode) treeNode;
        Iterator<Map.Entry<String, JsonNode>> elementsIterator = root.fields();
        while (elementsIterator.hasNext()) {
            Map.Entry<String, JsonNode> element = elementsIterator.next();
            String name = element.getKey();
            if (fieldValuesToMatch.containsKey(name)) {
                Set<String> valuesToMatch = fieldValuesToMatch.get(name);
                for (String valueToMatch : valuesToMatch) {
                    if (element.getValue().asText().matches(valueToMatch)) {
                        objectClass = registry.get(name + "=" + valueToMatch);
                        break;
                    }
                }
                if (objectClass != null) {
                    break;
                }
            }
        }
        if (objectClass == null) {
            objectClass = HashMap.class;
        }
    } else {

    }
    if (objectClass == null) {
        return super.deserialize(codec.treeAsTokens(treeNode), ctxt);
    }
    return codec.treeToValue(treeNode, objectClass);
}

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/*from  w  w w.  j ava2  s. co m*/
    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;
}