gkopff / gson-jodatime-serialisers Goto Github PK
View Code? Open in Web Editor NEWA set of Gson serialiser/deserialisers for dealing with Joda Time entities.
License: MIT License
A set of Gson serialiser/deserialisers for dealing with Joda Time entities.
License: MIT License
If local date or datetime is empty on json property, the structure cannot be deserialized. The issue lies on parsing local date or datetime as the following code demonstrate
final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN);
fmt.parseLocalDate(null); // throws NPE
fmt.parseLocalDate(""); // throws java.lang.IllegalArgumentException
Throws exception here:
java.lang.UnsupportedOperationException: JsonObject at com.google.gson.JsonElement.getAsString
`// Do not try to deserialize null or empty values
if json.getAsString() == null || json.getAsString().isEmpty()) {
return null;
}`
it is because json.getAsString() throws it but json.toString() works good
here is stackoverflow question https://stackoverflow.com/a/9324906/1307690
@spegelref You added the bits and pieces to make this OSGi aware. I wondered if you knew what the right thing to do is with regards to OSGi + JPMS?
The simplest first step towards supporting JPMS is to add an automatic module name entry to the MANIFEST.MF
.
However, it looks like just configuring the maven-jar-plugin
to do it, like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>com.fatboyindustrial.gson-javatime-serialisers</Automatic-Module-Name>
</manifestEntries>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
... doesn't work when bnd-maven-plugin
is involved in creating the manifest.
Any ideas how to get bnd-maven-plugin
and maven-jar-plugin
to place nicely together?
I see you add PeriodConverter code, So What is the expected release date for version 1.4
This is a suggestion, it would be handy to have Kotlin extension functions for all Converters.register*
functions.
For example, Converters.registerDateTime(GsonBuilder)
would become:
fun GsonBuilder.registerDateTime(): GsonBuilder {
registerTypeAdapter(DATE_TIME_TYPE, DateTimeConverter())
return this
}
And consequentially, Converters.registerDateTime(new GsonBuilder()).create();
would become:
GsonBuilder().registerDateTime().create()
I'm getting:
java.lang.UnsupportedOperationException: JsonObject
at com.google.gson.JsonElement.getAsString(JsonElement.java:191)
at com.fatboyindustrial.gsonjodatime.DateTimeConverter.deserialize(DateTimeConverter.java:85)
at com.fatboyindustrial.gsonjodatime.DateTimeConverter.deserialize(DateTimeConverter.java:44)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
at com.google.gson.Gson.fromJson(Gson.java:814)
at com.google.gson.Gson.fromJson(Gson.java:779)
at com.google.gson.Gson.fromJson(Gson.java:728)
at com.google.gson.Gson.fromJson(Gson.java:700)
For object with next fields:
private String id;
private DateTime createdAt;
private DateTime updatedAt;
private boolean enabled;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public DateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(DateTime createdAt) {
this.createdAt = createdAt;
}
public DateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(DateTime updatedAt) {
this.updatedAt = updatedAt;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
Using:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId>
<artifactId>gson-jodatime-serialisers</artifactId>
<version>1.2.0</version>
</dependency>
php timestamp is not different with java timestamp, if the server is php, this library would not work.
I believe the reason this happens is because the default "Type" for a DateTime object only goes down to the hour.
I posted a bit on SO about it here:
http://stackoverflow.com/questions/31710000/joda-datetime-deserializer-is-truncating-datetimes-minute-second-millisecond
also a relevant post that may be able to help us fix this is here:
http://stackoverflow.com/questions/18786243/use-gson-with-date4j-datetime
Add support for optional values: these should be encoded as an empty string, and returned as Optional<DateTime>
. This will add a dependency on Guava.
When parsing a string like "2016-06-29T12:00:00+02:00"
the DateTimeConverter will crash, because it uses ISODateTimeFormat.dateTime()
to get a formatter that explicitely expects a millisecond value. There is an alternative that explicitely doesn't excpect a millisecond value: ISODateTimeFormat.dateTimeNoMillis()
.
Both versions should be supported, as both are quite common (and valid regarding ISO 8601).
Parsing dates with no millisecond values breaks for instants.
E.g 2019-07-23T00:06:12Z
From GSON docs:
New applications should prefer TypeAdapter, whose streaming API is more efficient than this interface's tree API.
@test
public void testJodaDateZoneUsingGson() throws Exception{
Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
DateTime now = new DateTime(2016,8,29,15,10,30);
DateTimeZone dtz = DateTimeZone.forID("Europe/Berlin");
DateTime now1 = now.withZone(dtz);
System.out.println(now1 + "," + now1.getZone());
String nowStr1 = gson.toJson(now1);
DateTime convertedNow1 = gson.fromJson(nowStr1, DateTime.class);
assertEquals("Europe/Berlin", convertedNow1.getZone().toString());
}
The convertedNow1.getZone() return is Local Zone.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.