IllegalArgumentException:使用@JsonString注释反序列化生成的对象 [英] IllegalArgumentException: Deserializing generated objects with @JsonString annotation

查看:81
本文介绍了IllegalArgumentException:使用@JsonString注释反序列化生成的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由Cloud Endpoints生成的对象中的长值用@JsonString注释.使用GsonFactory反序列化这些对象时,这将导致IllegalArgumentException.

Long values in Objects generated by Cloud Endpoints are annotated with @JsonString. This causes a IllegalArgumentException when deserializing those Objects using a GsonFactory.

这是堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: number type formatted as a JSON number cannot use @JsonString annotation [key updated, field private java.lang.Long com.google.api.services.timetable.model.Lesson.updated]
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)
    at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:599)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:350)
    at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:586)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:289)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:266)
    at com.google.api.client.json.JsonFactory.fromString(JsonFactory.java:207)

产生异常的示例代码:

GsonFactory gsonFactory = new GsonFactory();
Lesson lesson = new Lesson();
lesson.setUpdated(2);
String json = gsonFactory.toString(lesson);
gsonFactory.fromString(json, Lesson.class);

原始讨论 https://groups.google.com/d/msg/endpoints-trusted-testers/-/__ TKGoruZVt0J

推荐答案

发生此异常的原因是因为Java客户端库期望所有长整数都加引号(也称为字符串),因为JavaScript无法处理64位正确的整数精度.有一个已知的问题,Python SDK无法正确地将64位整数序列化为字符串.我不确定确切地从何处获取JSON,但是如果它在用户代码中,则需要确保您也正确引用了64位整数.

The reason why this exception occurs is because the Java client library expects all long integers to be quoted (aka strings), because JavaScript can't handle 64-bit integer precision correctly. There's a known issue where the Python SDK won't correctly serialize 64-bit integers as strings. I'm not sure where you're getting the JSON from, exactly, but if it's in user code, you need to make sure you also have 64-bit integers quoted properly.

这篇关于IllegalArgumentException:使用@JsonString注释反序列化生成的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