Reputation: 12726
I'm having trouble converting json to a java typed arraylist when I included timestamp fields from the database.
Note that without the timestamp fields (the rest of the fields, not shown here), conversion works fine. It's just when I try to include the timestamp fields below that it fails with a com.google.gson.JsonSyntaxException.
I've seen answers here about using setDateFormat on a GsonBuilder, e.g GSON - Date format
So I tried that, but couldn't make it work. I'm not sure how to do it in this context where I want a typed array list. None of the examples I found was like that.
Here's the code:
Gson gson = new Gson();
TypeToken<List<Resource>> token = new TypeToken<List<Resource>>() {
};
List<Resource> resources = gson.fromJson(response, token.getType());
The mapping object has these fields:
private Timestamp created;
private Timestamp modified;
And corresponding getters and setters:
/**
* @return the created
*/
public Timestamp getCreated() {
return created;
}
/**
* @param created the created to set
*/
public void setCreated(Timestamp created) {
this.created = created;
}
/**
* @return the modified
*/
public Timestamp getModified() {
return modified;
}
/**
* @param modified the modified to set
*/
public void setModified(Timestamp modified) {
this.modified = modified;
}
EDIT:
As requested, here is the full exception message (as much as I got):
Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: 2014-03-02 15:33:55
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at model.DataAccess.getResources(DataAccess.java:82)
at se.expertinfo.ccmsliteplugin.PluginHelper.doSearch(PluginHelper.java:132)
at views.SearchView.triggerSearchEvent(SearchView.java:227)
at views.SearchView.valueChanged(SearchView.java:213)
at javax.swing.JTree.fireValueChanged(JTree.java:2919)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3378)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:634)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1092)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:293)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:187)
at javax.swing.JTree.setSelectionPath(JTree.java:1631)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2396)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3612)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressedDND(BasicTreeUI.java:3589)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3548)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:154)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.text.ParseException: Unparseable date: "2014-03-02 15:33:55"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)
... 69 more
EDIT 2:
Sample Json (very shortened, the actual result is very long, but the syntax is the same):
[
{
"id": "16",
"title": "Warranty 43456",
"created_by": "Anders Svensson",
"modified_by": "Anders Svensson",
"version": "1",
"version_displayname": "1.0",
"informationtype": "document",
"filetype": "xml",
"base_id": "uuid_03372934-a213-11e3-93de-2b47e5a489c2",
"resource_id": "uuid_03372934-a213-11e3-93de-2b47e5a489c2",
"created": "2014-03-02 15:33:55",
"modified": "2014-03-02 15:34:51",
"categories": "10,product,Audi A6;9,product,Audi A4;21,subject,Safety;8,product,Audi A3;21,subject,Safety;15,product,Volvo V70"
},
{
"id": "22",
"title": "Transportation 37337",
"created_by": "Anders Svensson",
"modified_by": "Anders Svensson",
"version": "1",
"version_displayname": "1.0",
"informationtype": "document",
"filetype": "xml",
"base_id": "uuid_03372b96-a213-11e3-93de-2b47e5a489c2",
"resource_id": "uuid_03372b96-a213-11e3-93de-2b47e5a489c2",
"created": "2014-03-02 15:33:55",
"modified": "2014-03-02 15:34:51",
"categories": "10,product,Audi A6;12,product,BMW 320;11,product,BMW 120;13,product,BMW 520;8,product,Audi A3;19,subject,Transportation;9,product,Audi A4;19,subject,Transportation"
}
]
Upvotes: 0
Views: 4027
Reputation: 279970
Set an appropriate date format for your Gson
object. For example,
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
Otherwise it attempts to use some defaults which you can find in the DateTypeAdapter
class source code.
Upvotes: 2