Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could you please add support of java.time as is part of Java8 ? #739

Open
CyborTronik opened this issue Nov 20, 2015 · 9 comments
Open

Could you please add support of java.time as is part of Java8 ? #739

CyborTronik opened this issue Nov 20, 2015 · 9 comments
Labels
enhancement java8 Issues related to making Java 8 the minimum supported version

Comments

@CyborTronik
Copy link

Hi,
Could you please make builtin support of JodaTime types as is included in Java and no needs for separate library?

@JakeWharton
Copy link
Contributor

Note: JodaTime and JSR310 are very different things.

On Fri, Nov 20, 2015 at 1:55 PM CyborTronik [email protected]
wrote:

Hi,
Could you please make builtin support of JodaTime types as is included in
Java and no needs for separate library?


Reply to this email directly or view it on GitHub
#739.

@inder123
Copy link
Collaborator

minimum supported version of Java in Gson is Java 5. The next version will bump it up to Java 6.
We don't intend to require Java 8 any time soon.

Is there a way this can be supported without bumping up the minimum supported version?

@JakeWharton
Copy link
Contributor

I would guess a peer library built against Java 8 which just provides a
TypeAdapterFactory for javax.time.

On Sat, Nov 21, 2015 at 2:50 PM inder123 [email protected] wrote:

minimum supported version of Java in Gson is Java 5. The next version will
bump it up to Java 6.
We don't intend to require Java 8 any time soon.

Is there a way this can be supported without bumping up the minimum
supported version?


Reply to this email directly or view it on GitHub
#739 (comment).

@JakeWharton JakeWharton changed the title Could you please add support of JodaTime as is part of Java8 ? Could you please add support of java.time as is part of Java8 ? Jun 20, 2018
@kang0921ok
Copy link

kang0921ok commented Apr 17, 2020

Hi I hava a question.
Not supported yet?
What is recommended method use a jsr310 in gson?

I searched this url : https://github.com/google-gson/typeadapters
It is best method?

@RockyMM
Copy link

RockyMM commented Jun 7, 2022

Is this issue still relevant (after all these years)?

EDIT: I guess it is until java8 is the minimum version supported

gson/pom.xml

Line 30 in 827e117

<javaVersion>7</javaVersion>

@Mario-Eis
Copy link

According to Oracle (https://www.oracle.com/java/technologies/java-se-support-roadmap.html), even the extended support for Java 7 ended in July 2022.

@RockyMM
Copy link

RockyMM commented Sep 27, 2022

@Mario-Eis I guess this does not matter. There are many clients of this library still using Java 7. I would guess that there are a lot or some very important internal Google projects that use Java 7. Until the gson in whole drops Java 7 support, there won't be any progress here.

More info #2018

@joannakotula
Copy link

Until gson supports java.time properly it will not fully support java 17. We are currently migrating to java 17, and it seems that we will have to replace all gson calls, as we use java.time a lot:

com.google.gson.JsonIOException: Failed making field 'java.time.Duration#seconds' accessible; either increase its visibility or write a custom TypeAdapter for its declaring type.

@ca-chan-ka
Copy link

Workaround: Create a gson instance with custom adapters like this

import java.lang.reflect.Type;
import java.time.*;
import java.util.Map;
import java.util.function.Function;
import com.google.gson.*;
​
public class GsonWithJavaTime {
    public static Gson getInstance() {
        GsonBuilder builder = new GsonBuilder();
        Map<Type, Function<String, Object>> parsers = Map.of(
                ZonedDateTime.class, ZonedDateTime::parse,
                LocalDate.class, LocalDate::parse,
                LocalDateTime.class, LocalDateTime::parse
        );
        parsers.entrySet().forEach(entry -> builder.registerTypeAdapter(entry.getKey(), adapter(entry.getValue())));
        return builder.create();
    }
​
    private static <T> JsonDeserializer<T> adapter(Function<String, T> function) {
        return (element, type, context) -> function.apply(element.getAsJsonPrimitive().getAsString());
    }
}

@eamonnmcmanus eamonnmcmanus added the java8 Issues related to making Java 8 the minimum supported version label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement java8 Issues related to making Java 8 the minimum supported version
Projects
None yet
Development

No branches or pull requests

10 participants