【问题标题】:GSON DateTypeException when Converting Date in typed in Milliseconds以毫秒为单位转换日期时的 GSON DateTypeException
【发布时间】:2015-08-19 13:33:29
【问题描述】:

我有一个带有created Date 属性的联系人类。我正在尝试执行以下操作:

    Contact received = gson.fromJson(contactJson, Contact.class);

但是我得到了例外:

 com.google.gson.JsonSyntaxException: 1433444958703
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66)

已经发布了与此类似的解决方案: "Unparseable date: 1302828677828" trying to deserialize with Gson a millisecond-format date received from server

但是它对我不起作用(不会编译)。

Gson 2.3.1 版

想法?

TIA, - 奥莱

【问题讨论】:

    标签: rest gson


    【解决方案1】:

    您可以编写一个简单的类型适配器并将其注册到 GsonBuilder 实例中,或者在 Contact 类本身中使用 @JsonAdapter 进行注释。请注意,您应该将日期序列化为长字符串,因为 long 不是有效的 JSON 类型,并且可能会被修剪为 int(对于大多数日期来说肯定会失败)。

    适配器的外观如下:

    public class LongDateTypeAdapter extends TypeAdapter<Date> {
      @Override public void write(JsonWriter out, Date value) throws IOException {
        if (value == null) {
          out.nullValue();
        } else {
          out.value(String.valueOf(value.getTime()));
        }
      }
      @Override public Date read(JsonReader in) throws IOException {
        switch (in.peek()) {
        case NULL: return null;
        case STRING: 
          try {
            return new Date(Long.parseLong(in.nextString()));
          } catch (NumberFormatException e) {
            throw new JsonSyntaxException(e);
          }
        default: throw new JsonSyntaxException("invalid date" + in.getPath());
        }
      }
    }
    

    您可以在以下位置查看源代码: https://github.com/google-gson/typeadapters/blob/master/common/src/main/java/LongDateTypeAdapter.java

    以下是说明如何使用它的测试: https://github.com/google-gson/typeadapters/blob/master/common/src/test/java/LongDateTypeAdapterTest.java

    【讨论】:

    • 您好@inder123 感谢您指出替代解决方案。我们是否可以向构建器添加一个简单的选项来进行序列化,以指定 Spring Boot 在异常中列出的三种标准形式之一? (\"yyyy-MM-dd'T'HH:mm:ss.SSSZ\" \"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'\" \"EEE, dd MMM yyyy HH :mm:ss zzz\" \"yyyy-MM-dd\")) 另外,如果 GSON 有一个更智能的反序列化来反映 Spring Boot Marshalling 的反序列化并且可能还包含来自其他框架的格式,那就太好了。
    • @user1684269 您应该将 GsonBuilder.setDateFormat() 与您选择的模式一起使用。
    【解决方案2】:

    在这里(Gson: JsonSyntaxException on date)找到了一个类似的解决方案,并稍微调整了一下:

    class JsonDateDeserializer
        implements JsonDeserializer<Date> {
    
        public Date deserialize(JsonElement json,
                                Type date,
                                JsonDeserializationContext context)
            throws JsonParseException {
            String stringDate = json.getAsJsonPrimitive().getAsString();
            return new Date(Long.parseLong(stringDate));
        }
    }
    
    Gson gson =
        new GsonBuilder().registerTypeAdapter(Date.class,
                                              new JsonDateDeserializer())
            .create();
    

    这行得通。如果这是 GSON 中的默认设置,那就太好了:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 2016-07-02
      • 2020-12-17
      • 1970-01-01
      相关资源
      最近更新 更多