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

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

Introduction

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

Prototype

public static NullNode getInstance() 

Source Link

Usage

From source file:com.ning.metrics.action.hdfs.data.parser.SmileRowSerializer.java

public static void eventToRow(Registrar r, SmileEnvelopeEventDeserializer deserializer, Rows rows) {
    final SmileEnvelopeEvent event;
    try {/*from w w w .  j  av  a2s . c  o m*/
        event = deserializer.getNextEvent();
    } catch (IOException e) {
        throw new RowAccessException(e);
    }
    final JsonNode node = (JsonNode) event.getData();

    final Map<Short, GoodwillSchemaField> schema = r.getSchema(event.getName());
    final List<ColumnKey> columnKeyList = new ArrayList<ColumnKey>(node.size());
    final List<JsonNodeComparable> data = new ArrayList<JsonNodeComparable>(node.size());

    // Without Goodwill integration, simply pass the raw json
    if (schema == null) {
        final Iterator<String> nodeFieldNames = node.fieldNames();
        while (nodeFieldNames.hasNext()) {
            columnKeyList.add(new DynamicColumnKey(nodeFieldNames.next()));
        }

        final Iterator<JsonNode> nodeElements = node.elements();
        while (nodeElements.hasNext()) {
            JsonNode next = nodeElements.next();
            if (next == null) {
                next = NullNode.getInstance();
            }
            data.add(new JsonNodeComparable(next));
        }
    } else {
        // With Goodwill, select only the fields present in the Goodwill schema, and preserve ordering
        for (final GoodwillSchemaField schemaField : schema.values()) {
            final String schemaFieldName = schemaField.getName();
            columnKeyList.add(new DynamicColumnKey(schemaFieldName));
            JsonNode delegate = node.get(schemaFieldName);
            if (delegate == null) {
                delegate = NullNode.getInstance();
            }
            data.add(new JsonNodeComparable(delegate));
        }
    }

    rows.add(RowFactory.getRow(new RowSchema(event.getName(), columnKeyList), data));
}

From source file:net.hamnaberg.json.ValueImpl.java

private static JsonNode getJsonValue(Value value) {
    if (value.isNumeric()) {
        Number number = value.asNumber();
        if (number instanceof BigDecimal) {
            return new DecimalNode((BigDecimal) number);
        }//from   w  w  w . j a  v a  2s  . c o m
        return new DoubleNode(value.asNumber().doubleValue());
    } else if (value.isString()) {
        return new TextNode(value.asString());
    } else if (value.isBoolean()) {
        return BooleanNode.valueOf(value.asBoolean());
    }
    return NullNode.getInstance();
}

From source file:com.palominolabs.testutil.JsonAssert.java

