Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonNode.equals(Comparator<JsonNode>, JsonNode) to support configurable/external equality comparison #790

Closed
cowtowncoder opened this issue May 13, 2015 · 2 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

Since it is difficult to make default JsonNode.equals(Object) work "perfectly" (cover all use cases users have), partly because there is no way to configure this handling (no way to pass context/config info, for example), it may make more sense to add a way to use custom Comparators.
The only real functionality in databinding, then, would be traversal of ArrayNodes and ObjectNodes, to handle structural part; but use passed-in comparator object for scalar value comparisons.

@cowtowncoder cowtowncoder changed the title Add JsonNode.equals(Comparator<JsonNode>) to support configurable/external equality comparison Add JsonNode.equals(Comparator<JsonNode>, JsonNode) to support configurable/external equality comparison May 13, 2015
@cowtowncoder cowtowncoder added this to the 2.6.0 milestone May 13, 2015
@cowtowncoder
Copy link
Member Author

Ok: as per unit test:

    public void testCustomComparators() throws Exception
    {
        ObjectNode root1 = MAPPER.createObjectNode();
        root1.put("value", 5);
        ObjectNode root2 = MAPPER.createObjectNode();
        root2.put("value", 5.0);

        // default equals(): not strictly equal
        assertFalse(root1.equals(root2));

        // but. Custom comparator can make all the difference
        Comparator<JsonNode> cmp = new Comparator<JsonNode>() {
            @Override
            public int compare(JsonNode o1, JsonNode o2) {
                if (o1.equals(o2)) {
                    return 0;
                }
                if ((o1 instanceof NumericNode) && (o2 instanceof NumericNode)) {
                    double d1 = ((NumericNode) o1).asDouble();
                    double d2 = ((NumericNode) o2).asDouble();
                    if (d1 == d2) { // strictly equals because it's integral value
                        return 1;
                    }
                }
                return 0;
            }
        };
        // but with custom comparator, yes
        assertTrue(root1.equals(cmp, root2));

@cowtowncoder
Copy link
Member Author

@fge Not sure if it's relevant any more, but I think you wanted more easily configurable comparison. This addition in 2.6 will allow defining simple scalar comparators, and handle Array/Object traversal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant