google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.29k stars 4.28k forks source link

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

Open CyborTronik opened 8 years ago

CyborTronik commented 8 years ago

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

JakeWharton commented 8 years ago

Note: JodaTime and JSR310 are very different things.

On Fri, Nov 20, 2015 at 1:55 PM CyborTronik notifications@github.com 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 https://github.com/google/gson/issues/739.

inder123 commented 8 years ago

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 commented 8 years ago

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 notifications@github.com 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 https://github.com/google/gson/issues/739#issuecomment-158677356.

kang0921ok commented 4 years ago

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 commented 2 years ago

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

EDIT: I guess it is until java8 is the minimum version supported https://github.com/google/gson/blob/827e117f6dafa776a6103ed2cce81975c4dfad67/pom.xml#L30

Mario-Eis commented 2 years ago

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 commented 2 years ago

@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 commented 1 year ago

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 commented 11 months ago

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());
    }
}