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

Ordinal number type inequality after deserialization #64

Open
GoogleCodeExporter opened this issue Dec 24, 2015 · 5 comments
Open

Ordinal number type inequality after deserialization #64

GoogleCodeExporter opened this issue Dec 24, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. Create a JSONObject with byte, short, int key/value pairs.
2. Create a new JSONObject with the string from the first JSONObject
3. Compare them with .equals

What is the expected output? What do you see instead?

I expect equals to return true. 

What version of the product are you using? On what operating system?

Json-Simple V1.1 on JRE 1.6.29 on Windows 7.

Please provide any additional information below.

It fails because on the first object, the native specified types are honored in 
the map (byte, short, int).  But after being loaded from the JSON string, all 
of the ordinal values are (long).  So the equals in the Map comparison checks 
the classes and the Byte.class is not equals to Long.class so it fails even 
though numerically the values are the same.  I have a wrapper class for 
JSONObject and worked around the problem like this:

if (value instanceof Float) {
            asMap(jsonObject).put(key, ((Number)value).doubleValue());
        } else if (value instanceof Double) {
            asMap(jsonObject).put(key, ((Number)value).doubleValue());
        } else if (value instanceof Number) {
            asMap(jsonObject).put(key, ((Number)value).longValue());
        } else {
            asMap(jsonObject).put(key, value);
        }

Original issue reported on code.google.com by [email protected] on 19 Jan 2012 at 5:32

@GoogleCodeExporter
Copy link
Author

It's not guaranteed to be equal: some meta info such as data type will be lost 
when serializing Java Object to JSON literal. For example, if we have a 
java.lang.Long with value 123, when generating JSON, it will output the value 
123 itself without any type info, now if we want to read it back to Java 
object, we have no way to know if we should make it a Long or an Integer. 

Original comment by [email protected] on 21 Feb 2012 at 5:32

  • Changed state: WontFix

@GoogleCodeExporter
Copy link
Author

Right but that's why I normalized it on the way in.  You have to pick a type 
(longs and doubles it seems) when you are deserializing, may as well normalize 
the numbers on the way in to match their deserialized counterparts.

Original comment by [email protected] on 21 Feb 2012 at 5:35

@GoogleCodeExporter
Copy link
Author

If the value of an int or short or long is 123, the value stored in JSON is 
"123". While you have insufficient information to return the value "123" as an 
int rather than long, you can determine that the converted values are 
identical.  

The Map comparison should not check the Java objects, but rather the JSON 
ordinal values to be compared.


Original comment by [email protected] on 21 Feb 2012 at 5:52

@GoogleCodeExporter
Copy link
Author

Well, I accept it as an enhancement to override the "equals" of JSONObject to 
make it compare values instead of objects, if it's the case of this request.

Original comment by [email protected] on 21 Feb 2012 at 11:45

  • Changed state: Accepted
  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

That would definitely help. Thanks!

Original comment by [email protected] on 22 Feb 2012 at 3:30

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

No branches or pull requests

1 participant