Example usage for com.fasterxml.jackson.databind.node NullNode instance

List of usage examples for com.fasterxml.jackson.databind.node NullNode instance

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node NullNode instance.

Prototype

NullNode instance

To view the source code for com.fasterxml.jackson.databind.node NullNode instance.

Click Source Link

Usage

From source file:com.turn.shapeshifter.AutoSerializer.java

/**
 * Returns the JSON representation of the value of a message's field.
 *
 * @param value the value to represent in JSON
 * @param field the descriptor of the value's field.
 * @param schemas a container for object schemas to use for formatting
 * fields that refer to other messages//from   w  w w.j a  va  2s.com
 * @throws SerializationException
 */
private JsonNode serializeValue(Object value, FieldDescriptor field, ReadableSchemaRegistry registry)
        throws SerializationException {
    JsonNode valueNode = NullNode.instance;
    switch (field.getType()) {
    case BOOL:
        valueNode = BooleanNode.valueOf((Boolean) value);
        break;
    case BYTES:
        break;
    case DOUBLE:
        valueNode = new DoubleNode((Double) value);
        break;
    case ENUM:
        EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) value;
        String enumValue = enumValueDescriptor.getName();
        String convertedValue = AutoSchema.PROTO_ENUM_CASE_FORMAT.to(AutoSchema.JSON_ENUM_CASE_FORMAT,
                enumValue);
        valueNode = new TextNode(convertedValue);
        break;
    case FLOAT:
        valueNode = new DoubleNode((Float) value);
        break;
    case GROUP:
        break;
    case FIXED32:
    case INT32:
    case SFIXED32:
    case SINT32:
    case UINT32:
        valueNode = new IntNode((Integer) value);
        break;
    case FIXED64:
    case INT64:
    case SFIXED64:
    case SINT64:
    case UINT64:
        valueNode = new LongNode((Long) value);
        break;
    case MESSAGE:
        Message messageValue = (Message) value;
        try {
            valueNode = registry.get(messageValue.getDescriptorForType()).getSerializer()
                    .serialize(messageValue, registry);
            valueNode = valueNode.size() == 0 ? NullNode.instance : valueNode;
        } catch (SchemaObtentionException soe) {
            throw new SerializationException(soe);
        }
        break;
    case STRING:
        valueNode = new TextNode((String) value);
        break;
    default:
        break;
    }
    return valueNode;
}

From source file:com.ikanow.aleph2.data_model.utils.TestJsonUtils.java

@Test
public void test_jacksonToJava() {

    assertEquals(Arrays.asList("test"),
            JsonUtils.jacksonToJava(JsonUtils._mapper.convertValue(Arrays.asList("test"), JsonNode.class)));
    assertEquals(new String(new byte[] { (byte) 0xFF, (byte) 0xFE }),
            new String((byte[]) JsonUtils.jacksonToJava(
                    JsonUtils._mapper.convertValue(new byte[] { (byte) 0xFF, (byte) 0xFE }, JsonNode.class))));
    assertEquals(true, JsonUtils.jacksonToJava(JsonUtils._mapper.convertValue(true, JsonNode.class)));
    assertEquals(false, JsonUtils.jacksonToJava(JsonUtils._mapper.convertValue(false, JsonNode.class)));
    assertEquals(1L, JsonUtils.jacksonToJava(JsonUtils._mapper.convertValue(1L, JsonNode.class)));
    assertEquals(2.0, (double) JsonUtils.jacksonToJava(JsonUtils._mapper.convertValue(2.0, JsonNode.class)),
            0.00001);/*from   w ww  .j a v  a  2  s .  c o  m*/
    assertEquals(Collections.emptyMap(), JsonUtils.jacksonToJava(JsonUtils._mapper.createObjectNode()));
    assertEquals(TestBean.class, JsonUtils.jacksonToJava(new POJONode(new TestBean())).getClass());
    assertEquals("test", JsonUtils.jacksonToJava(JsonUtils._mapper.convertValue("test", JsonNode.class)));
    assertEquals(null, JsonUtils.jacksonToJava(NullNode.instance));
}

