nguyennhutkhanh / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

JsonPrimitive's hashCode() function fails to obey the hashCode contract for some numeric values #523

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
JsonPrimitive's hashCode() function sometimes fails to obey the hashCode 
contract.

From Object.hashCode()'s Javadoc:
"If two objects are equal according to the equals(Object) method, then calling 
the hashCode method on each of the two objects must produce the same integer 
result."

With JsonPrimitive, this is not the case for equal double/integer values. For 
example, a JsonPrimitive of 1 (integer) equals a JsonPrimitive of 1d (double), 
but they produce different hash codes.

Reproducing code:
JsonPrimitive p1 = new JsonPrimitive(1);
JsonPrimitive p2 = new JsonPrimitive(1d);
System.out.println(p1.hashCode()); // prints 1
System.out.println(p2.hashCode()); // prints 1072693248
System.out.println(p1.equals(p2)); // prints true

Original issue reported on code.google.com by bcai...@google.com on 14 Jun 2013 at 10:26

GoogleCodeExporter commented 9 years ago
Change/fix to JsonPrimitive equals() in 2.0 resulted in the break.  it was 
working as expected b4 that. 

It might be related to this feature enhancement in 2.0
>>> Gson 2.0 permits integers to have .0 fractions like "1.0".

Looks like hashcode() implementation will have to changed.

Original comment by haripr...@gmail.com on 30 Jul 2013 at 2:02