gkopff / gson-javatime-serialisers Goto Github PK
View Code? Open in Web Editor NEWA set of GSON serialiser/deserialisers for dealing with Java 8 java.time entities.
License: MIT License
A set of GSON serialiser/deserialisers for dealing with Java 8 java.time entities.
License: MIT License
It doesn't seem as if this adds a converter for java.time.Duration
:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.gson.internal.reflect.ReflectionHelper
(file:/Users/ifly6/.m2/repository/com/google/code/gson/gson/2.9.1/gson-2.9.1.jar) to field
java.time.Duration.seconds
WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.reflect.ReflectionHelper
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Is that intentional?
If json has time field represented as number of milliseconds InstantConverter
failed to deserialize it
java.time.format.DateTimeParseException: Text '1472545956124' could not be parsed at index 0
See related issue: gkopff/gson-jodatime-serialisers#43
Can I deserialize a value using InstantConverter that includes a UTC offset like, "2017-04-22T00:00:00-04:00", or does the string have to be in UTC? I think the answer is that it doesn't support UTC offset, because I tried and it didn't work, but that could be user error.
Thanks!
I can change Converters#registerAll(GsonBuilder) to the following, and all tests still pass:
public static GsonBuilder registerAll(GsonBuilder builder)
{
if (builder == null) { throw new NullPointerException("builder cannot be null"); }
// registerLocalDate(builder);
// registerLocalDateTime(builder);
// registerLocalTime(builder);
// registerOffsetDateTime(builder);
// registerOffsetTime(builder);
registerZonedDateTime(builder);
return builder;
}
ConvertersTest should probably convert Container to a JsonTree and compare values against what is returned using the converters directly. In my example, LocalDate becomes {"year":2014,"month":12,"day":11}
, which is deserialised without a problem.
UPDATE:
Just saw that this is the case for ALL tests. I can comment out the type adapter registration and the test still passes. So essentially nothing is being tested.
java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at com.fatboyindustrial.gsonjavatime.LocalTimeConverter.deserialize(LocalTimeConverter.java:95)
at com.fatboyindustrial.gsonjavatime.LocalTimeConverter.deserialize(LocalTimeConverter.java:1)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
at com.google.gson.Gson.fromJson(Gson.java:696)
at xxx.util.JsonUtil.fromJson(JsonUtil.java:19)
at xxx.servlet.yyy.zzzServlet.doGet(CreateSchedule.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
I am continuously getting the above the error. but i am failed to find the exact cause. i have downloaded the source and attached it to my application. when i debug it, i found the exception is being thrown when LocalTime is formated from empty string
Adding a serializer/de-serializer for java.time.ZoneId
Don't we need serializer/De-serializer for ZoneId's?
The Guava dependency is unused and can be removed.
The Instant converter is working great for me, but I'm having trouble with the LocalDate and LocalDateTime converters.
private Instant date;
private LocalDateTime date2;
private LocalDate date3;
private Instant date4;
private static Gson gson = Converters.registerAll(new GsonBuilder()).create();
public MyClass(final Instant date) {
this.date = date;
this.date2 = LocalDateTime.ofInstant(date, ZoneId.of("UTC"));
this.date3 = date2.toLocalDate();
this.date4 = date3.atStartOfDay().toInstant(ZoneOffset.UTC);
}
public String toJSON() {
return gson.toJson(this);
}
When I call toJSON
, the two Instants (date
, date4
) are serialized properly to ISO timestamps, but date2
and date3
serialize to {"date": {"year", "month", "day"}, "time": {...}}
and {"year", "month", "day"}
respectively.
Given the relatively simplicity of the implementation of this package, I'm not sure where the error might lie, but I just wanted to inquire here in case you have any ideas.
Helllo,
I have Forked your serializers to be used in Java 7 with threeten library, a backport of java8 date time to Java7. It uses another artifactId to not interfere with the Java8 version.
Do you want to publish it to maven? Maybe i could do a pull request by creating a new branch for this variation?
Thank you for your attention
Phil.
Hi ,
If json has time field represented as number of Seconds InstantConverter failed to deserialize it.
I have a test case where i use gson parse along with awaitility with seconds
Published kafka record has gson.tojson mapping
Throwing following exception::
Failed making field "java.time.Instant.seconds' accessible; either change its visibility or write a custom TypeAdapter for its declaring typel
þal.reflect .ReflectionHelper.makeAccessible (ReflectionHelper.java:22)
mal.bind. ReflectiveTypeAdapterFactory.getBoundField
Caused by: java.lang.reflect.Inaccessible@bjectException: Unable to make field private final long java.time.Instant.seconds accessible: module
I tried following way of registering adaptor but still no success
Any suggestions could you kindly provide.. thanks in advance
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.