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类;
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| public static class UnityTypeBindings { static bool registerd; static UnityTypeBindings() { Register(); } public static void Register() { if (registerd) return; registerd = true; JsonMapper.RegisterExporter<Type>((v, w) => { w.Write(v.Ful JsonMapper.RegisterImporter<string, Type>((s) => { return T 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 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 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(); }); 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(); }); 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(); }); 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(); }); JsonMapper.RegisterExporter<Bounds>((v, w) => { w.WriteObjectStart(); w.WritePropertyName("center"); writeVector3(v.center, w); w.WritePropertyName("size"); writeVector3(v.size, w); w.WriteObjectEnd(); }); 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(); }); 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(); }); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 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); } }
|