Example usage for com.fasterxml.jackson.databind.node LongNode LongNode

List of usage examples for com.fasterxml.jackson.databind.node LongNode LongNode

Introduction

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

Prototype

public LongNode(long paramLong) 

Source Link

Usage

From source file:org.jsonbuilder.implementations.jackson.JacksonNumberNode.java

public JacksonNumberNode(Number number) {
    value = new LongNode((Integer) number);
}

From source file:com.redhat.lightblue.query.ValueTest.java

/**
 * Test of toJson method, of class Value.
 *///from  w  ww .j av a  2s.co m
@Test
public void testToJson() throws IOException {
    // if in int range returns IntNode
    Value instance = new Value(10L);
    JsonNode expResult = new IntNode(10);
    JsonNode result = instance.toJson();
    assertEquals(expResult, result);

    instance = new Value(Long.MAX_VALUE);
    expResult = new LongNode(Long.MAX_VALUE);
    result = instance.toJson();
    assertEquals(expResult, result);

    instance = new Value(10.0D);
    expResult = new DoubleNode(10.0D);
    result = instance.toJson();
    assertEquals(expResult, result);

    instance = new Value(3.0F);
    expResult = new FloatNode(3.0F);
    result = instance.toJson();
    assertEquals(expResult, result);
}

From source file:es.bsc.amon.util.tree.TreeNodeFactory.java

public static JsonNode toJson(TreeNode n) {
    JsonNode json = null;//from   ww  w. jav  a 2  s. c om
    if (n instanceof ObjNode) {
        json = new ObjectNode(JsonNodeFactory.instance);
        for (Map.Entry<String, TreeNode> e : ((ObjNode) n).properties.entrySet()) {
            ((ObjectNode) json).put(e.getKey(), toJson(e.getValue()));
        }
    } else if (n instanceof NodeArray) {
        json = new ArrayNode(JsonNodeFactory.instance);
        for (TreeNode ne : ((NodeArray) n).elements) {
            ((ArrayNode) json).add(toJson(ne));
        }
    } else if (n instanceof StringValue) {
        json = new TextNode(((StringValue) n).value);
    } else if (n instanceof NumberValue) {
        Number val = ((NumberValue) n).value;
        if (val instanceof Byte || val instanceof Short || val instanceof Integer || val instanceof Long) {
            json = new LongNode(val.longValue());
        } else {
            json = new DoubleNode(val.doubleValue());
        }
    } else
        throw new RuntimeException("You should not reach this");
    return json;
}

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

private JsonNode json(long value) {
    return new LongNode(value);
}

From source file:com.redhat.lightblue.query.ValueTest.java

/**
 * Test of fromJson method, of class Value.
 *///from  w ww  . ja v  a  2  s  .  c  o m
@Test
public void testFromJson() throws IOException {
    Value expResult = new Value(10L);
    JsonNode longNode = new LongNode(10);
    Value result = Value.fromJson(longNode);
    assertEquals(expResult, result);

    boolean error = false;
    try {
        Value.fromJson(JsonUtils.json("[\"test\"]"));
    } catch (Error e) {
        error = true;
        String exString = Error.get(QueryConstants.ERR_INVALID_VALUE, "[\"test\"]").toString();
        assertEquals(exString, e.toString());
    }
    if (!error) {
        fail("Expection was not thrown");
    }
}

From source file:com.aol.one.patch.DefaultPatcherTest.java

@Test
public void testReplaceForSuccess() throws PatchException {

    List<PatchOperation> operations = new ArrayList<>();

    TextNode strNode = new TextNode("1.1String");
    DoubleNode doubleNode = new DoubleNode(10.1d);
    LongNode longNode = new LongNode(200L);
    TextNode childStrNode = new TextNode("2.1String");
    DoubleNode childDoubleNode = new DoubleNode(102.1);

    operations.add(new ReplaceOperation("/doubleField", doubleNode));
    operations.add(new ReplaceOperation("/strField", strNode));
    operations.add(new ReplaceOperation("/longField", longNode));
    operations.add(new ReplaceOperation("/child/strField", childStrNode));
    operations.add(new ReplaceOperation("/child/doubleField", childDoubleNode));

    patcher.patch(testObject, operations);

    assertThat(testObject.getDoubleField(), is(doubleNode.asDouble()));
    assertThat(testObject.getStrField(), is(strNode.asText()));
    assertThat(testObject.getLongField(), is(longNode.longValue()));
    verify(testObject).setStrField(strNode.asText());
    verify(testObject).setDoubleField(doubleNode.doubleValue());
    // replacement of long field via setLongField
    verify(testObject).setLongField(longNode.longValue());

    // child/*from w ww  . ja v  a 2s  . c o  m*/
    assertThat(childTestObject.getDoubleField(), is(childDoubleNode.asDouble()));
    assertThat(childTestObject.getStrField(), is(childStrNode.asText()));
    verify(childTestObject).setStrField(childStrNode.textValue());
    verify(childTestObject).setDoubleField(childDoubleNode.doubleValue());
}

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 ww .j  ava 2s  .  c  o m
 * @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.mapr.synth.samplers.CommonPointOfCompromise.java

@Override
public JsonNode sample() {
    ArrayNode r = nodeFactory.arrayNode();

    double t = start;
    double averageInterval = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS) / transactionsPerDay.nextDouble();
    Exponential interval = new Exponential(1 / averageInterval, gen);

    Date date = new Date();
    boolean compromised = false;
    while (t < end) {
        ObjectNode transaction = new ObjectNode(nodeFactory);
        t += interval.nextDouble();/*  w  ww . ja v a 2 s .co  m*/
        date.setTime((long) t);
        transaction.set("timestamp", new LongNode((long) (t / 1000)));
        transaction.set("date", new TextNode(df.format(date)));
        Integer merchantId = merchant.sample();
        transaction.set("merchant", new IntNode(merchantId));

        if (merchantId == 0 && t >= compromiseStart && t < compromiseEnd) {
            compromised = true;
            transaction.set("compromise", new IntNode(1));
        } else {
            transaction.set("compromise", new IntNode(0));
        }

        if (t > exploitEnd) {
            compromised = false;
        }

        double pFraud;
        if (t >= exploitStart && compromised) {
            pFraud = compromisedFraudRate;
        } else {
            pFraud = uncompromisedFraudRate;
        }

        transaction.set("fraud", new IntNode((gen.nextDouble() < pFraud) ? 1 : 0));

        r.add(transaction);
    }
    return r;
}

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//from w w w  .  j a v a 2s .c  om
 * @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: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  w w w.  j a v a  2 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;
}