From source file:com.turn.shapeshifter.SchemaSerializer.java

/**
 * Returns the JSON representation of the value of a message's field.
 *
 * @param value the value to represent in JSON
 * @param field the descriptor of the value's field.
 * @param schemas a container for object schemas to use for formatting fields that refer to
 *                  other messages/*  ww  w . jav a 2s.c o m*/
 * @throws SerializationException
 */
private JsonNode serializeValue(Object value, FieldDescriptor field, ReadableSchemaRegistry schemas)
        throws SerializationException {
    JsonNode valueNode = NullNode.instance;
    if (schema.getTransforms().containsKey(field.getName())) {
        return schema.getTransforms().get(field.getName()).serialize(value);
    }
    switch (field.getType()) {
    case BOOL:
        valueNode = BooleanNode.valueOf((Boolean) value);
        break;
    case BYTES:
        break;
    case DOUBLE:
        valueNode = new DoubleNode((Double) value);
        break;
    case ENUM:
        EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) value;
        String enumValue = NamedSchema.PROTO_ENUM_CASE_FORMAT.to(schema.getEnumCaseFormat(),
                enumValueDescriptor.getName());
        valueNode = new TextNode(enumValue);
        break;
    case FLOAT:
        valueNode = new DoubleNode((Float) value);
        break;
    case GROUP:
        break;
    case FIXED32:
    case INT32:
    case SFIXED32:
    case SINT32:
    case UINT32:
        valueNode = new IntNode((Integer) value);
        break;
    case FIXED64:
    case INT64:
    case SFIXED64:
    case SINT64:
    case UINT64:
        valueNode = new LongNode((Long) value);
        break;
    case MESSAGE:
        Message messageValue = (Message) value;
        Schema subSchema = null;
        if (schema.getSubObjectsSchemas().containsKey(field.getName())) {
            String schemaName = schema.getSubObjectsSchemas().get(field.getName());
            if (schemas.contains(schemaName)) {
                subSchema = schemas.get(schemaName);
            } else {
                throw new IllegalStateException();
            }
        } else {
            try {
                subSchema = schemas.get(field.getMessageType());
            } catch (SchemaObtentionException soe) {
                throw new SerializationException(soe);
            }
        }
        valueNode = subSchema.getSerializer().serialize(messageValue, schemas);
        break;
    case STRING:
        valueNode = new TextNode((String) value);
        break;
    default:
        break;
    }
    return valueNode;
}

From source file:org.wisdom.wamp.WampControllerPubSubTest.java

/**
 * Use the following PUBLISH message format: [TYPE, URI, Event]
 * @throws RegistryException/*w  w  w .  j a va  2 s . c  o  m*/
 */
@Test
public void testPublicationAndReception() throws RegistryException {
    clear();
    Json json = new JsonService();
    WampController controller = createWampControllerAndConnectClient(json);

    controller.open("id2");
    clear();

    // client 1 subscribes to a topic
    clear();
    ArrayNode msg = json.newArray();
    msg.add(MessageType.SUBSCRIBE.code());
    msg.add("http://example.com:9001/wamp/topic");
    controller.onMessage(CLIENT_ID, msg);

    // client 2 send an event on the topic, client 2 is not a subscriber of the topic

    // Simple payload
    clear();
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add("hello");
    controller.onMessage("id2", msg);

    assertThat(message).isNotNull();
    JsonNode node = json.parse(last());
    assertThat(node.isArray());
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).asText()).isEqualTo("hello");

    // Null payload
    clear();
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    msg.add(NullNode.instance);
    controller.onMessage("id2", msg);

    assertThat(message).isNotNull();
    node = json.parse(last());
    assertThat(node.isArray());
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(node.get(2).isNull()).isTrue();

    // Complex payload
    clear();
    msg = json.newArray();
    msg.add(MessageType.PUBLISH.code());
    msg.add("http://example.com:9001/wamp/topic");
    Struct reference = new Struct("hi", 1000, true, new String[] { "a", "b" });
    msg.add(json.toJson(reference));
    controller.onMessage("id2", msg);

    assertThat(message).isNotNull();
    node = json.parse(last());
    assertThat(node.isArray());
    assertThat(node.get(0).asInt()).isEqualTo(MessageType.EVENT.code());
    assertThat(node.get(1).asText()).isEqualTo("http://example.com:9001/wamp/topic");
    assertThat(json.fromJson(node.get(2), Struct.class)).isEqualTo(reference);
}

