Java Examples for com.fasterxml.jackson.databind.node.ValueNode

The following java examples will help you to understand the usage of com.fasterxml.jackson.databind.node.ValueNode. These source code samples are taken from different open source projects.

Example 1
Project: intercom-java-master  File: CustomAttributeDeserializer.java View source code
@Override
public CustomAttribute deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    CustomAttribute cda = null;
    final String currentName = jp.getParsingContext().getCurrentName();
    final ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    final ValueNode vNode = mapper.readTree(jp);
    if (vNode.asToken().isScalarValue()) {
        if (vNode.getNodeType() == JsonNodeType.BOOLEAN) {
            cda = new CustomAttribute<Boolean>(currentName, vNode.asBoolean(), Boolean.class);
        } else if (vNode.getNodeType() == JsonNodeType.STRING) {
            cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
        } else if (vNode.getNodeType() == JsonNodeType.NUMBER) {
            final NumericNode nNode = (NumericNode) vNode;
            if (currentName.endsWith("_at")) {
                cda = new CustomAttribute<Long>(currentName, vNode.longValue(), Long.class);
            } else if (nNode.isInt()) {
                cda = new CustomAttribute<Integer>(currentName, vNode.intValue(), Integer.class);
            } else if (nNode.isFloat()) {
                cda = new CustomAttribute<Float>(currentName, vNode.floatValue(), Float.class);
            } else if (nNode.isDouble()) {
                cda = new CustomAttribute<Double>(currentName, vNode.doubleValue(), Double.class);
            } else if (nNode.isLong()) {
                cda = new CustomAttribute<Long>(currentName, vNode.longValue(), Long.class);
            } else {
                cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
            }
        } else {
            cda = new CustomAttribute<String>(currentName, vNode.asText(), String.class);
        }
    }
    return cda;
}
Example 2
Project: stormpath-sdk-java-master  File: JSONPropertiesSource.java View source code
private void getFlattenedMap(String currentPath, JsonNode jsonNode, Map<String, String> map) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";
        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            getFlattenedMap(pathPrefix + entry.getKey(), entry.getValue(), map);
        }
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;
        for (int i = 0; i < arrayNode.size(); i++) {
            getFlattenedMap(currentPath + "[" + i + "]", arrayNode.get(i), map);
        }
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        map.put(currentPath, valueNode.asText());
    }
}
Example 3
Project: bson4jackson-master  File: BsonJavaScriptDeserializer.java View source code
@Override
@SuppressWarnings("deprecation")
public JavaScript deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp instanceof BsonParser) {
        BsonParser bsonParser = (BsonParser) jp;
        if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT || (bsonParser.getCurrentBsonType() != BsonConstants.TYPE_JAVASCRIPT && bsonParser.getCurrentBsonType() != BsonConstants.TYPE_JAVASCRIPT_WITH_SCOPE)) {
            throw ctxt.mappingException(JavaScript.class);
        }
        return (JavaScript) bsonParser.getEmbeddedObject();
    } else {
        TreeNode tree = jp.getCodec().readTree(jp);
        String code = null;
        TreeNode codeNode = tree.get("$code");
        if (codeNode instanceof ValueNode) {
            code = ((ValueNode) codeNode).asText();
        }
        Map<String, Object> scope = null;
        TreeNode scopeNode = tree.get("$scope");
        if (scopeNode instanceof ObjectNode) {
            @SuppressWarnings("unchecked") Map<String, Object> scope2 = jp.getCodec().treeToValue(scopeNode, Map.class);
            scope = scope2;
        }
        return new JavaScript(code, scope);
    }
}
Example 4
Project: json-data-generator-master  File: JsonUtils.java View source code
public void flattenJsonIntoMap(String currentPath, JsonNode jsonNode, Map<String, Object> map) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";
        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            flattenJsonIntoMap(pathPrefix + entry.getKey(), entry.getValue(), map);
        }
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;
        for (int i = 0; i < arrayNode.size(); i++) {
            flattenJsonIntoMap(currentPath + "[" + i + "]", arrayNode.get(i), map);
        }
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        Object value = null;
        if (valueNode.isNumber()) {
            value = valueNode.numberValue();
        } else if (valueNode.isBoolean()) {
            value = valueNode.asBoolean();
        } else if (valueNode.isTextual()) {
            value = valueNode.asText();
        }
        map.put(currentPath, value);
    }
}
Example 5
Project: monasca-common-master  File: Configurations.java View source code
private static void buildConfigFor(String path, Map<String, String> config, JsonNode node) {
    for (Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext(); ) {
        Map.Entry<String, JsonNode> field = i.next();
        if (field.getValue() instanceof ValueNode) {
            ValueNode valueNode = (ValueNode) field.getValue();
            config.put(DOT_JOINER.join(path, field.getKey()), valueNode.asText());
        } else if (field.getValue() instanceof ArrayNode) {
            StringBuilder combinedValue = new StringBuilder();
            ArrayNode arrayNode = (ArrayNode) field.getValue();
            for (Iterator<JsonNode> it = arrayNode.elements(); it.hasNext(); ) {
                String value = it.next().asText().replaceAll("^\"|\"$", "");
                if (combinedValue.length() > 0)
                    combinedValue.append(',');
                combinedValue.append(value);
            }
            config.put(DOT_JOINER.join(path, field.getKey()), combinedValue.toString());
        }
        buildConfigFor(DOT_JOINER.join(path, field.getKey()), config, field.getValue());
    }
}
Example 6
Project: succinct-master  File: JsonBlockSerializer.java View source code
private void flattenJsonTree(String currentPath, JsonNode jsonNode, ByteArrayOutputStream out) throws SerializationException {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";
        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            flattenJsonTree(pathPrefix + entry.getKey(), entry.getValue(), out);
        }
    } else if (jsonNode.isArray()) {
        throw new SerializationException("Arrays in JSON are not supported yet.");
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        if (!fieldMapping.containsField(currentPath)) {
            fieldMapping.put(currentPath, delimiters[currentDelimiterIdx++], getNodeType(jsonNode));
        } else {
            DataType existingType = fieldMapping.getDataType(currentPath);
            DataType newType = getNodeType(valueNode);
            if (existingType != newType) {
                DataType encapsulatingType = DataType.encapsulatingType(existingType, newType);
                fieldMapping.updateType(currentPath, encapsulatingType);
            }
        }
        try {
            byte fieldByte = fieldMapping.getDelimiter(currentPath);
            out.write(fieldByte);
            out.write(valueNode.asText().getBytes());
            out.write(fieldByte);
        } catch (IOException e) {
            throw new SerializationException(e.getMessage());
        }
    }
}
Example 7
Project: maxwell-master  File: RowMapDeserializer.java View source code
@Override
public RowMap deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    JsonNode type = node.get("type");
    if (type == null) {
        throw new ParseException("`type` is required and cannot be null.");
    }
    JsonNode database = node.get("database");
    if (database == null) {
        throw new ParseException("`database` is required and cannot be null.");
    }
    JsonNode table = node.get("table");
    if (table == null) {
        throw new ParseException("`table` is required and cannot be null.");
    }
    JsonNode ts = node.get("ts");
    if (ts == null) {
        throw new ParseException("`ts` is required and cannot be null.");
    }
    JsonNode xid = node.get("xid");
    JsonNode data = node.get("data");
    JsonNode oldData = node.get("old");
    RowMap rowMap = new RowMap(type.asText(), database.asText(), table.asText(), ts.asLong(), new ArrayList<String>(), null);
    if (xid != null) {
        rowMap.setXid(xid.asLong());
    }
    if (data instanceof ObjectNode) {
        Iterator keys = data.fieldNames();
        if (keys != null) {
            while (keys.hasNext()) {
                String key = (String) keys.next();
                JsonNode value = data.get(key);
                if (value.isValueNode()) {
                    ValueNode valueNode = (ValueNode) value;
                    rowMap.putData(key, getValue(valueNode));
                }
            }
        }
    } else {
        throw new ParseException("`data` is required and cannot be null.");
    }
    if (oldData instanceof ObjectNode) {
        Iterator keys = oldData.fieldNames();
        if (keys != null) {
            while (keys.hasNext()) {
                String key = (String) keys.next();
                JsonNode value = oldData.get(key);
                if (value.isValueNode()) {
                    ValueNode valueNode = (ValueNode) value;
                    rowMap.putOldData(key, getValue(valueNode));
                }
            }
        }
    }
    return rowMap;
}
Example 8
Project: lightblue-core-master  File: JsonUtils.java View source code
/**
     * Returns a Java object for a json value node based on the node type.
     */
public static Object valueFromJson(ValueNode node) {
    if (node instanceof NullNode) {
        return null;
    } else {
        if (node instanceof TextNode) {
            return node.textValue();
        } else if (node instanceof BooleanNode) {
            return node.booleanValue();
        } else if (node instanceof NumericNode) {
            return node.numberValue();
        } else {
            throw new RuntimeException("Unsupported node type:" + node.getClass().getName());
        }
    }
}
Example 9
Project: heroic-master  File: DurationSerialization.java View source code
private Duration deserializeObject(TreeNode tree, DeserializationContext c) throws JsonMappingException {
    if (tree == null) {
        throw c.mappingException("expected object");
    }
    TreeNode node;
    ValueNode valueNode;
    final long duration;
    final TimeUnit unit;
    if ((node = tree.get("duration")) != null && node.isValueNode() && (valueNode = (ValueNode) node).isNumber()) {
        duration = valueNode.asLong();
    } else {
        throw c.mappingException("duration is not a numeric field");
    }
    if ((node = tree.get("unit")) != null && node.isValueNode() && (valueNode = (ValueNode) node).isTextual()) {
        unit = TimeUnit.valueOf(valueNode.asText().toUpperCase());
    } else {
        unit = Duration.DEFAULT_UNIT;
    }
    return new Duration(duration, unit);
}
Example 10
Project: action-core-master  File: TestSmileRowSerializer.java View source code
@Test(groups = "fast")
public void testToRowsOrderingWithoutRegistrar() throws Exception {
    final Map<String, Object> eventMap = createEventMap();
    final ByteArrayOutputStream out = createSmileEnvelopePayload(eventMap);
    final InputStream stream = new ByteArrayInputStream(out.toByteArray());
    final SmileRowSerializer serializer = new SmileRowSerializer();
    final Rows rows = serializer.toRows(new NullRegistrar(), stream);
    final RowSmile firstRow = (RowSmile) rows.iterator().next();
    final ImmutableMap<String, ValueNode> actual = firstRow.toMap();
    // Without the registrar, verify we output all fields, including the metadata ones (eventDate, eventGranularity)
    Assert.assertEquals(actual.keySet().size(), eventMap.keySet().size() + 2);
    Assert.assertNotNull(actual.get("eventDate"));
    Assert.assertEquals(actual.get("eventGranularity"), "HOURLY");
    Assert.assertEquals(actual.get("field1"), eventMap.get("field1"));
    Assert.assertEquals(actual.get("field2"), eventMap.get("field2"));
}
Example 11
Project: incubator-streams-master  File: PropertyUtil.java View source code
private static void addKeys(String currentPath, JsonNode jsonNode, Map<String, Object> map, char seperator) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + seperator;
        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            addKeys(pathPrefix + entry.getKey(), entry.getValue(), map, seperator);
        }
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;
        if (arrayNode.isTextual()) {
            List<String> list = mapper.convertValue(arrayNode, List.class);
            map.put(currentPath, list);
        }
        if (arrayNode.isNumber()) {
            List<String> list = mapper.convertValue(arrayNode, List.class);
            map.put(currentPath, list);
        }
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        if (valueNode.isTextual())
            map.put(currentPath, valueNode.asText());
        else if (valueNode.isNumber())
            map.put(currentPath, valueNode);
    }
}
Example 12
Project: jongo-master  File: BsonDeserializers.java View source code
@Override
public MinKey deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    TreeNode tree = jp.getCodec().readTree(jp);
    if (tree.isObject()) {
        int value = ((ValueNode) tree.get("$minKey")).asInt();
        if (value == 1) {
            return new MinKey();
        }
        throw ctxt.mappingException(MinKey.class);
    } else if (tree instanceof POJONode) {
        return (MinKey) ((POJONode) tree).getPojo();
    } else if (tree instanceof TextNode) {
        return new MinKey();
    } else {
        throw ctxt.mappingException(MinKey.class);
    }
}
Example 13
Project: lightblue-migrator-master  File: JsonDiff.java View source code
private long computeHash(JsonNode node, List<String> context) {
    long hash = 0;
    if (filter.includeField(context)) {
        if (node instanceof ValueNode) {
            return node.hashCode();
        } else if (node == null) {
            hash = 0;
        } else if (node instanceof NullNode) {
            hash = 1;
        } else if (node instanceof ObjectNode) {
            hash = 0;
            int ctxn = context.size();
            context.add("");
            for (Iterator<Map.Entry<String, JsonNode>> itr = node.fields(); itr.hasNext(); ) {
                Map.Entry<String, JsonNode> entry = itr.next();
                context.set(ctxn, entry.getKey());
                if (filter.includeField(context)) {
                    hash += entry.getKey().hashCode();
                    hash += computeHash(entry.getValue(), context);
                }
            }
            context.remove(ctxn);
        } else if (node instanceof ArrayNode) {
            hash = 0;
            int i = 0;
            int ctxn = context.size();
            context.add("");
            for (Iterator<JsonNode> itr = node.elements(); itr.hasNext(); i++) {
                context.set(ctxn, Integer.toString(i));
                hash += computeHash(itr.next(), context);
            }
            context.remove(ctxn);
        } else {
            hash = node.hashCode();
        }
    }
    return hash;
}
Example 14
Project: xmlsh-master  File: JSONUtils.java View source code
public static Object asJavaNative(JsonNode node) {
    if (node.isValueNode()) {
        ValueNode value = (ValueNode) node;
        if (value.isNumber())
            return ((NumericNode) value).numberValue();
        if (value.isBoolean())
            return value.asBoolean();
        if (value.isTextual())
            return value.asText().toString();
        if (value.isNull())
            return null;
    }
    if (node.isArray()) {
        ArrayNode a = (ArrayNode) node;
        ArrayList<Object> al = new ArrayList<Object>(a.size());
        for (JsonNode an : a) {
            al.add(asJavaNative(an));
        }
        return al;
    }
    ObjectMapper mapper = getJsonObjectMapper();
    if (node.isObject()) {
        return mapper.convertValue(node, Map.class);
    }
    // WTF
    return node.toString();
}
Example 15
Project: hamcrest-jackson-master  File: IsJsonIntTest.java View source code
@Test
public void testMatches() {
    // Given
    ValueNode valueNode = jsonNodeFactory.numberNode(Integer.valueOf(10));
    IsJsonInt matcher = new IsJsonInt(10);
    // When
    boolean matches = matcher.matches(valueNode);
    // Then
    assertTrue(matches);
}