Example usage for com.fasterxml.jackson.databind JsonNode asText

List of usage examples for com.fasterxml.jackson.databind JsonNode asText

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode asText.

Prototype

public abstract String asText();

Source Link

Usage

From source file:org.bndtools.rt.repository.marshall.CapReqJson.java

private static void toAttribute(CapReqBuilder builder, JsonNode attrNode) throws IllegalArgumentException {
    String name = getRequiredValueField(attrNode, "name").asText();
    Object value;/*from   w  w w. j a  v  a2  s.  c o  m*/
    JsonNode valueNode = getRequiredValueField(attrNode, "value");

    JsonNode typeNode = attrNode.get("type");
    AttributeType type = AttributeType.DEFAULT;
    if (typeNode != null) {
        String typeName = typeNode.asText();
        type = AttributeType.parseTypeName(typeName);
    }

    if (valueNode.isFloatingPointNumber()) {
        if (typeNode == null || type.equals(AttributeType.DOUBLE))
            value = valueNode.asDouble();
        else
            throw new IllegalArgumentException(String
                    .format("JSON type for value is floating point, does not match declared type '%s'", type));
    } else if (valueNode.isIntegralNumber()) {
        if (typeNode == null || type.equals(AttributeType.LONG))
            value = valueNode.asLong();
        else if (type.equals(AttributeType.DOUBLE))
            value = valueNode.asDouble();
        else
            throw new IllegalArgumentException(
                    String.format("JSON type for value is integral, does not match declared type '%s'", type));
    } else if (valueNode.isTextual()) {
        String valueStr = valueNode.asText();
        value = type.parseString(valueStr);
    } else {
        throw new IllegalArgumentException("JSON value node is not a recognised type");
    }

    builder.addAttribute(name, value);
}

From source file:com.amazonaws.services.kinesis.aggregators.StreamAggregatorUtils.java

public static String readValueAsString(JsonNode json, String atPath) {
    JsonNode node = readJsonValue(json, atPath);

    return node == null ? null : node.asText();
}

From source file:com.baasbox.commands.ScriptsResource.java

private static JsonNode storageCommand(JsonNode command, JsonCallback callback) throws CommandException {
    JsonNode moduleId = command.get(ScriptCommand.ID);

    if (moduleId == null || !moduleId.isTextual()) {
        throw new CommandParsingException(command, "error parsing module id");
    }//from   w  w w. j  ava  2 s. c  o m
    String id = moduleId.asText();
    JsonNode params = command.get(ScriptCommand.PARAMS);
    if (params == null || !params.isObject()) {
        throw new CommandParsingException(command, "error parsing params");
    }
    JsonNode actionNode = params.get("action");
    if (actionNode == null || !actionNode.isTextual()) {
        throw new CommandParsingException(command, "error parsing action");
    }
    String action = actionNode.asText();
    ODocument result;
    if ("get".equals(action)) {
        try {
            result = ScriptingService.getStore(id);
        } catch (ScriptException e) {
            //should never happen
            throw new CommandExecutionException(command, "script does not exists");
        }
    } else if ("set".equals(action)) {
        JsonNode args = params.get("args");
        if (args == null) {
            args = NullNode.getInstance();
        }
        if (!args.isObject() && !args.isNull()) {
            throw new CommandExecutionException(command, "Stored values should be objects or null");
        }
        try {
            result = ScriptingService.resetStore(id, args);
        } catch (ScriptException e) {
            throw new CommandExecutionException(command, "script does not exists");
        }
    } else if ("swap".equals(action)) {
        if (callback == null)
            throw new CommandExecutionException(command, "missing function callback");
        try {
            result = ScriptingService.swap(id, callback);
        } catch (ScriptException e) {
            throw new CommandExecutionException(command, ExceptionUtils.getMessage(e), e);
        }
    } else if ("trade".equals(action)) {
        if (callback == null)
            throw new CommandExecutionException(command, "missing function callback");
        try {
            result = ScriptingService.trade(id, callback);
        } catch (ScriptException e) {
            throw new CommandExecutionException(command, ExceptionUtils.getMessage(e), e);
        }
    } else {
        throw new CommandParsingException(command, "unknown action: " + action);
    }
    if (result == null) {
        return NullNode.getInstance();
    } else {
        String s = result.toJSON();
        try {
            ObjectNode jsonNode = (ObjectNode) Json.mapper().readTree(s);
            jsonNode.remove("@version");
            jsonNode.remove("@type");
            return jsonNode;
        } catch (IOException e) {
            throw new CommandExecutionException(command, "error converting result", e);
        }
    }
}

From source file:com.facebook.api.FacebookPostActivitySerializer.java

private static DateTime parseDate(JsonNode value) throws ActivitySerializerException {
    try {/* w  ww.j  ava2 s. c  o m*/
        return FACEBOOK_FORMAT.parseDateTime(value.asText());
    } catch (Exception e) {
        throw new ActivitySerializerException("Unable to parse date " + value.asText());
    }
}

From source file:com.mirth.connect.client.ui.components.rsta.AutoCompleteProperties.java

static AutoCompleteProperties fromJSON(String autoCompleteJSON) {
    Boolean activateAfterLetters = null;
    String activateAfterOthers = null;
    Integer activationDelay = null;

    if (StringUtils.isNotBlank(autoCompleteJSON)) {
        try {// w w  w  .  j  a  v a2  s. c  o m
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode rootNode = (ObjectNode) mapper.readTree(autoCompleteJSON);

            JsonNode node = rootNode.get("activateAfterLetters");
            if (node != null) {
                activateAfterLetters = node.asBoolean();
            }

            node = rootNode.get("activateAfterOthers");
            if (node != null) {
                activateAfterOthers = node.asText();
            }

            node = rootNode.get("activationDelay");
            if (node != null) {
                activationDelay = node.asInt();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return new AutoCompleteProperties(activateAfterLetters, activateAfterOthers, activationDelay);
}

From source file:com.baasbox.util.QueryParams.java

public static QueryParams getParamsFromJson(JsonNode node) {
    if (node == null) {
        return QueryParams.getInstance();
    }//from  ww  w. jav  a  2s .  c o m
    Map<String, String[]> query = new HashMap<String, String[]>();
    Iterator<Map.Entry<String, JsonNode>> nodes = node.fields();
    while (nodes.hasNext()) {
        Map.Entry<String, JsonNode> next = nodes.next();
        String k = next.getKey();
        JsonNode val = next.getValue();
        if (val.isArray()) {
            String[] ary = new String[val.size()];
            int idx = 0;
            for (JsonNode n : val) {
                String s = n == null ? null : n.asText();
                ary[idx++] = s;
            }
            query.put(k, ary);
        } else {
            String[] o = { val.asText() };
            query.put(k, o);
        }
    }
    return getParamsFromQueryString(query);
}

From source file:org.apache.drill.optiq.EnumerableDrill.java

/** Converts a JSON node to Java objects ({@link List}, {@link Map},
 * {@link String}, {@link Integer}, {@link Double}, {@link Boolean}. */
static Object wrapper(JsonNode node) {
    switch (node.asToken()) {
    case START_OBJECT:
        return map((ObjectNode) node);
    case START_ARRAY:
        return array((ArrayNode) node);
    case VALUE_STRING:
        return node.asText();
    case VALUE_NUMBER_INT:
        return node.asInt();
    case VALUE_NUMBER_FLOAT:
        return node.asDouble();
    case VALUE_TRUE:
        return Boolean.TRUE;
    case VALUE_FALSE:
        return Boolean.FALSE;
    case VALUE_NULL:
        return null;
    default:/*  w ww .ja va 2 s  . c om*/
        throw new AssertionError("unexpected: " + node + ": " + node.asToken());
    }
}

From source file:com.ikanow.aleph2.security.utils.LdifExportUtil.java

/**
 * Returns the sourceIds and the bucket IDs associated with the community.
 * @param communityId//from  w  ww. ja v a  2s.c o  m
 * @return
 * @throws ExecutionException 
 * @throws InterruptedException 
protected Tuple2<Set<String>, Set<String>> loadSourcesAndBucketIdsByCommunityId(String communityId) throws InterruptedException, ExecutionException {
   Set<String> sourceIds = new HashSet<String>();
   Set<String> bucketIds = new HashSet<String>();      
   ObjectId objecId = new ObjectId(communityId); 
   Cursor<JsonNode> cursor = getSourceDb().getObjectsBySpec(CrudUtils.anyOf().when("communityIds", objecId)).get();
        
         for (Iterator<JsonNode> it = cursor.iterator(); it.hasNext();) {
            JsonNode source = it.next();
            String sourceId = source.get("_id").asText();   
            sourceIds.add(sourceId);
            JsonNode extracType = source.get("extractType");
            if(extracType!=null && "V2DataBucket".equalsIgnoreCase(extracType.asText())){
               JsonNode bucketId = source.get("key");
               if(bucketId !=null){
                  // TODO HACK , according to Alex, buckets have a semicolon as last id character to facilitate some string conversion 
                  bucketIds.add(bucketId.asText()+";");
               }
            }
            // bucket id
         }
               
   return new Tuple2<Set<String>, Set<String>>(sourceIds,bucketIds);
}
 */

protected static String extractField(JsonNode node, String field) {
    String value = "";
    if (node != null) {
        JsonNode fieldNode = node.get(field);
        if (fieldNode != null) {
            value = fieldNode.asText();
        }
    }
    return value;
}

From source file:de.jlo.talendcomp.json.TypeUtil.java

public static Long convertToLong(JsonNode node) throws Exception {
    if (node.isNumber()) {
        return node.asLong();
    } else if (node.isTextual()) {
        return convertToLong(node.asText());
    } else if (node.isNull()) {
        return null;
    } else {// ww  w  . j a  v a 2  s. com
        throw new Exception("Node: " + node + " cannot be converted to Long");
    }
}

From source file:de.jlo.talendcomp.json.TypeUtil.java

public static Double convertToDouble(JsonNode node) throws Exception {
    if (node.isNumber()) {
        return node.asDouble();
    } else if (node.isTextual()) {
        return convertToDouble(node.asText());
    } else {//w w  w . j  av  a 2 s. c  om
        throw new Exception("Node: " + node + " cannot be converted to Double");
    }
}