Search in sources :

Example 1 with TextNode

use of org.codehaus.jackson.node.TextNode in project cdap by caskdata.

the class SchemaTest method testAvroRecordSchema.

@Test
public void testAvroRecordSchema() throws Exception {
    org.apache.avro.Schema avroStringSchema = org.apache.avro.Schema.create(org.apache.avro.Schema.Type.STRING);
    org.apache.avro.Schema avroIntSchema = org.apache.avro.Schema.create(org.apache.avro.Schema.Type.INT);
    org.apache.avro.Schema schema = org.apache.avro.Schema.createRecord("UserInfo", "Describes user information", "org.example.schema", false);
    List<org.apache.avro.Schema.Field> fields = new ArrayList<>();
    org.apache.avro.Schema.Field field = new org.apache.avro.Schema.Field("username", avroStringSchema, "Field represents username", new TextNode("unknown"));
    fields.add(field);
    field = new org.apache.avro.Schema.Field("age", avroIntSchema, "Field represents age of user", new IntNode(-1));
    fields.add(field);
    schema.setFields(fields);
    Schema parsedSchema = Schema.parseJson(schema.toString());
    Assert.assertTrue("UserInfo".equals(parsedSchema.getRecordName()));
    Assert.assertEquals(2, parsedSchema.getFields().size());
    Schema.Field parsedField = parsedSchema.getFields().get(0);
    Assert.assertTrue("username".equals(parsedField.getName()));
    Assert.assertTrue("STRING".equals(parsedField.getSchema().getType().toString()));
    parsedField = parsedSchema.getFields().get(1);
    Assert.assertTrue("age".equals(parsedField.getName()));
    Assert.assertTrue("INT".equals(parsedField.getSchema().getType().toString()));
}
Also used : IntNode(org.codehaus.jackson.node.IntNode) Schema(co.cask.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) TextNode(org.codehaus.jackson.node.TextNode) Test(org.junit.Test)

Example 2 with TextNode

use of org.codehaus.jackson.node.TextNode in project neo4j by neo4j.

the class AbstractRESTInteraction method getValue.

private Object getValue(JsonNode valueNode) {
    Object value;
    if (valueNode instanceof TextNode) {
        value = valueNode.asText();
    } else if (valueNode instanceof ObjectNode) {
        value = mapValue(valueNode.getFieldNames(), valueNode);
    } else if (valueNode instanceof ArrayNode) {
        ArrayNode aNode = (ArrayNode) valueNode;
        ArrayList<String> listValue = new ArrayList<>(aNode.size());
        for (int j = 0; j < aNode.size(); j++) {
            listValue.add(aNode.get(j).asText());
        }
        value = listValue;
    } else if (valueNode instanceof IntNode) {
        value = valueNode.getIntValue();
    } else if (valueNode instanceof LongNode) {
        value = valueNode.getLongValue();
    } else if (valueNode.isNull()) {
        return null;
    } else {
        throw new RuntimeException(String.format("Unhandled REST value type '%s'. Need String (TextNode), List (ArrayNode), Object (ObjectNode), long (LongNode), or int (IntNode).", valueNode.getClass()));
    }
    return value;
}
Also used : IntNode(org.codehaus.jackson.node.IntNode) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) TextNode(org.codehaus.jackson.node.TextNode) ArrayNode(org.codehaus.jackson.node.ArrayNode) Matchers.containsString(org.hamcrest.Matchers.containsString) LongNode(org.codehaus.jackson.node.LongNode)

Aggregations

ArrayList (java.util.ArrayList)2 IntNode (org.codehaus.jackson.node.IntNode)2 TextNode (org.codehaus.jackson.node.TextNode)2 Schema (co.cask.cdap.api.data.schema.Schema)1 ArrayNode (org.codehaus.jackson.node.ArrayNode)1 LongNode (org.codehaus.jackson.node.LongNode)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1