Skip to content

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

@OneGeek

Description

@OneGeek

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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions