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.jboss.aerogear.simplepush.server.netty.standalone.ConfigReader.java

private static SimplePushServerConfig parseSimplePushProperties(final JsonNode json) {
    final JsonNode host = json.get("host");
    final JsonNode port = json.get("port");
    final Builder builder = DefaultSimplePushConfig.create(host.asText(), port.asInt());
    final JsonNode password = json.get("password");
    if (password != null) {
        builder.password(password.asText());
    }//from w ww.  java2 s  .co m
    final JsonNode useragentReaperTimeout = json.get("useragent-reaper-timeout");
    if (useragentReaperTimeout != null) {
        builder.userAgentReaperTimeout(useragentReaperTimeout.asLong());
    }
    final JsonNode endpointHost = json.get("endpoint-host");
    if (endpointHost != null) {
        builder.endpointHost(endpointHost.asText());
    }
    final JsonNode endpointPort = json.get("endpoint-port");
    if (endpointPort != null) {
        builder.endpointPort(endpointPort.asInt());
    }
    final JsonNode endpointTls = json.get("endpoint-tls");
    if (endpointTls != null) {
        builder.endpointTls(endpointTls.asBoolean());
    }
    final JsonNode endpointPrefix = json.get("endpoint-prefix");
    if (endpointPrefix != null) {
        builder.endpointPrefix(endpointPrefix.asText());
    }
    final JsonNode ackInterval = json.get("ack-interval");
    if (ackInterval != null) {
        builder.ackInterval(ackInterval.asLong());
    }
    final JsonNode notifierMaxThreads = json.get("notifier-max-threads");
    if (notifierMaxThreads != null) {
        builder.notifierMaxThreads(notifierMaxThreads.asInt());
    }
    return builder.build();
}

From source file:com.redhat.lightblue.util.Error.java

public static Error fromJson(JsonNode node) {
    if (node instanceof ObjectNode) {
        String e = null;//from   w ww . j a  va2s.  c  o m
        String m = null;

        JsonNode x;

        x = ((ObjectNode) node).get("errorCode");
        if (x != null) {
            e = x.asText();
        }
        x = ((ObjectNode) node).get("msg");
        if (x != null) {
            m = x.asText();
        }

        Error ret = new Error(e, m);

        x = ((ObjectNode) node).get("context");
        if (x != null) {
            StringTokenizer tok = new StringTokenizer(x.asText(), "/");
            while (tok.hasMoreTokens()) {
                ret.pushContext(tok.nextToken());
            }
        }

        return ret;
    }
    return null;
}

From source file:org.jboss.aerogear.io.netty.handler.codec.sockjs.util.JsonUtil.java

public static String[] decode(final String content) throws IOException {
    final JsonNode root = MAPPER.readTree(content);
    if (root.isObject()) {
        return new String[] { root.toString() };
    }//from   w ww .ja v  a2 s. c o  m

    if (root.isValueNode()) {
        return new String[] { root.asText() };
    }

    if (!root.isArray()) {
        throw new JsonMappingException("content must be a JSON Array but was : " + content);
    }
    final List<String> messages = new ArrayList<String>();
    final Iterator<JsonNode> elements = root.elements();
    while (elements.hasNext()) {
        final JsonNode field = elements.next();
        if (field.isValueNode()) {
            messages.add(field.asText());
        } else {
            messages.add(field.toString());
        }
    }
    return messages.toArray(new String[messages.size()]);
}

From source file:nl.esciencecenter.xnatclient.data.XnatParser.java

/**
 * Expect a single XnatObject at JsonNode
 * /*from   w w w.j av a2s  .  co  m*/
 * @return
 */
public static XnatObject parseXnatObject(XnatObjectType type, JsonNode node) {
    Iterator<String> names = node.fieldNames();
    // create new object
    XnatObject obj = XnatObject.create(type);
    logger.debugPrintf(" - New XnatObject:<%s>\n", obj.getObjectType());

    // use headers from XnatObject definition (not json):
    StringList headerList = new StringList(obj.getFieldNames());
    for (int i = 0; i < headerList.size(); i++) {
        String name = headerList.get(i);
        JsonNode fieldObj = node.get(name);
        if (fieldObj != null) {
            String text = fieldObj.asText();
            logger.debugPrintf(" - - set %s='%s'\n", name, text);
            obj.set(name, text);
        }
    }

    return obj;
}

