Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON.Net: deserializing polymorphic types without specifying the assembly
    primarykey
    data
    text
    <p>I see that using JSON.Net, I can decode polymorphic objects if a <code>$type</code> attribute specifies the specific type of the JSON object. In all the examples I've seen, <code>$type</code> 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.</p> <p>I am able to deserialize the JSON using:</p> <pre><code>public class SingleAssemblyJsonTypeBinder : SerializationBinder { private readonly Assembly _assembly; private Dictionary&lt;string, Type&gt; _typesBySimpleName = new Dictionary&lt;string, Type&gt;(StringComparer.OrdinalIgnoreCase); private Dictionary&lt;Type,string&gt; _simpleNameByType = new Dictionary&lt;Type, string&gt;(); public SingleAssemblyJsonTypeBinder(Assembly assembly) { _assembly = assembly; _typesBySimpleName = new Dictionary&lt;string, Type&gt;(); foreach (var type in _assembly.GetTypes().Where(t =&gt; 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; } } } </code></pre> <p>...</p> <pre><code>public static JsonSerializerSettings GetJsonSerializationSettings() { var settings = new JsonSerializerSettings(); settings.Binder = new SingleAssemblyJsonTypeBinder(typeof(MvcApplication).Assembly); settings.TypeNameHandling = TypeNameHandling.Objects; return settings; } </code></pre> <p>...</p> <pre><code>var serializer = JsonSerializer.Create(settings); </code></pre> <p>I haven't been able to make this work with MVC though. I'm configuring JSON deserialization per the code below in <code>Application_Start</code>, and the object is deserialized, but using the base type one.</p> <pre><code>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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload