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

Unparseable date exception when parsing SQL dates in 1.5 #268

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 5 comments
Closed

Unparseable date exception when parsing SQL dates in 1.5 #268

GoogleCodeExporter opened this issue Mar 19, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?

1. set date format as follows:

Gson gson = new GsonBuilder().setPrettyPrinting().setDateFormat("MMM dd, yyyy 
hh:mm:ss a").create();

2. create a new java.sql.Date 
java.sql.Date sqlDate = new java.sql.Date(12345l);

3. create a class with a date field and create an instance of that class 
setting our date as a member

private static class MyDate {
    public Date date;
}

3. set the date in the class and serialize it to JSON:


public static void main(String[] args) throws ParseException {

    Gson gson = new GsonBuilder().setPrettyPrinting().setDateFormat("MMM dd, yyyy hh:mm:ss a").create();

    java.sql.Date sqlDate = new java.sql.Date(12345l);

    MyDate d = new MyDate();
    d.date = sqlDate;

    String json = gson.toJson(d);
    System.out.println(json);
    gson.fromJson(json, MyDate.class);
}

private static class MyDate {
    public Date date;
}

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

serialized MyDate {"date":"Dec 31, 1969"}) does not have a time component in 
gson 1.5, however in gson 1.4 MyDate will be serialized as {"date":"Dec 31, 
1969 07:00:12 PM"} 

Attempting to deserialize MyDate which was serialized with gson 1.5 results in 
the following exception, when using the same DateFormat:

Exception in thread "main" com.google.gson.JsonParseException: 
java.text.ParseException: Unparseable date: "Dec 31, 1969"

The desired behavior would be to serialize the time component as zeros when it 
is specified in the DateFormat, but not available in the date object. The 
current behavior of 1.5 results in unparsable date exceptions when the date 
does not have a time component.

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

gson version 1.5, on Windows 7 and Red Hat Enterprise Linux Server release 5.3 



Original issue reported on code.google.com by dmitri.sotnikov@gmail.com on 23 Nov 2010 at 10:42

@GoogleCodeExporter
Copy link
Author

I'm seeing the same issue (I think)...out of curiosity, have you tried this 
with gson 1.6 yet?

Original comment by snag...@yahoo.com on 30 Nov 2010 at 10:26

@GoogleCodeExporter
Copy link
Author

The same problem occurs in Gson 1.6, the main issue is that I think you should 
be able to deserialize the data with the same GsonBuilder that was used to 
serialize it, and that's currently not the case.

Original comment by dmitri.sotnikov@gmail.com on 30 Nov 2010 at 11:24

@GoogleCodeExporter
Copy link
Author

The work around I'm using currently is to write custom serializers and 
deserializers for dates:

        private static final String dateFormat = "MM/dd/yyyy hh:mm:ss a";
    private static SimpleDateFormat format = new SimpleDateFormat(dateFormat);

    private static class DateTimeSerializer implements JsonSerializer<Date> {
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(format.format(src));
        }
    }

    private static class DateTimeDeserializer implements JsonDeserializer<Date> {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            try {
                return format.parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage());
            }
        }
    }

Original comment by dmitri.sotnikov@gmail.com on 30 Nov 2010 at 11:26

@GoogleCodeExporter
Copy link
Author

Cool, thanks.  It turned out the problem I was having was that my object model 
had a java.sql.Timestamp (derived from the database), which is a subclass of 
Date and therefore skipped the custom serializer.  By registering all the 
subclasses (including java.sql.Date, etc.) with the same serializer, it works 
fine now.

BTW, is there no way to configure the GsonBuilder so that subclasses use the 
same custom serializer?  It seems like that would be desired, unless a more 
specific serializer applied.

Original comment by snag...@yahoo.com on 1 Dec 2010 at 5:26

@GoogleCodeExporter
Copy link
Author

Fixed by r762.

Original comment by limpbizkit on 29 Mar 2011 at 9:53

  • Changed state: Fixed

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