Example usage for com.fasterxml.jackson.databind JsonNode equals

List of usage examples for com.fasterxml.jackson.databind JsonNode equals

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode equals.

Prototype

public abstract boolean equals(Object paramObject);

Source Link

Usage

From source file:de.thingweb.desc.ThingDescriptionParserTest.java

@Test
public void testRoundtrip1() throws Exception {
    // TODO shall we test round tripping of any node? e.g, "actuator:unit": "actuator:ms" as part of inputData

    // String filename = "jsonld" + File.separator + "led.v2.plain.jsonld";
    String tdSample = "{\r\n" + "  \"@context\": [\r\n"
            + "    \"http://w3c.github.io/wot/w3c-wot-td-context.jsonld\",\r\n"
            + "    { \"actuator\": \"http://example.org/actuator#\" }\r\n" + "  ],\r\n"
            + "  \"@type\": \"Thing\",\r\n" + "  \"name\": \"MyLEDThing\",\r\n" + "  \"uris\": [\r\n"
            + "    \"coap://myled.example.com:5683/\",\r\n"
            + "    \"http://mything.example.com:8080/myled/\"\r\n" + "  ],\r\n"
            + "  \"encodings\": [ \"JSON\",\"EXI\"],\r\n" + "  \"security\": {\r\n"
            + "    \"cat\": \"token:jwt\",\r\n" + "    \"alg\": \"HS256\",\r\n"
            + "    \"as\": \"https://authority-issuing.example.org\"\r\n" + "  },\r\n"
            + "  \"properties\": [\r\n" + "    {\r\n" + "      \"@type\": \"actuator:onOffStatus\",\r\n"
            + "      \"name\": \"status\",\r\n" + "      \"valueType\": { \"type\": \"boolean\" },\r\n"
            + "      \"writable\": true,\r\n" + "      \"hrefs\": [ \"pwr\", \"status\" ]\r\n" + "    }\r\n"
            + "  ],\r\n" + "  \"actions\": [\r\n" + "    {\r\n" + "      \"@type\": \"actuator:fadeIn\",\r\n"
            + "      \"name\": \"fadeIn\",\r\n" + "      \"inputData\": {\r\n"
            + "        \"valueType\": { \"type\": \"integer\" }"
            //            + ",\r\n" + 
            //            "        \"actuator:unit\": \"actuator:ms\"\r\n"
            + "      },\r\n" + "      \"hrefs\": [\"in\", \"led/in\"  ]\r\n" + "    },\r\n" + "    {\r\n"
            + "      \"@type\": \"actuator:fadeOut\",\r\n" + "      \"name\": \"fadeOut\",\r\n"
            + "      \"inputData\": {\r\n" + "        \"valueType\": { \"type\": \"integer\" }"
            //            + ",\r\n" + 
            //            "        \"actuator:unit\": \"actuator:ms\"\r\n"
            + "      },\r\n" + "      \"hrefs\": [\"out\", \"led/out\" ]\r\n" + "    }\r\n" + "  ],\r\n"
            + "  \"events\": [\r\n" + "    {\r\n" + "      \"@type\": \"actuator:alert\",\r\n"
            + "      \"name\": \"criticalCondition\",\r\n"
            + "      \"valueType\": { \"type\": \"string\" },\r\n"
            + "      \"hrefs\": [ \"ev\", \"alert\" ]\r\n" + "    }\r\n" + "  ]\r\n" + "}";

    ObjectMapper mapper = new ObjectMapper();

    JsonNode original = mapper.readValue(tdSample, JsonNode.class);
    JsonNode generated = mapper.readValue(
            ThingDescriptionParser.toBytes(ThingDescriptionParser.fromBytes(tdSample.getBytes())),
            JsonNode.class);

    assertTrue(original.equals(generated));
}

From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntityFormatterTest.java

