LitJson報錯記錄

2022-08-03 21:00:29

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