Search in sources :

Example 1 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project jackson-databind by FasterXML.

the class TestBeanConversions method testNodeConvert.

// [Issue-11]: simple cast, for Tree
public void testNodeConvert() throws Exception {
    ObjectNode src = (ObjectNode) MAPPER.readTree("{}");
    TreeNode node = src;
    ObjectNode result = MAPPER.treeToValue(node, ObjectNode.class);
    // should just cast...
    assertSame(src, result);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TreeNode(com.fasterxml.jackson.core.TreeNode)

Example 2 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project beam by apache.

the class TestPipeline method convertToArgs.

public static String[] convertToArgs(PipelineOptions options) {
    try {
        byte[] opts = MAPPER.writeValueAsBytes(options);
        JsonParser jsonParser = MAPPER.getFactory().createParser(opts);
        TreeNode node = jsonParser.readValueAsTree();
        ObjectNode optsNode = (ObjectNode) node.get("options");
        ArrayList<String> optArrayList = new ArrayList<>();
        Iterator<Entry<String, JsonNode>> entries = optsNode.fields();
        while (entries.hasNext()) {
            Entry<String, JsonNode> entry = entries.next();
            if (entry.getValue().isNull()) {
                continue;
            } else if (entry.getValue().isTextual()) {
                optArrayList.add("--" + entry.getKey() + "=" + entry.getValue().asText());
            } else {
                optArrayList.add("--" + entry.getKey() + "=" + entry.getValue());
            }
        }
        return optArrayList.toArray(new String[optArrayList.size()]);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Entry(java.util.Map.Entry) TreeNode(com.fasterxml.jackson.core.TreeNode) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 3 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project realm-java by realm.

the class RealmListNYTimesMultimediumDeserializer method deserialize.

@Override
public List<NYTimesMultimedium> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    RealmList<NYTimesMultimedium> list = new RealmList<>();
    TreeNode treeNode = jp.getCodec().readTree(jp);
    if (!(treeNode instanceof ArrayNode)) {
        return list;
    }
    ArrayNode arrayNode = (ArrayNode) treeNode;
    for (JsonNode node : arrayNode) {
        NYTimesMultimedium nyTimesMultimedium = objectMapper.treeToValue(node, NYTimesMultimedium.class);
        list.add(nyTimesMultimedium);
    }
    return list;
}
Also used : RealmList(io.realm.RealmList) TreeNode(com.fasterxml.jackson.core.TreeNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) NYTimesMultimedium(io.realm.examples.newsreader.model.entity.NYTimesMultimedium) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project Gaffer by gchq.

the class HyperLogLogPlusJsonDeserialiser method deserialize.

// TODO - See 'Can't create HyperLogLogPlus sketches in JSON'
@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 {
        throw new IllegalArgumentException("Recieved null or empty HyperLogLogPlus sketch");
    }
}
Also used : HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) TreeNode(com.fasterxml.jackson.core.TreeNode) TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 5 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project steve by RWTH-i5-IDSG.

the class Deserializer method handleError.

/**
 * Do NOT catch and handle exceptions for incoming RESPONSEs. Let the processing fail.
 * There is no mechanism in OCPP to report back such erroneous messages.
 */
private void handleError(CommunicationContext context, String messageId, JsonParser parser) {
    FutureResponseContext responseContext = futureResponseContextStore.get(context.getSession(), messageId);
    if (responseContext == null) {
        throw new SteveException("An error message was received as response to a not-sent call. The message was: %s", context.getIncomingString());
    }
    ErrorCode code;
    String desc;
    String details = null;
    try {
        parser.nextToken();
        code = ErrorCode.fromValue(parser.getText());
        parser.nextToken();
        desc = parser.getText();
        // ErrorDescription - Should be filled in if possible, otherwise a clear empty string "".
        if ("".equals(desc)) {
            desc = null;
        }
        // From spec:
        // ErrorDetails - This JSON object describes error details in an undefined way.
        // If there are no error details you should fill in an empty object {}, missing or null is not allowed
        parser.nextToken();
        TreeNode detailsNode = parser.readValueAsTree();
        if (detailsNode != null && detailsNode.size() != 0) {
            details = mapper.writeValueAsString(detailsNode);
        }
    } catch (IOException e) {
        throw new SteveException("Deserialization of incoming error message failed", e);
    }
    OcppJsonError error = new OcppJsonError();
    error.setMessageId(messageId);
    error.setErrorCode(code);
    error.setErrorDescription(desc);
    error.setErrorDetails(details);
    context.setIncomingMessage(error);
    context.createErrorHandler(responseContext.getTask());
}
Also used : TreeNode(com.fasterxml.jackson.core.TreeNode) OcppJsonError(de.rwth.idsg.steve.ocpp.ws.data.OcppJsonError) ErrorCode(de.rwth.idsg.steve.ocpp.ws.data.ErrorCode) IOException(java.io.IOException) FutureResponseContext(de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext) SteveException(de.rwth.idsg.steve.SteveException)

Aggregations

TreeNode (com.fasterxml.jackson.core.TreeNode)21 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 IOException (java.io.IOException)5 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)4 BsonParser (de.undercouch.bson4jackson.BsonParser)4 JsonParser (com.fasterxml.jackson.core.JsonParser)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 HashMap (java.util.HashMap)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2 IntNode (com.fasterxml.jackson.databind.node.IntNode)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CandlepinJsonProcessingException (org.candlepin.common.exceptions.CandlepinJsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CommandRecord (com.kloia.eventapis.pojos.CommandRecord)1 HllSketch (com.yahoo.sketches.hll.HllSketch)1 SteveException (de.rwth.idsg.steve.SteveException)1 ErrorCode (de.rwth.idsg.steve.ocpp.ws.data.ErrorCode)1