From source file:com.baasbox.controllers.ScriptsAdmin.java

private static void validateBody(JsonNode body) throws ScriptException {
    if (body == null) {
        throw new ScriptException("Missing body");
    }//from w w w.  ja  v a2 s. c o m
    JsonNode name = body.get(ScriptsDao.NAME);
    if (name == null || (!name.isTextual()) || name.asText().trim().length() == 0) {
        throw new ScriptException("Missing required 'name' property");
    }
    JsonNode code = body.get(ScriptsDao.CODE);
    if (code == null || (!code.isTextual()) || code.asText().trim().length() == 0) {
        throw new ScriptException("Missing required 'code' property");
    }
    JsonNode lang = body.get(ScriptsDao.LANG);
    if (lang == null || (!lang.isTextual()) || lang.asText().trim().length() == 0) {
        throw new ScriptException("Missing required 'lang' property or it is not a string");
    }
    if (ScriptLanguage.forName(lang.asText()) == null) {
        throw new ScriptException("Language '" + lang.asText() + "' is not supported");
    }
    JsonNode encoded = body.get(ScriptsDao.ENCODED);
    if (encoded != null) {
        if (!encoded.isTextual()) {
            throw new ScriptException("The field 'encoded' must be a String");
        }
        try {
            ScriptsDao.ENCODED_TYPE.valueOf(encoded.asText().toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new ScriptException("The specified 'encoded' value is not valid");
        }
    }
}

From source file:com.bbva.kltt.apirest.core.util.parser.ParserUtil.java

/**
 * Generate a Set of String from String// w w w  . ja  va2s  .com
 *
 * @param arrayNode with the JsonNode as Array
 * @return a set of strings
 */
public static Set<String> generateSetStringFromString(final ArrayNode arrayNode) {
    final Set<String> outcome = new HashSet<String>();

    final Iterator<JsonNode> iterator = arrayNode.elements();
    while (iterator.hasNext()) {
        final JsonNode jsonNode = iterator.next();
        outcome.add(jsonNode.asText());
    }

    return outcome;
}

From source file:de.thingweb.typesystem.jsonschema.JsonSchemaType.java

public static JsonType getJsonType(JsonNode jsonSchemaNode) throws JsonSchemaException {
    JsonType jtype = null;//w  w  w. j  av a2 s  .  co m

    if (jsonSchemaNode != null) {
        JsonNode typeNode = jsonSchemaNode.findValue("type");
        if (typeNode != null) {
            switch (typeNode.asText()) {
            case "boolean":
                jtype = new JsonBoolean();
                break;
            case "integer":
            case "number":
                boolean exclusiveMinimum = false;
                JsonNode exclusiveMinimumNode = jsonSchemaNode.findValue("exclusiveMinimum");
                if (exclusiveMinimumNode != null
                        && exclusiveMinimumNode.getNodeType() == JsonNodeType.BOOLEAN) {
                    exclusiveMinimum = exclusiveMinimumNode.asBoolean();
                }
                boolean exclusiveMaximum = false;
                JsonNode exclusiveMaximumNode = jsonSchemaNode.findValue("exclusiveMaximum");
                if (exclusiveMaximumNode != null
                        && exclusiveMaximumNode.getNodeType() == JsonNodeType.BOOLEAN) {
                    exclusiveMaximum = exclusiveMaximumNode.asBoolean();
                }

                if ("integer".equals(typeNode.asText())) {
                    jtype = new JsonInteger();
                    JsonNode minimumNode = jsonSchemaNode.findValue("minimum");
                    if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonInteger) jtype).setMinimum(minimumNode.asLong());
                    }
                    JsonNode maximumNode = jsonSchemaNode.findValue("maximum");
                    if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonInteger) jtype).setMaximum(maximumNode.asLong());
                    }
                } else {
                    assert ("number".equals(typeNode.asText()));

                    jtype = new JsonNumber();
                    JsonNode minimumNode = jsonSchemaNode.findValue("minimum");
                    if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonNumber) jtype).setMinimum(minimumNode.asDouble());
                    }
                    JsonNode maximumNode = jsonSchemaNode.findValue("maximum");
                    if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonNumber) jtype).setMaximum(maximumNode.asDouble());
                    }
                }

                ((AbstractJsonNumeric) jtype).setExclusiveMinimum(exclusiveMinimum);
                ((AbstractJsonNumeric) jtype).setExclusiveMaximum(exclusiveMaximum);

                break;
            case "null":
                jtype = new JsonNull();
                break;
            case "string":
                jtype = new JsonString();
                break;
            case "array":
                JsonNode itemsNode = jsonSchemaNode.findValue("items");
                if (itemsNode != null && JsonNodeType.OBJECT == itemsNode.getNodeType()) {
                    jtype = new JsonArray(getJsonType(itemsNode));
                } else {
                    throw new JsonSchemaException("items not object");
                }

                break;
            case "object":
                JsonNode propertiesNode = jsonSchemaNode.findValue("properties");
                if (propertiesNode != null && JsonNodeType.OBJECT == propertiesNode.getNodeType()) {
                    Iterator<Entry<String, JsonNode>> iter = propertiesNode.fields();
                    Map<String, JsonType> properties = new HashMap<String, JsonType>();
                    while (iter.hasNext()) {
                        Entry<String, JsonNode> e = iter.next();
                        JsonNode nodeProp = e.getValue();
                        properties.put(e.getKey(), getJsonType(nodeProp));
                    }
                    jtype = new JsonObject(properties);
                } else {
                    throw new JsonSchemaException("Properties not object");
                }
                // required
                JsonNode requiredNode = jsonSchemaNode.findValue("required");
                if (requiredNode != null && JsonNodeType.ARRAY == requiredNode.getNodeType()) {
                    ArrayNode an = (ArrayNode) requiredNode;
                    Iterator<JsonNode> iterReq = an.elements();
                    while (iterReq.hasNext()) {
                        JsonNode nreq = iterReq.next();
                        if (JsonNodeType.STRING == nreq.getNodeType()) {
                            ((JsonObject) jtype).addRequired(nreq.asText());
                        } else {
                            throw new JsonSchemaException("Unexpected required node: " + nreq);
                        }
                    }
                }
                break;
            }
        }
    }

    return jtype;
}

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

