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. HTML区块

    1.HTML 可以通过 <div> 和 <span>将元素组合起来. 2.HTML <div> 元素 HTML <div> 元素是块级元素,它可用于组合 ...

  2. 苹果宣布 2022 年 Apple 设计大奖得主

    Apple 今日举办了年度 Apple 设计大奖颁奖仪式,表彰 12 款出类拔萃的 app 与游戏佳作.今年的获奖者包括来自全球各地的开发者.他们通过 app 呈现锐意创新.别出心裁的优美设计体验,以 ...

  3. 【C++ 字符串题目】 输入三个人名,按字母顺序排序输出

    题目来源:https://acm.ujn.edu.cn Problem A: [C++ 字符串] 输入三个人名,按字母顺序排序输出 Time Limit: 1 Sec  Memory Limit: 1 ...

  4. 论文阅读 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning

    6 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning207 link:https ...

  5. 表达式的动态解析和计算,Flee用起来真香

    前言 在很多项目中经常会出现需要动态解析表达式和计算的场景,比如一些自动审核规则,或者是一些变量的值通过维护的公式在运行过程中动态算出:由于场景需求,都需要比较灵活的配置对应的表达式,然后希望在需要的 ...

  6. rosbag遍历数据出错:(unicode error) 'utf-8' codec can't decode byte 0xcd in position 31: invalid continuation byte

    主题: 前言 针对ros系统记录的bag文件,可以使用python的rosbag包,按照不同起止时间和topic进行提取. 然而,有的topic可以使用rosbag读取,但是不能遍历,存在解码错误.原 ...

  7. ABAP CDS - Annotations 注解

    Syntax ... annotation[.annotation1[.annotation2]][:value]  ... Effect Annotation that can be specifi ...

  8. python采集A站m3u8视频格式视频

    基本开发环境 (https://jq.qq.com/?_wv=1027&k=NofUEYzs) Python 3.6 Pycharm 相关模块的使用 (https://jq.qq.com/?_ ...

  9. Python爬取全球是最大的电影数据库网站IMDb数据

    在使用 Python 开发爬虫的过程中,requests 和 BeautifulSoup4(别名bs4) 应用的比较广泛,requests主要用于模拟浏览器的客户端请求,以获取服务器端响应,接收到的响 ...

  10. Python自动化办公:将文本文档内容批量分类导入Excel表格

    序言 (https://jq.qq.com/?_wv=1027&k=GmeRhIX0) 它来了,它又来了. 本文实现用Python将文本文件自动保存到Excel表格里面去. 需求 将锦江区.t ...