Skip to content

Floats with zeros after decimal point and integers are treated as identical #114

Open
@NateEag

Description

@NateEag

This isn't really a bug, but it is a behavior that caught me off-guard, so I thought it was worth mentioning. The right resolution for this may well be "Works as intended."

I recently learned the hard way that two blobs of raw JSON can be different but be reported as identical by json-diff. A minimal reproduction is below:

git clone [email protected]:andreyvit/json-diff.git
cd json-diff
npm ci
echo '{"amount": 42.0}' > old.json
echo '{"amount": 42}' > new.json
# No output from the below
./bin/json-diff.js old.json new.json

Semantically, 42.0 === 42, so this is arguably the Right Thing.

The two documents are not precisely identical, however.

In my case, I was comparing them because I had two documents that were returning different SHA256 hashes and I wanted to see how they differed.

Being told they were identical by json-diff threw me off for a moment.

I happened to already know they were differing lengths, so it didn't take me long to figure out what was happening.

I could easily see myself having been thrown off badly if I'd had a little less information before I used json-diff on my documents.

As I said at the start - this isn't exactly a genuine bug, but it really surprised me, so I thought I'd mention it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions