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


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类型的数据进行解析的过程中:抛出以下异常[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kKhPJtbR-1678966707209)(E:/LearnPath/[0006]Note/WizNote/images/image-20230316182452076.png)]


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类型的反序列化转换。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zrDEThnm-1678966707209)(E:/LearnPath/[0006]Note/WizNote/images/image-20230316191215233.png)]

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lq9MU6Ma-1678966707210)(E:/LearnPath/[0006]Note/WizNote/images/image-20230316191145547.png)]

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ngiS02dt-1678966707210)(E:/LearnPath/[0006]Note/WizNote/images/image-20230316192049380.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3CVpK1zh-1678966707211)(E:/LearnPath/[0006]Note/WizNote/images/image-20230316191759938.png)]

三、解决方案:

解决方案一: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://www.newtonsoft.com/json

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

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

公众号:平平无奇代码猴
也可以搜索:Jackiie_wang 公众号,欢迎大家关注!欢迎催更!留言!

作者:ProMer_Wang

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

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

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值