Json.NET与Newtonsoft.Json SerializationBinder有所不同 [英] Json.NET vs Newtonsoft.Json SerializationBinder is different

查看:104
本文介绍了Json.NET与Newtonsoft.Json SerializationBinder有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个主题中有很多类似的问题,但是我在这里找不到答案.

I know there are many similar questions in this topic, yet I did not find my answer here.

我已经在Unity和Newtonsoft.json中下载了Json.net作为另一个项目中的NuGet软件包. Newtonsoft.json正在工作,我试图在Unity项目中使用此工作解决方案.

I have downloaded Json.net in Unity and Newtonsoft.json as a NuGet package in a different project. The Newtonsoft.json is working and I tried to use this working solution in a Unity project.

需要使用ISerializationBinder来转换抽象类层次结构,我想仅 而不是其完整程序集名称对类名进行序列化/反序列化. (这样做的原因是C#代码应与其他程序通信.)

I need to use ISerializationBinder for converting an abstract class hierarchy and I want to serialize/deserialize the classnames only, and not their full assembly name. (Reason for that is that the C# code should communicate with a different program.)

我使用具有以下类进行序列化的解决方案(从在线官方示例复制):

I use a solution that has the following class for serializing (copied from an official example online):

public class KnownTypesBinder : ISerializationBinder
{
    public IList<Type> KnownTypes { get; set; }

    public Type BindToType(string assemblyName, string typeName)
    {
        return KnownTypes.SingleOrDefault(t => t.Name == typeName);
    }

    public void BindToName(Type serializedType, out string assemblyName, out string typeName)
    {
        assemblyName = null;
        typeName = serializedType.Name;
    }
}

ISerializationBinderJson.net中不存在. JsonSerializerSettings明显不同:

ISerializationBinder does not exist in Json.net. The JsonSerializerSettings is clearly different:

Newtonsoft.json:

public class JsonSerializerSettings 
{
    public ISerializationBinder SerializationBinder { get; set; }
}

Json.net:

public class JsonSerializerSettings
{
    public SerializationBinder Binder { get; set; }
}

System.Runtime.Serialization.SerializationBinder(不是从ISerializationBinder派生的)没有称为public void BindToName(Type serializedType, out string assemblyName, out string typeName)的方法,这意味着无法定义类型名称.

System.Runtime.Serialization.SerializationBinder (is not derived from ISerializationBinder) has no method called public void BindToName(Type serializedType, out string assemblyName, out string typeName), which means the type name cannot be defined.

  • 那么,如何在Unity中仅使用类名来解决序列化问题? (仅在此处设置TypeNameHandling = TypeNameHandling.Objects,不能解决问题)
  • 为什么不是所有人都声称自己是同一个人吗? (即使下面的Json.net的StackOverflow标签也可以做到这一点.)
  • 差异是否与Unity使用Mono的事实有关?
  • So, how can I solve the serialization - by using only the class names - in Unity? (Only setting TypeNameHandling = TypeNameHandling.Objects, is no solution here)
  • Why does everyone claim that they are the same when it is clearly not the case? (Even the StackOverflow tag below for Json.net does that.)
  • Does the difference have anything to do with the fact that Unity uses Mono?

谢谢.

推荐答案

这是版本控制问题.您所指的JSON .NET for Unity只是使用(如注释中所述)Newtonsoft.Json 8.0.3.

This is a versioning issue. The JSON .NET for Unity you are referring to as just Json.NET uses (as mentioned in the comments) Newtonsoft.Json 8.0.3.

如果您想使用Newtonsoft.Json的新功能,建议您使用其他解决方案. Newton.Json为Unity制作的当前最新分支在这里: https://github.com/jilleJr/Newtonsoft.Json-for-Unity#readme ,在撰写本文时位于Newtonsoft.Json 12.0.3.

If you want to use fresh features from Newtonsoft.Json I suggest jumping to a different solution. The currently most up-to-date fork of Newtonsoft.Json made for Unity is here: https://github.com/jilleJr/Newtonsoft.Json-for-Unity#readme, being at Newtonsoft.Json 12.0.3 at the time of writing.

jilleJr/Newtonsoft.Json-for-Unity也通过内置的Unity软件包管理器进行部署,因此,使用户保持最新状态也很容易.

jilleJr/Newtonsoft.Json-for-Unity is also deployed via the builtin Unity Package Manager, so staying up-to-date will be easier as a user as well.

这篇关于Json.NET与Newtonsoft.Json SerializationBinder有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