ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LitJson报错记录

2022-08-03 14:04:26  阅读:119  来源: 互联网

标签:Exporter name WriteProperty 记录 value 报错 LitJson JsonMapper RegisterExporter


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

标签:Exporter,name,WriteProperty,记录,value,报错,LitJson,JsonMapper,RegisterExporter
来源: https://www.cnblogs.com/littleperilla/p/16546785.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有