2
When using the library GSON
for the manipulation of JSON
, I realized that by doing the parsers the date is being changed, it’s tiny, but it’s enough to break my unit test with JUnit
.
Follow a sketch code to show strange behavior.
Gson gson = new Gson();
Date data = new Date();
String dataNoFormatoJSON = gson.toJson(data);
Date dataDoJSON = gson.fromJson(dataNoFormatoJSON, Date.class);
long dataTime = data.getTime();
long dataDoJSONTime = dataDoJSON.getTime();
System.out.println(data + " - " + dataTime);
System.out.println(dataDoJSON + " - " + dataDoJSONTime);
Exit from this code:
Tue Oct 17 17:02:03 GFT 2017 - 1508270523483
Tue Oct 17 17:02:03 GFT 2017 - 1508270523000
First the toString()
of Date
and then the date.getTime()
which is the representation of that date in long
.
Realize that the toString
, apparently shows no change, already the date.getTime
is different.
first(before the JSON parse) 1508270523483
after (after parse JSON) 1508270523000
I used Gson myself and it worked, Tks!!!
– Henrique Santiago