sefasdfasd / google-gson

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

Decoding Date object from Json String not working!! #434

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
run following code snippet.   
   Gson gson = new Gson();
   Date myDate = new Date();   
   System.out.println("JSON : " + gson.toJson(myDate));
   myDate = gson.fromJson("Apr 12, 2012 11:56:04 AM", Date.class);
   System.out.println("Date : " + myDate);

What is the expected output? 
   JSON : "Apr 12, 2012 12:03:58 PM"
   Date : Thu Apr 12 12:03:58 IST 2012

What do you see instead?
   JSON : "Apr 12, 2012 12:04:56 PM"
Exception in thread "main" com.google.gson.JsonSyntaxException: Apr
    at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:78)
    at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:63)
    at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:38)
    at com.google.gson.Gson.fromJson(Gson.java:720)
    at com.google.gson.Gson.fromJson(Gson.java:686)
    at com.google.gson.Gson.fromJson(Gson.java:636)
    at com.google.gson.Gson.fromJson(Gson.java:608)
    at test.Test2.main(Test2.java:19)
Caused by: java.text.ParseException: Unparseable date: "Apr"
    at java.text.DateFormat.parse(DateFormat.java:354)
    at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:76)
    ... 7 more

What version of the product are you using? On what operating system?
   gson 2.0 on Ubuntu 11.10

Original issue reported on code.google.com by e.alifar...@gmail.com on 12 Apr 2012 at 6:36

GoogleCodeExporter commented 9 years ago
That isn't a JSON string; you're losing the quotes. You should pass in a quoted 
string, like this:

   myDate = gson.fromJson("\"Apr 12, 2012 11:56:04 AM\"", Date.class);

Original comment by limpbizkit on 16 Apr 2012 at 9:30