skyscreamer / JSONassert

Write JSON unit tests in less code. Great for testing REST interfaces.
http://jsonassert.skyscreamer.org
Apache License 2.0
1k stars 197 forks source link

JSONAssert cannot handle numbers of differing precision in an array of objects if is the only property that differs across the items #115

Open OneGeek opened 5 years ago

OneGeek commented 5 years ago

According to JSONAssert {"Foo":1} == {"Foo":1.0}, but [{"Foo":1}] != [{"Foo":1.0}]. It seems to be deciding to use the number as a "unique key" to determine object identity within the array, which is an invalid assumption in this case.

I would expect JSONAssert to at least parse the value of each field when deciding if they are unequal across all objects in the array (and thus suitable to be used as the unique key).

Test case included below.

package com.example;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

public class TestJSONAssert
{
    // This test fails
    @Test 
    public void testArrayCompareDifferingPrecision() throws JSONException {
        String actual =   "[{ \"Foo\" : 1.0 }]";
        String expected = "[{ \"Foo\" : 1   }]";
        JSONAssert.assertEquals(expected, actual, false);
    }

    @Test
    public void testArrayCompareSamePrecision() throws JSONException {
        String actual =   "[{ \"Foo\" : 1   }]";
        String expected = "[{ \"Foo\" : 1   }]";
        JSONAssert.assertEquals(expected, actual, false);
    }

    @Test
    public void testObjectCompareDifferingPrecision() throws JSONException {
        String actual =   "{ \"Foo\" : 1.0 }";
        String expected = "{ \"Foo\" : 1   }";
        JSONAssert.assertEquals(expected, actual, false);
    }

    @Test
    public void testObjectCompareSamePrecision() throws JSONException {
        String actual =   "{ \"Foo\" : 1   }";
        String expected = "{ \"Foo\" : 1   }";
        JSONAssert.assertEquals(expected, actual, false);
    }
}
OneGeek commented 3 years ago

For what it's worth, this issue just bit me again.

Lu-Jichen commented 3 years ago

I would like to try to work on this issue.