irpas技术客

Unity3D中项目打包Android平台包运行过程中抛出异常:JSON integer is too large or small for an Int64_

网络投稿 5428

Unity3D中项目打包Android平台包运行过程中抛出异常:JSON integer is too large or small for an Int64 背景:一、遇到情况描述:二、导致的原因:三、解决方案:解决方案一:String手动解析解决方案二:更新Newtonsoft.Json的Dll库 四、参考链接: Unity3D中项目打包Android平台包运行过程中抛出异常:JSON integer is too large or small for an Int64

背景:

博主在打包的过程中,“屡次接收到”报错啦 ? ” “打包了没?”“打好了没?”,我可以很肯定地告诉你:,作为一个打包仔而言,老老实实解决bug解决问题比较重要,可能是项目中之前的数据没有达到这么大的一个阈值,当遇到这种情况后就确实有点头疼难解了。多处引用,改动风险比较大毕竟跟项目挂钩的比较紧密,时间也比较紧。然后在网上搜索相关的内容后,大概也是给了一个简单的思路,后续的内容还是已经解决的这个问题后才发现的,所以后续的思路仅供参考。替换dll是伴随风险的,除非一开始项目采用的就是新版的,否则一般不要这么去做。期间的改动,需要大家一起去评估对项目的影响和风险,采取最适合风险最低的方式去处理。大概就总结了一个这样的解决问题的思路文章。

一、遇到情况描述:

项目代码中涉及到了采用微软下 Newtonsoft.Json.dll解析库,数据序列化与反序列化的过程

???Unity打包过程中不报错,手机调试运行过程中抛出异常;(这个比较坑,槽点在于如果你调试的过程中没有去try catch,unity下是无法捕获的)

项目中序列化的过程中采用了下述这种方式:

JsonConvert.SerializeObject(data, settings);

反序列化的过程中采用了下述这种解析方式:

JsonConvert.DeserializeObject<xxxProtoInfo>(jsondata);

在试图对Ulong类型的数据进行解析的过程中:抛出以下异常


Newtonsoft.Json.JsonReaderException : JSON integer 9111111111111111117 is too large or small for an Int64. Path ‘xxxparm’, line 1, position 58. at Newtonsoft.Json.JsonTextReader.ParseNumber (Newtonsoft.Json.ReadType readType) at Newtonsoft.Json.JsonTextReader.ParseValue () at Newtonsoft.Json.JsonTextReader.Read () at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00053] in at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value)

二、导致的原因:

1、 👀**罪魁祸首 ** Newtonsoft.json.dll 版本较低(.net 3.5版本),类库文件本身不支持ulong类型的反序列化转换。

通过上述描述,我们其实也得知了,就是此处类型的bug,为了更加方便理解,翻译如下:

???所以综上所述,如果您在项目当中采用到了Newtonsoft.Json做转换,并且抛出了此类的异常,估计就是以上原因了,即采用了一个并不适配ulong类型或者更大的值的 dll库文件,既然遇到了此类问题,一定是有办法解决的,作者也贴心的在github中提到了解决方式,”简单且粗暴🐧“ ,即序列化成字符串后再自己进行转换。作者也表示后续讲修复这个问题(目前来说也的确修复了这个问题,亲测,但是至于序列化的过程有没有差异就不知道了)

三、解决方案: 解决方案一:String手动解析

将数据序列化成string后,对string进行划分并解析成对应的类结构即数据结构(已解决,代码有优化空间)

public static TestInfo1 ConvertByJsonData(List<string> jsonData) { TestInfo1 info = new TestInfo1(0, 0, 0, null);//假定TestInfo1中的类结构成员为(string,int,ulong,TestInfo)对应类型的变量; if (jsonData!=null && jsonData.Count > 0) { for (var i = 0; i < jsonData.Count; i++) { switch (jsonData[i]) { case "key1": infoValue1 = jsonData[i+1];//将string解析成类结构成员变量,注意:记得匹配类型;假定infoValue1 为string类型 break; case "key2": infoValue2= int.Parse(jsonData[i+1]);//将string解析成类结构成员变量,注意:记得匹配类型;假定infoValue2 为Int类型 break; case "key3": infoValue3 = ulong.Parse(jsonData[i+1]);//将string解析成类结构成员变量,注意:记得匹配类型;假定infoValue3 为ulong类型 break; case "key4": if (jsonData[i + 1] != "null") { //类结构中包含类结构,跟这个同样的结构去解析 TestInfo2 testinfo2 = xxxxx.ConvertByJsonData(List<string> jsonData) } break; } } } return info; } 解决方案二:更新Newtonsoft.Json的Dll库

更新dll库,照搬原方法,替换库文件即可(注意平台依赖关系,防止无用功打包)

更新项目中引用所依赖的dll库,更新为已解决该问题的dll库即可。

(未亲测,测过一个版本有问题,由于比较急,暂时还是采用的自己手动解析的方法做的处理)

注意:相差版本过大,部分方法或许可能存在不适配;博主踩过坑…

四、参考链接:

以下贴上对应库下载地址以及作者的github链接地址,大家自行进行了解和学习;

一个简单好用的json 转换网址: https://codebeautify.org/string-to-json-online

Json.Net 下载地址: https://·/json

Json.Net 源码地址:https://github.com/JamesNK/Newtonsoft.Json

如果您遇到了这个问题,并且我的文章确确实实帮到您了,希望您给一个赞支持一下我呀,喜欢我的文章的朋友也可以关注我的博客,关注我的后续的文章分享呀,谢谢啦。

作者:ProMer_Wang

链接:https://blog.csdn.net/qq_43801020/article/details/129596819

本文为ProMer_Wang的原创文章,著作权归作者所有,转载请注明原文出处,欢迎转载!


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #integer #is #too #large #or #small #for