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:things.thing.ThingDeserializer.java

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

    ObjectCodec oc = jp.getCodec();
    JsonNode node = null;/*from   w  ww .  j av a  2 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.calrissian.mango.json.deser.NodeDeserializer.java

/**
 * {"or":{"children":[{"and":{"children":[{"eq":{"key":"k1","type":"string","value":"v1"}},{"neq":{"key":"k2","type":"ipv4","value":"1.2.3.4"}}]}},{"and":{"children":[{"eq":{"key":"k3","type":"integer","value":"1234"}}]}}]}}
 *//*w  w w . ja  v a 2 s. c  o m*/
@Override
public Node deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
    JsonNode root = jsonParser.getCodec().readTree(jsonParser);
    Iterator<String> fieldNames = root.fieldNames();
    if (fieldNames.hasNext()) {
        String fieldKey = fieldNames.next();
        JsonNode fieldJson = root.get(fieldKey);
        return parseField(fieldKey, fieldJson); //only expecting one root node
    }
    return null;
}

From source file:com.addthis.codec.jackson.WriteonlyPropertyIgnorer.java

@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser jp,
        JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName)
        throws IOException, JsonProcessingException {
    ObjectCodec objectCodec = jp.getCodec();
    if (objectCodec instanceof ObjectMapper) {
        ObjectMapper objectMapper = (ObjectMapper) objectCodec;
        BeanDescription beanDescription = objectMapper.getSerializationConfig()
                .introspect(ctxt.constructType(beanOrClass.getClass()));
        for (BeanPropertyDefinition propertyDefinition : beanDescription.findProperties()) {
            if (propertyName.equals(propertyDefinition.getName())) {
                jp.skipChildren();/*w  w  w . j  av a 2  s .  c o m*/
                return true;
            }
        }
    }
    return false;
}

From source file:br.com.criativasoft.opendevice.core.json.CommandDeserialize.java

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

    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(jp);
    Class<? extends Command> commandClass = null;

    JsonNode jsonType = root.get("type");
    int type = jsonType.intValue();

    CommandType commandType = CommandType.getByCode(type);

    if (commandType == null)
        throw new IllegalArgumentException("type of command must be provided !");

    if (DeviceCommand.isCompatible(commandType)) {
        return mapper.readValue(root.toString(), DeviceCommand.class);
    } else {//from www.j a  va2 s .c  om

        Class<? extends Command> cmdClass = registry.get(commandType);

        if (cmdClass == null) {
            throw new IllegalArgumentException(
                    "Command type not supported!! You need configure in CommandDeserialize");
        }

        return mapper.readValue(root.toString(), cmdClass);

    }

}

From source file:org.dswarm.graph.json.deserializer.ModelDeserializer.java

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

    final ObjectCodec oc = jp.getCodec();

    if (oc == null) {

        return null;
    }/* w  ww.j a va2s  .  co m*/

    final JsonNode node = oc.readTree(jp);

    if (node == null) {

        return null;
    }

    if (!ArrayNode.class.isInstance(node)) {

        throw new JsonParseException("expected a JSON array full of resource objects of the model",
                jp.getCurrentLocation());
    }

    if (node.size() <= 0) {

        return null;
    }

    final Model model = new Model();

    for (final JsonNode resourceNode : node) {

        final Resource resource = resourceNode.traverse(oc).readValueAs(Resource.class);
        model.addResource(resource);
    }

    return model;
}

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: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 www  . j a  va 2s  .  co  m*/
            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. jav  a 2 s .c  o m
            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.anhonesteffort.flock.registration.model.SubscriptionPlanDeserializer.java

@Override
public SubscriptionPlan deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    try {/*from   w  w w. j  ava 2 s. c o  m*/

        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        Integer planType = (Integer) node.get("plan_type").numberValue();
        String accountId = node.get("account_id").textValue();
        String planId = node.get("plan_id").textValue();

        switch (planType) {
        case SubscriptionPlan.PLAN_TYPE_NONE:
            return new SubscriptionPlan(accountId, planType, planId);

        case SubscriptionPlan.PLAN_TYPE_STRIPE:
            return new StripePlan(accountId, planId);

        case SubscriptionPlan.PLAN_TYPE_GOOGLE:
            String purchaseToken = node.get("purchase_token").textValue();
            Long expirationDate = (Long) node.get("expiration_date").numberValue();
            return new GooglePlan(accountId, planId, purchaseToken, expirationDate);

        default: {
            throw new JsonParseException("unknown plan type " + planType, JsonLocation.NA);
        }
        }

    } catch (NullPointerException e) {
        throw new JsonParseException("something was null D:", JsonLocation.NA);
    }
}

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

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

    List<Pair<String, 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());
        }//from w w w. jav a 2 s.com

        return result;
    }

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