• 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);
        }
    }
    
  • 相关阅读:
    微服务的拆分策略
    一文详解JVM的内存结构
    【HTML】HTML基础7.2(有序列表)
    图解LeetCode——面试题 17.19. 消失的两个数字(难度:困难)
    Spring IOC之ImportSelector接口
    SwiftUI Swift 教程之 14 个有用的数组运算符
    Apollo planning之PathBoundsDecider
    从0基础到车载测试工程师,薪资11K,肯拼搏的人,总会有所收获
    真实的博弈智能可能不太讲究鲁棒性
    Java.lang.Class类 getClasses()方法有什么功能呢?
  • 原文地址:https://www.cnblogs.com/littleperilla/p/16548598.html