1.float转double报错

报错类型:

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

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

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

解决办法:

JsonMapper.cs中添加几行代码;

LitJson报错记录-LMLPHP

LitJson报错记录-LMLPHP

2.Dictionary中key为int时报错

报错类型:

InvalidCastException: Specified cast is not valid

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

解决办法:

JsonMapper.cs中WriteValue方法修改

LitJson报错记录-LMLPHP

3.Dictionary中key为enum时报错

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

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

解决办法:

修改JsonMapper.cs中ReadValue方法:

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

下面将reader.Value转为Object;

LitJson报错记录-LMLPHP

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);
    }
}
08-04 10:27