private boolean jsonEqual(String string1, String string2) {
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    try {//  w  w  w. j av a 2 s .c  o m
        JsonNode json1 = mapper.readTree(string1);
        JsonNode json2 = mapper.readTree(string2);
        return json1.equals(json2);
    } catch (IOException ex) {
        Logger.getLogger(EntityFormatterTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

From source file:org.opendaylight.sfc.sbrest.json.SfstateExporterTest.java

private boolean testExportSfstateJson(String expectedResultFile, boolean nameOnly) throws IOException {
    ServiceFunctionState serviceFunctionState;
    String exportedSfstateString;
    SfstateExporterFactory sfstateExporterFactory = new SfstateExporterFactory();

    if (nameOnly) {
        serviceFunctionState = this.buildServiceFunctionStateNameOnly();
        exportedSfstateString = sfstateExporterFactory.getExporter().exportJsonNameOnly(serviceFunctionState);
    } else {/*from  w ww .jav  a2 s  . co m*/
        serviceFunctionState = this.buildServiceFunctionState();
        exportedSfstateString = sfstateExporterFactory.getExporter().exportJson(serviceFunctionState);
    }

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedSfstateJson = objectMapper
            .readTree(this.gatherServiceFunctionStateJsonStringFromFile(expectedResultFile));
    JsonNode exportedSfstateJson = objectMapper.readTree(exportedSfstateString);

    System.out.println("EXPECTED: " + expectedSfstateJson);
    System.out.println("CREATED:  " + exportedSfstateJson);

    return expectedSfstateJson.equals(exportedSfstateJson);
}

From source file:com.github.fge.jackson.JsonNumEquals.java

@Override
protected boolean doEquivalent(final JsonNode a, final JsonNode b) {
    /*/*  w  ww  .jav  a2s. c o m*/
     * If both are numbers, delegate to the helper method
     */
    if (a.isNumber() && b.isNumber())
        return numEquals(a, b);

    final NodeType typeA = NodeType.getNodeType(a);
    final NodeType typeB = NodeType.getNodeType(b);

    /*
     * If they are of different types, no dice
     */
    if (typeA != typeB)
        return false;

    /*
     * For all other primitive types than numbers, trust JsonNode
     */
    if (!a.isContainerNode())
        return a.equals(b);

    /*
     * OK, so they are containers (either both arrays or objects due to the
     * test on types above). They are obviously not equal if they do not
     * have the same number of elements/members.
     */
    if (a.size() != b.size())
        return false;

    /*
     * Delegate to the appropriate method according to their type.
     */
    return typeA == NodeType.ARRAY ? arrayEquals(a, b) : objectEquals(a, b);
}

From source file:com.almende.eve.state.mongo.MongoState.java

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

        // Poor man's equality as some Numbers are compared incorrectly: e.g.
        // IntNode versus LongNode
        if (oldVal.equals(cur) || oldVal.toString().equals(cur.toString())) {
            properties.put(key, newVal);
            result = updateProperties(false); // updateField(key, newVal);
        }
    } catch (final UpdateConflictException e) {
        LOG.log(Level.WARNING, e.getMessage());
        reloadProperties();
        // recur if update conflict occurs
        locPutIfUnchanged(key, newVal, oldVal);
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "locPutIfUnchanged error", e);
    }

    return result;
}

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 {/*from  w  ww  .j  a  v a 2s.  co m*/
        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: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 ww.j  av  a2 s  .c  o  m*/
        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;
}

From source file:com.almende.eve.state.ConcurrentJsonFileState.java

@Override
public synchronized boolean locPutIfUnchanged(final String key, final JsonNode newVal, JsonNode oldVal) {
    boolean result = false;
    try {/*from w  w  w .j  a va  2 s .  c o  m*/
        openFile();
        read();

        JsonNode cur = NullNode.getInstance();
        if (properties.containsKey(key)) {
            cur = properties.get(key);
        }
        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(key, newVal);
            write();
            result = true;
        }
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "", e);
        // Don't let users loop if exception is thrown. They
        // would get into a deadlock....
        result = true;
    }
    closeFile();
    return result;
}

From source file:com.almende.eve.state.file.ConcurrentJsonFileState.java

