1.float转double报错

报错类型:

Max allowed object depth reached while trying to export from type System.Collections.Generic.List

序列化时候会遇到float和double互转问题;

注意这里double转float会导致精度丢失;

解决办法:

JsonMapper.cs中添加几行代码;

2.Dictionary中key为int时报错

报错类型:

InvalidCastException: Specified cast is not valid

原方法中Dictionary的key只支持(string)强转,int强转为string失败,会报错;

解决办法:

JsonMapper.cs中WriteValue方法修改

3.Dictionary中key为enum时报错

LitJson的字典不支持Key为枚举;

序列化不会出错,反序列化会报错,无法读取枚举类型;

解决办法:

修改JsonMapper.cs中ReadValue方法:

原本reader.Value被强转为string,无法识别出枚举;

下面将reader.Value转为Object;

4.Unity中LitJson类型扩展

添加向量Vector2、Vector3、Vector4;

添加四元素Quaternion、Color、Color32;

添加Bounds、Rect、RectOffset;

项目中加入UnityTypeBridge.cs类和JsonExtension.cs类;

代码:

public static class UnityTypeBindings
{
static bool registerd;
static UnityTypeBindings()
{
Register();
}
public static void Register()
{
if (registerd) return;
registerd = true;
// 注册Type类型的Exporter
JsonMapper.RegisterExporter<Type>((v, w) => { w.Write(v.Ful
JsonMapper.RegisterImporter<string, Type>((s) => { return T
// 注册Vector2类型的Exporter
Action<Vector2, JsonWriter> writeVector2 = (v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("x", v.x);
w.WriteProperty("y", v.y);
w.WriteObjectEnd();
};
JsonMapper.RegisterExporter<Vector2>((v, w) => { writeVecto
// 注册Vector3类型的Exporter
Action<Vector3, JsonWriter> writeVector3 = (v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("x", v.x);
w.WriteProperty("y", v.y);
w.WriteProperty("z", v.z);
w.WriteObjectEnd();
};
JsonMapper.RegisterExporter<Vector3>((v, w) => { writeVecto
// 注册Vector4类型的Exporter
JsonMapper.RegisterExporter<Vector4>((v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("x", v.x);
w.WriteProperty("y", v.y);
w.WriteProperty("z", v.z);
w.WriteProperty("w", v.w);
w.WriteObjectEnd();
});
// 注册Quaternion类型的Exporter
JsonMapper.RegisterExporter<Quaternion>((v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("x", v.x);
w.WriteProperty("y", v.y);
w.WriteProperty("z", v.z);
w.WriteProperty("w", v.w);
w.WriteObjectEnd();
});
// 注册Color类型的Exporter
JsonMapper.RegisterExporter<Color>((v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("r", v.r);
w.WriteProperty("g", v.g);
w.WriteProperty("b", v.b);
w.WriteProperty("a", v.a);
w.WriteObjectEnd();
});
// 注册Color32类型的Exporter
JsonMapper.RegisterExporter<Color32>((v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("r", v.r);
w.WriteProperty("g", v.g);
w.WriteProperty("b", v.b);
w.WriteProperty("a", v.a);
w.WriteObjectEnd();
});
// 注册Bounds类型的Exporter
JsonMapper.RegisterExporter<Bounds>((v, w) =>
{
w.WriteObjectStart();
w.WritePropertyName("center");
writeVector3(v.center, w);
w.WritePropertyName("size");
writeVector3(v.size, w);
w.WriteObjectEnd();
});
// 注册Rect类型的Exporter
JsonMapper.RegisterExporter<Rect>((v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("x", v.x);
w.WriteProperty("y", v.y);
w.WriteProperty("width", v.width);
w.WriteProperty("height", v.height);
w.WriteObjectEnd();
});
// 注册RectOffset类型的Exporter
JsonMapper.RegisterExporter<RectOffset>((v, w) =>
{
w.WriteObjectStart();
w.WriteProperty("top", v.top);
w.WriteProperty("left", v.left);
w.WriteProperty("bottom", v.bottom);
w.WriteProperty("right", v.right);
w.WriteObjectEnd();
}); }
}
public static class JsonExtensions
{
public static void WriteProperty(this JsonWriter w, string name, long value)
{
w.WritePropertyName(name);
w.Write(value);
}
public static void WriteProperty(this JsonWriter w, string name, string value)
{
w.WritePropertyName(name);
w.Write(value);
}
public static void WriteProperty(this JsonWriter w, string name, bool value)
{
w.WritePropertyName(name);
w.Write(value);
}
public static void WriteProperty(this JsonWriter w, string name, double value)
{
w.WritePropertyName(name);
w.Write(value);
} public static void WriteProperty(this JsonWriter w, string name, float value)
{
w.WritePropertyName(name);
w.Write(value);
}
}

LitJson报错记录的更多相关文章

  1. 报错记录(xml抬头报错)

    报错记录(xml抬头报错) Referenced file contains errors (http://www.springframework.org/schema/beans/spring-be ...

  2. IDEA 报错记录

    IDEA 报错记录 Process finished with exit code 0 这种主要是配了默认的 Tomcat ,然后又配置了外部的 Tomcat.解决办法,注释掉默认的: <dep ...

  3. Spring Boot 报错记录

    Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...

  4. 报错记录:getOutputStream() has already been called for this response

    仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...

  5. g++ 6.4编译opencv-2.4.10报错记录

      fetch公司的项目进行编译,此项目依赖opencv库.由于本人一直比较偏爱fedora,但也因此给我带来了许多"乐趣"(麻烦).fedora一直走得比较前沿,g++ 6.3了 ...

  6. windows的bpython安装方法以及数据库报错--记录

    ---恢复内容开始--- 安装bpython的时候发现了一个博客讲解如何成功安装bpython,分享一下链接http://www.cnblogs.com/zhaojiedi1992/p/zhaojie ...

  7. RabbitMq 报错记录

    只记录本人当时遇到的情况,仅作参考 添加消息队列报错:The connection cannot support any more channels. Consider creating a new ...

  8. zabbix3.0报错记录

    一.问题描述 在zabbix_server添加变量时,出现了以下的报错,

  9. scrapy shell 中文网站输出报错.记录.

    UnicodeDecodeError: 'gbk' codec can't decode bytes in position 381-382: illegal multibyte sequence 上 ...

随机推荐

  1. linux篇-linux 下建立多个tomcat

    第一步:复制,解压 将准备好的tomcat压缩包复制到你准备安装的目录,我的tomcat压缩包名字是tomcat.tar.gz,我的安 装目录是 /usr/java/tomcat 第二步:解压tomc ...

  2. 好客租房33-事件绑定this指向(总结)

    1推荐使用class的实例方法 //导入react import React from 'react'   import ReactDOM from 'react-dom' //导入组件   // 约 ...

  3. 没错,就是Access-Control-Allow-Origin,跨域

    服务端添加: <add name="Access-Control-Allow-Origin" value="*" /><add name=&q ...

  4. CF Divan and Kostomuksha

    题意:NKOJ CF 思路:首先发现贪心不了.因此dp.然后这题需要维护的就\(g_i\)和\(sum{g_i}\) 状态:\(dp[i]\): 当前最后一个为\(g_i\)的最大值 \(dp[i]= ...

  5. CF1588F Jumping Through the Array

    在讲正解之前,先播一个小故事: xay 复杂度错误过题.将操作按照时间分块,块内他令所有置换环都必须有至少一个"黑点". 可以通过没有修改 \(p\) 操作,同时 \(p_i=i\ ...

  6. Kafka到底有多高可靠?(RNG NB)

    在聊Kafka高可靠之前,先在评论区来波RNG NB好不好! 什么叫可靠性? 大家都知道,系统架构有三高:「高性能.高并发和高可用」,三者的重要性不言而喻. 对于任意系统,想要同时满足三高都是一件非常 ...

  7. es6.4.2api

    这是讲数据库的数据导入到es里  所有用到了mysql! 1.依赖 <?xml version="1.0" encoding="UTF-8"?> & ...

  8. 【clickhouse专栏】新建库角色用户初始化

    一.创建新的database clickhouse创建数据库的语法几乎和其他的关系型数据库是一样的,区别就是clickhouse存在集群cluster和库引擎engine的概念,可以根据需要进行指定. ...

  9. NLog自定义Target之MQTT

    NLog是.Net中最流行的日志记录开源项目(之一),它灵活.免费.开源 官方支持文件.网络(Tcp.Udp).数据库.控制台等输出 社区支持Elastic.Seq等日志平台输出 实时日志需求 在工业 ...

  10. 华为云发布桌面IDE-CodeArts

    摘要:华为伙伴暨开发者大会2022,发布华为云桌面IDE-CodeArts. 本文分享自华为云社区<华为云发布桌面IDE-CodeArts,让连接更简单.编码更智能>,作者: Huawei ...