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

JSON does not seem to support the java.util.Date type #29

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

JSON does not seem to support the java.util.Date type #29

GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
Sample code:
Gson gson = new Gson();
Date test = new Date();
System.out.println("Date is "+gson.toJson(test));

What is the expected output? What do you see instead?
Expected output was a json string with current date. Instead
Date is {}      (an empty javascript object)

What version of the product are you using? On what operating system?
gson-1.1.1. Mac OS X, JDK 5

Please provide any additional information below.
I modified my code to pass dates as Strings. But this is not elegant.


Original issue reported on code.google.com by kkape...@gmail.com on 5 Aug 2008 at 4:30

@GoogleCodeExporter
Copy link
Author


We plan to add a Default Date/Time serialization/deserialization in version 
1.2.  The
tricky thing about this feature is that we do not know the format that the 
client
supports.  If the system using the GSON library passes these JSON serialized 
object
back and forth and serializing/deserializing on both ends then its a non-issue 
for
which format is supported; however, if you used the serialized version for 
display
purposes then the date format matters.

For the time being, you can register the following Type Adapter:

/**
 * A default type adapter for a {@link Date} object.
 *
 * @author Joel Leitch
 */
public class DateTypeAdapter implements JsonSerializer<Date>, 
JsonDeserializer<Date> {

  private final DateFormat format = DateFormat.getInstance();

  public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
context) {
    String dateFormatAsString = format.format(src);
    return new JsonPrimitive(dateFormatAsString);
  }

  public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
context)
      throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
      throw new JsonParseException("The date should be a string value");
    }

    try {
      return format.parse(json.getAsString());
    } catch (ParseException e) {
      throw new JsonParseException(e);
    }
  }
}


// Create a GSON instance that can serialize/deserialize "java.util.Date" 
objects
Gson gson = new GsonBuilder()
    .registerTypeAdapter(new DateTypeAdapter())
    .create();

Original comment by joel.leitch@gmail.com on 5 Aug 2008 at 6:41

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

@GoogleCodeExporter
Copy link
Author

Submitted the type adapter to support this feature (r130).  Still need to 
update the
GsonBuilder class to make the Date TypeAdapter configurable without requiring 
the
adapter to be registered.

Original comment by joel.leitch@gmail.com on 5 Aug 2008 at 7:30

@GoogleCodeExporter
Copy link
Author

The change r145 contains the last piece of code required to close off this bug. 
Removed the DateTypeAdapter from the public API so that it can only be 
configured via
the GsonBuilder.

Original comment by joel.leitch@gmail.com on 13 Aug 2008 at 8:28

  • 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