@Override
public boolean locPutIfUnchanged(final String key, final JsonNode newVal, JsonNode oldVal) {
    boolean result = false;
    try {/*ww  w  . ja va2 s  .co  m*/
        openFile();
        read();

        JsonNode cur = NullNode.getInstance();
        if (properties.containsKey(key)) {
            cur = properties.get(key);
        }
        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(key, newVal);
            write();
            result = true;
        }
    } catch (final IllegalStateException e) {
        LOG.log(Level.WARNING, "Couldn't handle Statefile: " + e.getMessage(), e);
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "", e);
        // Don't let users loop if exception is thrown. They
        // would get into a deadlock....
        result = true;
    }
    closeFile();
    return result;
}

From source file:com.unboundid.scim2.common.DiffTestCase.java

/**
 * Test comparison of multi-valued complex attributes.
 *
 * @throws Exception if an error occurs.
 *///from   w ww  . ja  v  a 2  s.  c  om
@Test
public void testDiffMultiValuedAttribute() throws Exception {
    // *** multi-valued ***
    ObjectNode source = JsonUtils.getJsonNodeFactory().objectNode();
    ObjectNode target = JsonUtils.getJsonNodeFactory().objectNode();

    // - unchanged
    String email1 = "bjensen@example.com";
    String email2 = "babs@jensen.org";

    source.putArray("emails").add(email1).add(email2);
    target.putArray("emails").add(email1).add(email2);

    // - added
    String phone1 = "1234567890";
    String phone2 = "0987654321";

    target.putArray("phones").add(phone1).add(phone2);

    // - removed
    String im1 = "babs";
    String im2 = "bjensen";

    source.putArray("ims").add(im1).add(im2);
    target.putArray("ims");

    // - updated
    // -- unchanged
    String photo0 = "http://photo0";
    String photo1 = "http://photo1";
    // -- add a new value
    String photo3 = "http://photo3";
    // -- remove a value
    String thumbnail = "http://thumbnail1";

    source.putArray("photos").add(photo0).add(photo1).add(thumbnail);
    target.putArray("photos").add(photo0).add(photo1).add(photo3);

    // -- updated with all new values
    String entitlement1 = "admin";
    String entitlement2 = "user";
    String entitlement3 = "inactive";
    source.putArray("entitlements").add(entitlement1).add(entitlement2);
    target.putArray("entitlements").add(entitlement3);

    List<PatchOperation> d = JsonUtils.diff(source, target, false);
    assertEquals(d.size(), 4);

    assertTrue(d.contains(PatchOperation.remove(Path.root().attribute("ims"))));
    assertTrue(
            d.contains(PatchOperation.remove(Path.root().attribute("photos", Filter.eq("value", thumbnail)))));
    ObjectNode replaceValue = JsonUtils.getJsonNodeFactory().objectNode();
    replaceValue.putArray("entitlements").add(entitlement3);
    replaceValue.putArray("phones").add(phone1).add(phone2);
    assertTrue(d.contains(PatchOperation.replace(replaceValue)));
    ObjectNode addValue = JsonUtils.getJsonNodeFactory().objectNode();
    addValue.putArray("photos").add(photo3);
    assertTrue(d.contains(PatchOperation.add(addValue)));

    List<PatchOperation> d2 = JsonUtils.diff(source, target, true);
    for (PatchOperation op : d2) {
        op.apply(source);
    }
    removeNullNodes(target);

    // Have to compare photos explicitly since ordering of array values doesn't
    // matter.
    JsonNode sourcePhotos = source.remove("photos");
    JsonNode targetPhotos = target.remove("photos");
    assertEquals(sourcePhotos.size(), targetPhotos.size());
    for (JsonNode sourceValue : sourcePhotos) {
        boolean found = false;
        for (JsonNode targetValue : targetPhotos) {
            if (sourceValue.equals(targetValue)) {
                found = true;
                break;
            }
        }
        if (!found) {
            fail("Source photo value " + sourceValue + " not in target photo array " + targetPhotos);
        }
    }
    assertEquals(source, target);
}