private static void assertJsonNodeEquals(String msg, JsonNode expected, JsonNode actual) {
    if (expected.isTextual()) {
        if (actual.isTextual()) {
            assertEquals(msg, expected.textValue(), actual.textValue());
        } else {/*from  w  w  w  . java 2  s . c  o m*/
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected.isInt()) {
        if (actual.isInt()) {
            assertEquals(msg, expected.intValue(), actual.intValue());
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected.isObject()) {
        if (actual.isObject()) {
            assertJsonObjectEquals(msg, (ObjectNode) expected, (ObjectNode) actual);
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected.isArray()) {
        if (actual.isArray()) {
            assertJsonArrayEquals(msg, (ArrayNode) expected, (ArrayNode) actual);
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected == NullNode.getInstance()) {
        if (actual == NullNode.getInstance()) {
            return;
        }
        nonMatchingClasses(msg, expected, actual);
    } else if (expected.isBoolean()) {
        if (actual.isBoolean()) {
            assertEquals(msg, expected.booleanValue(), actual.booleanValue());
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else {
        fail(msg + "/Can only handle recursive Object, Array and Null instances, got a " + expected.getClass()
                + ": " + expected);
    }
}

From source file:com.github.fge.jsonpatch.mergepatch.ObjectMergePatch.java

@Override
public JsonNode apply(final JsonNode input) throws JsonPatchException {
    BUNDLE.checkNotNull(input, "jsonPatch.nullValue");
    /*//from   w  ww . j a v  a 2 s. c  o m
     * If the input is an object, we make a deep copy of it
     */
    final ObjectNode ret = input.isObject() ? (ObjectNode) input.deepCopy()
            : JacksonUtils.nodeFactory().objectNode();

    /*
     * Our result is now a JSON Object; first, add (or modify) existing
     * members in the result
     */
    String key;
    JsonNode value;
    for (final Map.Entry<String, JsonMergePatch> entry : modifiedMembers.entrySet()) {
        key = entry.getKey();
        /*
         * FIXME: ugly...
         *
         * We treat missing keys as null nodes; this "works" because in
         * the modifiedMembers map, values are JsonMergePatch instances:
         *
         * * if it is a NonObjectMergePatch, the value is replaced
         *   unconditionally;
         * * if it is an ObjectMergePatch, we get back here; the value will
         *   be replaced with a JSON Object anyway before being processed.
         */
        value = Optional.fromNullable(ret.get(key)).or(NullNode.getInstance());
        ret.put(key, entry.getValue().apply(value));
    }

    ret.remove(removedMembers);

    return ret;
}

From source file:com.baasbox.service.scripting.ScriptingService.java

private static ODocument updateStorageLocked(String name, boolean before, JsonCallback updaterFn)
        throws ScriptException {
    final ScriptsDao dao = ScriptsDao.getInstance();
    ODocument script = null;//  w  ww .j  a v a2s .  c o m
    try {
        script = dao.getByNameLocked(name);
        if (script == null)
            throw new ScriptException("Script not found");
        ODocument retScript = before ? script.copy() : script;

        ODocument storage = script.<ODocument>field(ScriptsDao.LOCAL_STORAGE);

        Optional<ODocument> storage1 = Optional.ofNullable(storage);

        JsonNode current = storage1.map(ODocument::toJSON).map(Json.mapper()::readTreeOrMissing)
                .orElse(NullNode.getInstance());
        if (current.isMissingNode())
            throw new ScriptEvalException("Error reading local storage as json");

        JsonNode updated = updaterFn.call(current);
        ODocument result;
        if (updated == null || updated.isNull()) {
            script.removeField(ScriptsDao.LOCAL_STORAGE);
        } else {
            result = new ODocument().fromJSON(updated.toString());
            script.field(ScriptsDao.LOCAL_STORAGE, result);
        }
        dao.save(script);
        ODocument field = retScript.field(ScriptsDao.LOCAL_STORAGE);
        return field;
    } finally {
        if (script != null) {
            script.unlock();
        }
    }
}

From source file:com.github.fge.jsonpatch.mergepatch.JsonMergePatchDeserializer.java

@Override
public JsonMergePatch getNullValue() {
    return new NonObjectMergePatch(NullNode.getInstance());
}

From source file:com.almende.eve.state.couchdb.CouchDBState.java

@Override
public synchronized boolean locPutIfUnchanged(final String key, final JsonNode newVal, JsonNode oldVal) {
    final String ckey = couchify(key);
    boolean result = false;
    try {// w ww. ja  va2s  .  com
        JsonNode cur = NullNode.getInstance();
        if (properties.containsKey(ckey)) {
            cur = properties.get(ckey);
        }
        if (oldVal == null) {
            oldVal = NullNode.getInstance();
        }

        // Poor mans equality as some Numbers are compared incorrectly: e.g.
        // IntNode versus LongNode
        if (oldVal.equals(cur) || oldVal.toString().equals(cur.toString())) {
            properties.put(ckey, newVal);
            update();
            result = true;
        }
    } catch (final UpdateConflictException uce) {
        read();
        return locPutIfUnchanged(ckey, newVal, oldVal);
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "", e);
    }

    return result;
}

From source file:io.macgyver.neorx.rest.NeoRxClient.java

protected ObjectNode createParameters(Object... args) {
    checkNotNull(args);//from  w  ww.  j  ava2s  .c  om
    checkArgument(args.length % 2 == 0, "must be an even number of arguments (key/value pairs)");
    ObjectNode n = mapper.createObjectNode();
    for (int i = 0; i < args.length; i += 2) {
        String key = args[i].toString();

        Object val = args[i + 1];
        if (val == null) {
            n.set(key, NullNode.getInstance());
        } else if (val instanceof String) {
            n.put(key, val.toString());
        } else if (val instanceof Integer) {
            n.put(key, (Integer) val);
        } else if (val instanceof Long) {
            n.put(key, (Long) val);
        } else if (val instanceof Double) {
            n.put(key, (Double) val);
        } else if (val instanceof Boolean) {
            n.put(key, (Boolean) val);

        } else if (val instanceof List) {

            ArrayNode an = mapper.createArrayNode();

            for (Object item : (List) val) {
                an.add(item.toString());
            }
            n.set(key, an);
        } else if (val instanceof Map) {
            n.set(key, mapToObjectNode((Map) val));
        } else if (val instanceof ObjectNode) {
            n.set(key, (ObjectNode) val);
        } else if (val instanceof ArrayNode) {
            n.set(key, (ArrayNode) val);
        } else if (val instanceof JsonNode) {
            JsonNode x = (JsonNode) val;
            if (x.isValueNode()) {
                n.set(key, x);
            } else {
                throw new IllegalArgumentException(
                        "parameter '" + key + "' type not supported: " + val.getClass().getName());
            }
        } else {
            throw new IllegalArgumentException(
                    "parameter '" + key + "' type not supported: " + val.getClass().getName());
        }

    }
    return n;
}

From source file:org.ocsoft.olivia.models.contents.config.JsonObject.java

public boolean getAsBoolean(String key, boolean defaultValue) {
    JsonNode value = root.get(key);//w ww  .j  a  va2 s  . c o m
    if (value == NullNode.getInstance())
        return defaultValue;
    return value.asBoolean(defaultValue);
}

From source file:com.almende.eve.state.couch.CouchState.java

@Override
public boolean locPutIfUnchanged(final String key, final JsonNode newVal, JsonNode oldVal) {
    final String ckey = couchify(key);
    boolean result = false;
    try {//from w w  w .j  a v  a 2  s.c  om
        JsonNode cur = NullNode.getInstance();
        if (properties.containsKey(ckey)) {
            cur = properties.get(ckey);
        }
        if (oldVal == null) {
            oldVal = NullNode.getInstance();
        }

        // Poor mans equality as some Numbers are compared incorrectly: e.g.
        // IntNode versus LongNode
        if (oldVal.equals(cur) || oldVal.toString().equals(cur.toString())) {
            synchronized (properties) {
                properties.put(ckey, newVal);
            }
            db.update(this);
            result = true;
        }
    } catch (final UpdateConflictException uce) {
        read();
        return locPutIfUnchanged(ckey, newVal, oldVal);
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "", e);
    }

    return result;
}