mani-terrazas / google-gson

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

Serialize dates as JavaScript Date constructor #578

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I want to use Gson to create a JSON string with dates represented as literal 
JavaScript Date constructors. For example 

  {v: new Date(2008, 1, 28, 0, 31, 26)}

This is how the Google Charts API's JavaScript literal data parameter can 
receive dates. See 
https://developers.google.com/chart/interactive/docs/reference#dataparam

Original issue reported on code.google.com by davidcwe...@gmail.com on 5 May 2014 at 7:44

GoogleCodeExporter commented 9 years ago
You can use a custom TypeAdapter to accomplish this. Please see the 
documentation at 
https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/
TypeAdapter.html

Original comment by j...@squareup.com on 9 Aug 2014 at 5:45

GoogleCodeExporter commented 9 years ago
I can see no way how to accomplish exactly this. You can get

{v: "new Date(2008, 1, 28, 0, 31, 26)"}

but it's not the same. It's a string rather than constructor invocation. 
However, it's no valid JSON either.

Original comment by Maaarti...@gmail.com on 12 Aug 2014 at 5:01