Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Gson date serialization is locale-specific #228

Open
GiantTreeLP opened this issue Jan 6, 2020 · 2 comments
Open

Gson date serialization is locale-specific #228

GiantTreeLP opened this issue Jan 6, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@GiantTreeLP
Copy link
Member

Gson serializes and deserializes dates based on locale.
This can cause issues like the following stacktrace.

We should change the default settings in Gson but upgrading may require a custom type adapter.

com.google.gson.JsonSyntaxException: Jan 6, 2020, 9:39:24 AM
	at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:74) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:59) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.TypeAdapters$26$1.read(TypeAdapters.java:598) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.TypeAdapters$26$1.read(TypeAdapters.java:596) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.Gson.fromJson(Gson.java:887) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.Gson.fromJson(Gson.java:952) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at de.dytanic.cloudnet.lib.utility.document.Document.getObject(Document.java:423) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.api.network.packet.in.PacketInLogoutPlayer.handleInput(PacketInLogoutPlayer.java:25) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.network.protocol.packet.PacketManager$2.run(PacketManager.java:99) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.network.protocol.packet.PacketManager$2.run(PacketManager.java:92) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.utility.CollectionWrapper.iterator(CollectionWrapper.java:61) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.network.protocol.packet.PacketManager.dispatchPacket(PacketManager.java:92) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.network.NetDispatcher$1.run(NetDispatcher.java:66) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.scheduler.TaskScheduler$VoidTaskEntry$1.call(TaskScheduler.java:585) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.scheduler.TaskScheduler$VoidTaskEntry$1.call(TaskScheduler.java:579) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.scheduler.TaskEntry.invoke(TaskEntry.java:33) ~[CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.scheduler.TaskScheduler$Worker.execute(TaskScheduler.java:527) [CloudNetAPI.jar:2.1.17]
	at de.dytanic.cloudnet.lib.scheduler.TaskScheduler$Worker.run(TaskScheduler.java:490) [CloudNetAPI.jar:2.1.17]
Caused by: java.text.ParseException: Failed to parse date ["Jan 6, 2020, 9:39:24 AM']: Invalid number: Jan 
	at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:72) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	... 20 more
Caused by: java.lang.NumberFormatException: Invalid number: Jan 
	at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:311) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:129) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:72) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]
	... 20 more
@issue-label-bot issue-label-bot bot added the bug Something isn't working label Jan 6, 2020
@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.87. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@GiantTreeLP GiantTreeLP self-assigned this Jan 6, 2020
@juliarn
Copy link
Contributor

juliarn commented Jan 6, 2020

Changing the date format with the GsonBuilder should solve this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants