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中添加幾行程式碼;

image-20220803133006688

image-20220803133027396

2.Dictionary中key為int時報錯

報錯類型:

InvalidCastException: Specified cast is not valid

原方法中Dictionary的key只支援(string)強轉,int強轉為string失敗,會報錯;

解決辦法:

JsonMapper.cs中WriteValue方法修改

image-20220803133402347

3.Dictionary中key為enum時報錯

LitJson的字典不支援Key為枚舉;

序列化不會出錯,反序列化會報錯,無法讀取枚舉類型;

解決辦法:

修改JsonMapper.cs中ReadValue方法:

原本reader.Value被強轉為string,無法識別出枚舉;

下面將reader.Value轉為Object;

image-20220803200954674

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);
    }
}