<small id='hm44C'></small><noframes id='hm44C'>

    1. <i id='hm44C'><tr id='hm44C'><dt id='hm44C'><q id='hm44C'><span id='hm44C'><b id='hm44C'><form id='hm44C'><ins id='hm44C'></ins><ul id='hm44C'></ul><sub id='hm44C'></sub></form><legend id='hm44C'></legend><bdo id='hm44C'><pre id='hm44C'><center id='hm44C'></center></pre></bdo></b><th id='hm44C'></th></span></q></dt></tr></i><div id='hm44C'><tfoot id='hm44C'></tfoot><dl id='hm44C'><fieldset id='hm44C'></fieldset></dl></div>

        <bdo id='hm44C'></bdo><ul id='hm44C'></ul>
    2. <tfoot id='hm44C'></tfoot>

        <legend id='hm44C'><style id='hm44C'><dir id='hm44C'><q id='hm44C'></q></dir></style></legend>

        JSON.Net:反序列化多态类型而不指定程序集

        时间:2023-08-23
        <tfoot id='HxqKh'></tfoot>
      1. <legend id='HxqKh'><style id='HxqKh'><dir id='HxqKh'><q id='HxqKh'></q></dir></style></legend>

        <i id='HxqKh'><tr id='HxqKh'><dt id='HxqKh'><q id='HxqKh'><span id='HxqKh'><b id='HxqKh'><form id='HxqKh'><ins id='HxqKh'></ins><ul id='HxqKh'></ul><sub id='HxqKh'></sub></form><legend id='HxqKh'></legend><bdo id='HxqKh'><pre id='HxqKh'><center id='HxqKh'></center></pre></bdo></b><th id='HxqKh'></th></span></q></dt></tr></i><div id='HxqKh'><tfoot id='HxqKh'></tfoot><dl id='HxqKh'><fieldset id='HxqKh'></fieldset></dl></div>
          <tbody id='HxqKh'></tbody>

            <small id='HxqKh'></small><noframes id='HxqKh'>

              • <bdo id='HxqKh'></bdo><ul id='HxqKh'></ul>

                1. 本文介绍了JSON.Net:反序列化多态类型而不指定程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我看到使用 JSON.Net,如果 $type 属性指定 JSON 对象的特定类型,我可以解码多态对象.在我见过的所有示例中,$type 都包含命名空间.是否可以在没有程序集的情况下仅包括一个简单的类型名称来完成这项工作?如果可能的话,我很乐意为 JsonSerializer 指定一个默认程序集.

                  I see that using JSON.Net, I can decode polymorphic objects if a $type attribute specifies the specific type of the JSON object. In all the examples I've seen, $type includes the namespace. Is it possible to make this work including just a simple type name without the assembly? I'd be happy to specify a default assembly to the JsonSerializer if that's possible.

                  我可以使用以下方法反序列化 JSON:

                  I am able to deserialize the JSON using:

                  public class SingleAssemblyJsonTypeBinder : SerializationBinder
                  {
                      private readonly Assembly _assembly;
                      private Dictionary<string, Type> _typesBySimpleName = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase); 
                      private Dictionary<Type,string> _simpleNameByType = new Dictionary<Type, string>(); 
                  
                      public SingleAssemblyJsonTypeBinder(Assembly assembly)
                      {
                          _assembly = assembly;
                          _typesBySimpleName = new Dictionary<string, Type>();
                  
                          foreach (var type in _assembly.GetTypes().Where(t => t.IsPublic))
                          {
                              if (_typesBySimpleName.ContainsKey(type.Name))
                                  throw new InvalidOperationException("Cannot user PolymorphicBinder on a namespace where multiple public types have same name.");
                  
                              _typesBySimpleName[type.Name] = type;
                              _simpleNameByType[type] = type.Name;
                          }
                      }
                  
                      public override Type BindToType(string assemblyName, string typeName)
                      {
                          Type result;
                          if (_typesBySimpleName.TryGetValue(typeName.Trim(), out result))
                              return result;
                  
                          return null;
                      }
                  
                      public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
                      {
                          string name;
                  
                          if (_simpleNameByType.TryGetValue(serializedType, out name))
                          {
                              typeName = name;
                              assemblyName = null;// _assembly.FullName;
                          }
                          else
                          {
                              typeName = null;
                              assemblyName = null;
                          }
                      }
                  }
                  

                  ...

                  public static JsonSerializerSettings GetJsonSerializationSettings()
                  {
                      var settings = new JsonSerializerSettings();
                      settings.Binder = new SingleAssemblyJsonTypeBinder(typeof(MvcApplication).Assembly);
                      settings.TypeNameHandling = TypeNameHandling.Objects;
                      return settings;
                  }
                  

                  ...

                  var serializer = JsonSerializer.Create(settings);
                  

                  虽然我无法使用 MVC 完成这项工作.我正在根据 Application_Start 中的以下代码配置 JSON 反序列化,并且对象已反序列化,但使用的是基类型之一.

                  I haven't been able to make this work with MVC though. I'm configuring JSON deserialization per the code below in Application_Start, and the object is deserialized, but using the base type one.

                  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Binder = new SingleAssemblyJsonTypeBinder(this.GetType().Assembly);
                  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.All;
                  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple;
                  

                  推荐答案

                  创建一个派生的 SerializationBinder 在其中重写 BindToName 并设置 out string assemblyName 为 null(Edit: 或您的默认程序集名称)和 out string typeName 为您的条带类型名称

                  Create a derived SerializationBinder in which override BindToName and Set out string assemblyName to null (Edit: or your default assembly name) and out string typeName to your striped type name

                  在序列化之前将 binder 设置为 JsonSerializerSettings.Binder.

                  Set the binder to the JsonSerializerSettings.Binder before serialisation.

                  如果这不起作用,请告诉我

                  Let me know if this is not working

                  这篇关于JSON.Net:反序列化多态类型而不指定程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:覆盖 Json.Net 中的默认原始类型处理 下一篇:在 json.net 中重命名 JProperty

                  相关文章

                  最新文章

                2. <legend id='DyhNv'><style id='DyhNv'><dir id='DyhNv'><q id='DyhNv'></q></dir></style></legend>

                    <i id='DyhNv'><tr id='DyhNv'><dt id='DyhNv'><q id='DyhNv'><span id='DyhNv'><b id='DyhNv'><form id='DyhNv'><ins id='DyhNv'></ins><ul id='DyhNv'></ul><sub id='DyhNv'></sub></form><legend id='DyhNv'></legend><bdo id='DyhNv'><pre id='DyhNv'><center id='DyhNv'></center></pre></bdo></b><th id='DyhNv'></th></span></q></dt></tr></i><div id='DyhNv'><tfoot id='DyhNv'></tfoot><dl id='DyhNv'><fieldset id='DyhNv'></fieldset></dl></div>

                      • <bdo id='DyhNv'></bdo><ul id='DyhNv'></ul>

                    1. <small id='DyhNv'></small><noframes id='DyhNv'>

                      <tfoot id='DyhNv'></tfoot>