有没有办法让改造/gson 将双时间戳转换为长?

我正在开发一个Android应用程序。我正在使用改造Gson来调用服务器并序列化/反序列化。在请求中,有一个包含timestamp( long) 的响应类。有时这是double因为iOS应用程序的版本double用于存储时间戳。


有没有办法强制Gson反序列化响应并转换为Response类(长)上存在的对象?


例外:


Caused by com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Expected a long but was 1555323142345.364 at line 1 column 1002 path $.userSettings.stories[4].readTimestamp

       at com.google.gson.internal.bind.TypeAdapters$11.read(TypeAdapters.java:323)

       at com.google.gson.internal.bind.TypeAdapters$11.read(TypeAdapters.java:313)

       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)

       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)

       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)

       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)

       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)

       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)

       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)

       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)

       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)

慕娘9325324
浏览 113回答 1
1回答

慕婉清6462132

为简单起见,假设您的JSON有效负载是:{&nbsp; &nbsp; "timestamp": 1555323142345.345}它应该适合:class Pojo {&nbsp; &nbsp; private long timestamp;&nbsp; &nbsp; // getters, setters, toString}解决方案取决于您是否可以更改Pojo模型。如果是并且您想要存储double并且long可以将类型更改为Number:private Number timestamp;你应该能够解析double' 和long' 的时间戳。如果您总是想要long实现自定义反序列化器:class LongOrDoubleJsonDeserializer implements JsonDeserializer<Long> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {&nbsp; &nbsp; &nbsp; &nbsp; if (json.isJsonPrimitive()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Number number = json.getAsNumber();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return number.longValue();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}您的Pojo财产应如下所示:@JsonAdapter(LongOrDoubleJsonDeserializer.class)private long timestamp;如果您无法更改Pojo课程,则需要TypeAdapterFactory使用自定义和注册GsonBuilder。请参阅此链接:创建改造实例。自定义实现可能如下所示:class LongOrDoubleTypeAdapterFactory implements TypeAdapterFactory {&nbsp; &nbsp; @Override&nbsp; &nbsp; public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {&nbsp; &nbsp; &nbsp; &nbsp; Class<? super T> rawType = type.getRawType();&nbsp; &nbsp; &nbsp; &nbsp; if (rawType == Long.class || rawType == long.class) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new TypeAdapter<T>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void write(JsonWriter out, T value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public T read(JsonReader in) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) new Long(in.nextLong());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) new Long(((Double) in.nextDouble()).longValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}并注册如下:Gson gson = new GsonBuilder()&nbsp; &nbsp; .registerTypeAdapterFactory(new LongOrDoubleTypeAdapterFactory())&nbsp; &nbsp; .create();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java