Java GSON-Joda时间串行器有标准实现吗?

Java GSON-Joda时间串行器有标准实现吗?,java,gson,jodatime,Java,Gson,Jodatime,我使用GSON将一些对象图序列化为JSON。这些对象图形使用实体(DateTime,LocalTime等) 谷歌最热门的“gson joda”是以下页面: 它为org.joda.time.DateTime提供类型适配器的源代码。此链接也是中引用的链接 我希望找到一个包含joda time Serialiser的预卷库,可以作为Maven依赖项引用,但我找不到 有吗?还是我被迫在我自己的项目中复制该片段?在我看来,没有这种库是很正常的 乍一看可能很简单,但风险在于最终会有很多依赖项(有些在编

我使用GSON将一些对象图序列化为JSON。这些对象图形使用实体(
DateTime
LocalTime
等)

谷歌最热门的“gson joda”是以下页面:

它为
org.joda.time.DateTime
提供类型适配器的源代码。此链接也是中引用的链接

我希望找到一个包含joda time Serialiser的预卷库,可以作为Maven依赖项引用,但我找不到


有吗?还是我被迫在我自己的项目中复制该片段?

在我看来,没有这种库是很正常的

乍一看可能很简单,但风险在于最终会有很多依赖项(有些在编译时,有些在运行时),要确保不创建不需要的依赖项并不容易


对于您的情况,这应该是可以的,因为我认为这只是一个运行时依赖项(如果没有使用JODA,那么使用serializerLib的项目应该不需要JODA lib)。但在其他一些情况下,这可能会变得丑陋。

复制该片段,许多人使用不同的日期字符串格式,从而混淆任何库的创建。

我在我的项目中使用下一个

public final class DateTimeDeserializer implements JsonDeserializer<DateTime>, JsonSerializer<DateTime>
{
   static final org.joda.time.format.DateTimeFormatter DATE_TIME_FORMATTER =
      ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);

   @Override
   public DateTime deserialize(final JsonElement je, final Type type,
                           final JsonDeserializationContext jdc) throws JsonParseException
   {
      return je.getAsString().length() == 0 ? null : DATE_TIME_FORMATTER.parseDateTime(dateAsString);
   }

   @Override
   public JsonElement serialize(final DateTime src, final Type typeOfSrc,
                                final JsonSerializationContext context)
   {
      return new JsonPrimitive(src == null ? StringUtils.EMPTY :DATE_TIME_FORMATTER.print(src)); 
   }
}
公共最终类DateTimeDeserializer实现JsonDeserializer、JsonSerializer
{
静态最终org.joda.time.format.DateTimeFormatter日期\时间\格式化程序=
ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
@凌驾
public DateTime反序列化(最终JsonElement je,最终类型,
最终JsonDeserializationContext(jdc)抛出JsonParseException
{
返回je.getAsString().length()=0?null:DATE\u TIME\u FORMATTER.parseDateTime(dateAsString);
}
@凌驾
公共JsonElement序列化(最终日期时间src,最终类型typeOfSrc,
最终JsonSerializationContext(上下文)
{
返回新的JsonPrimitive(src==null?StringUtils.EMPTY:DATE\u TIME\u FORMATTER.print(src));
}
}

我决定推出自己的开源软件-您可以在这里找到:

以下是Maven的详细信息(查看central以了解最新版本):


向GSON注册TypeAdapter以包装Joda预配置格式化程序的使用,请参阅

import java.lang.reflect.Type;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonPrimitive;
导入com.google.gson.JsonSerializationContext;
导入com.google.gson.JsonSerializer;
导入java.lang.reflect.Type;
导入org.joda.time.DateTime;
导入org.joda.time.format.ISODateTimeFormat;
公共类JodDateTimeWithGSON{
公共静态void main(字符串[]args){
Gson Gson=new GsonBuilder()
.registerTypeAdapter(DateTime.class,新的JsonSerializer()){
@凌驾
公共JsonElement序列化(日期时间json,类型typeOfSrc,JsonSerializationContext){
返回新的JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
}
})
.create()
;
//输出[“20160222T15:58:33.218Z”,42,“字符串”]
System.out.println(gson.toJson(新对象[]{DateTime.now(),42,“String”});
}
}

我使用上面的答案做了一个小助手,它将处理包含日期时间变量的模型对象的序列化和反序列化

    public static Gson gsonDateTime() {
    Gson gson = new GsonBuilder()
            .registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
                @Override
                public JsonElement serialize(DateTime json, Type typeOfSrc, JsonSerializationContext context) {
                    return new JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
                }
            })
            .registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
                @Override
                public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    DateTime dt = ISODateTimeFormat.dateTime().parseDateTime(json.getAsString());
                    return dt;
                }
            })
            .create();
    return gson;
}
公共静态Gson gsonDateTime(){
Gson Gson=new GsonBuilder()
.registerTypeAdapter(DateTime.class,新的JsonSerializer()){
@凌驾
公共JsonElement序列化(日期时间json,类型typeOfSrc,JsonSerializationContext){
返回新的JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
}
})
.registerTypeAdapter(DateTime.class,新的JsonDeserializer()){
@凌驾
公共DateTime反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext)引发JsonParseException{
DateTime dt=ISODateTimeFormat.DateTime().parseDateTime(json.getAsString());
返回dt;
}
})
.create();
返回gson;
}

