• 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((v, w) => { w.Write(v.Ful
            JsonMapper.RegisterImporter<string, Type>((s) => { return T
            // 注册Vector2类型的Exporter
            Action writeVector2 = (v, w) =>
            {
                w.WriteObjectStart();
                w.WriteProperty("x", v.x);
                w.WriteProperty("y", v.y);
                w.WriteObjectEnd();
            };
            JsonMapper.RegisterExporter((v, w) => { writeVecto
            // 注册Vector3类型的Exporter
            Action writeVector3 = (v, w) =>
            {
                w.WriteObjectStart();
                w.WriteProperty("x", v.x);
                w.WriteProperty("y", v.y);
                w.WriteProperty("z", v.z);
                w.WriteObjectEnd();
            };
            JsonMapper.RegisterExporter((v, w) => { writeVecto
            // 注册Vector4类型的Exporter
            JsonMapper.RegisterExporter((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((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((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((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((v, w) =>
            {
                w.WriteObjectStart();
                w.WritePropertyName("center");
                writeVector3(v.center, w);
                w.WritePropertyName("size");
                writeVector3(v.size, w);
                w.WriteObjectEnd();
            });
            // 注册Rect类型的Exporter
            JsonMapper.RegisterExporter((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((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);
        }
    }
    
  • 相关阅读:
    机器学习绪论
    一文速通Sentinel熔断及降级规则
    HTML5学习系列之实用性标记
    RecyclerView 刷新Item图片闪烁
    【实操】基于ChatGPT构建知识库
    如何改进企业旧式工时管理系统?
    vite - 多渠道差异化打包插件
    评估和选择最佳学习模型的一些指标总结
    计算机体系结构:MIPS计算例题(1.7)
    Git入门
  • 原文地址:https://www.cnblogs.com/littleperilla/p/16548598.html