public static BigDecimal convertToBigDecimal(JsonNode node) throws Exception {
    if (node.isNumber()) {
        return new BigDecimal(node.asText());
    } else if (node.isTextual()) {
        return convertToBigDecimal(node.asText());
    } else if (node.isNull()) {
        return null;
    } else {//from w  w  w .  ja v a 2 s  . c  o m
        throw new Exception("Node: " + node + " cannot be converted to BigDecimal");
    }
}

From source file:com.squarespace.template.JsonUtils.java

/**
 * Compare two JsonNode objects and return an integer.
 *
 * @return  a negative integer, zero, or a positive integer as this object
 *          is less than, equal to, or greater than the specified object.
 *//* w  w  w  . j a v  a 2s.c o m*/
public static int compare(JsonNode left, JsonNode right) {
    if (left.isLong() || left.isInt()) {
        return Long.compare(left.asLong(), right.asLong());

    } else if (left.isDouble() || left.isFloat()) {
        return Double.compare(left.asDouble(), right.asDouble());

    } else if (left.isTextual()) {
        return left.asText().compareTo(right.asText());

    } else if (left.isBoolean()) {
        return Boolean.compare(left.asBoolean(), right.asBoolean());
    }

    // Not comparable in a relative sense, default to equals.
    return left.equals(right) ? 0 : -1;
}

From source file:org.jboss.aerogear.sync.server.gcm.GcmDiffSyncHandler.java

private static String clientIdFromJson(JsonNode syncMessage) {
    final JsonNode clientId = syncMessage.get("clientId");
    String content = null;/*from   w  ww . ja  v  a 2s.c o  m*/
    if (clientId != null && !clientId.isNull()) {
        content = clientId.asText();

    }
    return content;
}