From source file:com.turn.shapeshifter.NamedSchemaSerializer.java

/**
 * Returns the JSON representation of the value of a message's field.
 *
 * @param value the value to represent in JSON
 * @param field the descriptor of the value's field.
 * @param schemas a container for object schemas to use for formatting
 * fields that refer to other messages//from ww  w.ja v a2 s .c o m
 * @throws SerializationException
 */
private JsonNode serializeValue(Object value, FieldDescriptor field, ReadableSchemaRegistry schemas)
        throws SerializationException {
    JsonNode valueNode = NullNode.instance;
    if (schema.getTransforms().containsKey(field.getName())) {
        return schema.getTransforms().get(field.getName()).serialize(value);
    }
    switch (field.getType()) {
    case BOOL:
        valueNode = BooleanNode.valueOf((Boolean) value);
        break;
    case BYTES:
        byte[] bytes = ((ByteString) value).toByteArray();
        String content = new String();
        for (int i = 0; i < bytes.length; i++) {
            content += (char) bytes[i];
        }
        valueNode = TextNode.valueOf(content);
        break;
    case DOUBLE:
        valueNode = new DoubleNode((Double) value);
        break;
    case ENUM:
        EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) value;
        String enumValue = NamedSchema.PROTO_ENUM_CASE_FORMAT.to(schema.getEnumCaseFormat(),
                enumValueDescriptor.getName());
        valueNode = new TextNode(enumValue);
        break;
    case FLOAT:
        valueNode = new DoubleNode((Float) value);
        break;
    case GROUP:
        break;
    case FIXED32:
    case INT32:
    case SFIXED32:
    case SINT32:
    case UINT32:
        valueNode = new IntNode((Integer) value);
        break;
    case FIXED64:
    case INT64:
    case SFIXED64:
    case SINT64:
    case UINT64:
        valueNode = schema.getSurfaceLongsAsStrings() ? new TextNode(((Long) value).toString())
                : new LongNode((Long) value);
        break;
    case MESSAGE:
        Message messageValue = (Message) value;
        Schema subSchema = null;
        if (schema.getSubObjectsSchemas().containsKey(field.getName())) {
            String schemaName = schema.getSubObjectsSchemas().get(field.getName());
            if (schemas.contains(schemaName)) {
                subSchema = schemas.get(schemaName);
            } else {
                throw new IllegalStateException();
            }
        } else {
            try {
                subSchema = schemas.get(field.getMessageType());
            } catch (SchemaObtentionException soe) {
                throw new SerializationException(soe);
            }
        }
        valueNode = subSchema.getSerializer().serialize(messageValue, schemas);
        valueNode = valueNode.size() == 0 ? NullNode.instance : valueNode;
        break;
    case STRING:
        valueNode = new TextNode((String) value);
        break;
    default:
        break;
    }
    return valueNode;
}

From source file:com.palominolabs.crm.sf.rest.RestConnectionImpl.java

private static boolean isNull(JsonNode node) {
    return node == null || node == NullNode.instance;
}

From source file:org.apache.usergrid.java.client.model.UsergridEntity.java

@JsonAnySetter
private void internalPutProperty(@NotNull final String name, @Nullable final JsonNode value) {
    if (value == null) {
        properties.put(name, NullNode.instance);
    } else {//from w w w  .  ja  v  a 2  s .c  o m
        properties.put(name, value);
    }
}