我希望有一个
gson-jodatime转换器
库,为各种Joda时间实体提供gson序列化器。这不会有失控的依赖关系-它只有两个:GSON(我已经在使用)和Joda Time(我已经在使用)。我明白了,然后你会有很多依赖关系,但采用了非常模块化的方法。这是可行的,但我想它并不存在,因为没有人认为为一小群类维护一个开源项目是有意义的。我决定维护这一小群类(并将结果发布到Maven Central)。请参见此处:该包使用ISO 8601将非常简单。它不需要与其他东西互操作(在第一个实例中)-它只需要能够往返数据本身。是的,这是您的需要(我也是),但一些项目使用JSON与异构环境通信(为此,我尝试使用SOAP,而更多地使用WSDLs自文档),所以一个库应该需要高水平的“可配置性”才能成为标准库。嗨,你能提供导入吗?我发现大约有20种classes@MartinL使用了GSONlib()
Type
是来自java反射API的类:
java.lang.reflect.Type
我试图将其添加到我的datetime反序列化程序中,但我的应用程序仍然崩溃。。。。因为无法读取datetime对象,该怎么办?对我来说也不管用。日期书写正确,即:,但当Gson试图读回它时,它哽住了。@Cheezmeister你们有例外吗?有一个例外
Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
SomeContainerObject original = new SomeContainerObject(new DateTime());

String json = gson.toJson(original);
SomeContainerObject reconstituted = gson.fromJson(json, SomeContainerObject.class);
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

public class JodaDateTimeWithGson {

    public static void main(String[] args) {
        Gson gson = new GsonBuilder()
            .registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>(){
                @Override
                public JsonElement serialize(DateTime json, Type typeOfSrc, JsonSerializationContext context) {
                    return new JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
                }
            })
            .create()
            ;

        // Outputs ["20160222T15:58:33.218Z",42,"String"]
        System.out.println(gson.toJson(new Object[] {DateTime.now(), 42, "String"}));
    }
}
    public static Gson gsonDateTime() {
    Gson gson = new GsonBuilder()
            .registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
                @Override
                public JsonElement serialize(DateTime json, Type typeOfSrc, JsonSerializationContext context) {
                    return new JsonPrimitive(ISODateTimeFormat.dateTime().print(json));
                }
            })
            .registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
                @Override
                public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    DateTime dt = ISODateTimeFormat.dateTime().parseDateTime(json.getAsString());
                    return dt;
                }
            })
            .create();
    return gson;
}