LitJson报错记录
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报错记录的更多相关文章
- 报错记录(xml抬头报错)
报错记录(xml抬头报错) Referenced file contains errors (http://www.springframework.org/schema/beans/spring-be ...
- IDEA 报错记录
IDEA 报错记录 Process finished with exit code 0 这种主要是配了默认的 Tomcat ,然后又配置了外部的 Tomcat.解决办法,注释掉默认的: <dep ...
- Spring Boot 报错记录
Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...
- 报错记录:getOutputStream() has already been called for this response
仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...
- g++ 6.4编译opencv-2.4.10报错记录
fetch公司的项目进行编译,此项目依赖opencv库.由于本人一直比较偏爱fedora,但也因此给我带来了许多"乐趣"(麻烦).fedora一直走得比较前沿,g++ 6.3了 ...
- windows的bpython安装方法以及数据库报错--记录
---恢复内容开始--- 安装bpython的时候发现了一个博客讲解如何成功安装bpython,分享一下链接http://www.cnblogs.com/zhaojiedi1992/p/zhaojie ...
- RabbitMq 报错记录
只记录本人当时遇到的情况,仅作参考 添加消息队列报错:The connection cannot support any more channels. Consider creating a new ...
- zabbix3.0报错记录
一.问题描述 在zabbix_server添加变量时,出现了以下的报错,
- scrapy shell 中文网站输出报错.记录.
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 381-382: illegal multibyte sequence 上 ...
随机推荐
- 斯坦福NLP课程 | 第15讲 - NLP文本生成任务
作者:韩信子@ShowMeAI,路遥@ShowMeAI,奇异果@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www. ...
- 205. Isomorphic Strings - LeetCode
Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...
- linux篇-rpm包安装mysql数据库
3.1上传以下两个rpm包到服务器上 MySQL-server-5.6.27-1.el6.x86_64.rpm MySQL-client-5.6.27-1.el6.x86_64.rpm 3.2卸载一个 ...
- 132_Power BI之建模必备要素&Power Query之数据表字段名称管理
博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 近段时间比较忙,也没有看到很好的DAX素材,很久没有更新文章了,刚好有时间就来凑个热闹. 今天主题是Power ...
- python之模块(os、sys、json、subprocess)
目录 os模块 sys模块 json模块 subprocess模块 os模块 os模块主要是与操作系统打交道. 导入os模块 import os 创建单层文件夹,路径必须要存在 os.mkdir(路径 ...
- SpringSecurity的 loginProcessingUrl为什么不能用
前情提要: 我在做一个springsecurity动态鉴权的项目时, 据网上说配置了 loginProcessingUrl("/login1"); 以后 就可以自定义login的请 ...
- 行列式&矩阵树定理
行列式: 参考 oi-wiki 定义 对于一个\(n*n\)的矩阵A行列式取值(标量) \(det(A)=|A|=\sum\limits_p(-1)^{\tau(p)}\prod\limits_{i= ...
- 给小白的 PostgreSQL 容器化部署教程(上)
作者:王志斌 编辑:钟华龙 本文来自社区小伙伴 王志斌 的投稿.从小白的角度,带你一步步实现将 RadonDB PostgreSQL 集群部署到 Kubernetes 上.文章分为上下两部分,第一部分 ...
- VUE3 之 render 函数的使用 - 这个系列的教程通俗易懂,适合新手
1. 概述 老话说的好:不用想的太多.太远,做好当天的事,知道明天要做什么就可以了. 言归正传,今天我们来聊聊 VUE 中 render 函数的使用. 2. render 函数 2.1 一个简单的例子 ...
- mybatis中返回一个List字段
目的:在一个查询接口里面,返回一个人的信息,以及这个人所携带的东西的信息,返回效果如下: { "msg": { "listMain": [ { "id ...