私はあなたのMyJsonConverter
が実際にしかしとして何をするかわかりません基本的な例は、あなただけのJsonSerializerSettings
を提供する必要があり、この単純な例では、作品TypeNameHandling.All
にTypeNameHandling
プロパティを設定する必要があります。
public interface IControl { }
public class Form
{
public IList<IControl> Controls { get; set; }
}
public class ControlA : IControl { }
public class ControlB : IControl { }
static void Main(string[] args)
{
var form = new Form();
form.Controls = new List<IControl>();
form.Controls.Add(new ControlA());
form.Controls.Add(new ControlB());
var json = JsonConvert.SerializeObject(form, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
var obj = JsonConvert.DeserializeObject<Form>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
}
編集
なしタイプの取り扱いは、したがって、我々はもう少し創造的な取得と生jSON
を読む必要が全く$type
プロパティを使用していないされていないことを明確化した後。そして、特別な方法でオブジェクトを構築します。次に、シリアライザの例を示します。
internal class MyJsonConverter : CustomCreationConverter<IControl>
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jObject = JObject.Load(reader);
var controlType = jObject["CustomProperty"]?.Value<string>();
IControl control = null;
if (!string.IsNullOrWhiteSpace(controlType))
{
switch (controlType.ToLowerInvariant())
{
case "controla":
control = Activator.CreateInstance(typeof(ControlA)) as IControl;
break;
case "controlb":
control = Activator.CreateInstance(typeof(ControlB)) as IControl;
break;
}
}
if (controlType == null)
throw new SerializationException($"Unable to deserialize property. {controlType}");
serializer.Populate(jObject.CreateReader(), control);
return control;
}
public override IControl Create(Type objectType)
{
return null;
}
}
基本的に我々は(問題のommitedされています)IControl
インタフェースのプロパティにdepndingされているとして、我々は、手動でJSONを解析し、このプロパティはexsits場合はプロパティにCustomProperty
を参照を取得します有効な文字列値で(または他の値を使用することもできます)、IControl
を手動で作成します。
最後のデシリアライズを扱う作品は最終ラインserializer.Populate()
完全なテストケースである:
internal class MyJsonConverter : CustomCreationConverter<IControl>
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jObject = JObject.Load(reader);
var controlType = jObject["CustomProperty"]?.Value<string>();
IControl control = null;
if (!string.IsNullOrWhiteSpace(controlType))
{
switch (controlType.ToLowerInvariant())
{
case "controla":
control = Activator.CreateInstance(typeof(ControlA)) as IControl;
break;
case "controlb":
control = Activator.CreateInstance(typeof(ControlB)) as IControl;
break;
}
}
if (controlType == null)
throw new SerializationException($"Unable to deserialize property. {controlType}");
serializer.Populate(jObject.CreateReader(), control);
return control;
}
public override IControl Create(Type objectType)
{
return null;
}
}
[JsonConverter(typeof(MyJsonConverter))]
public interface IControl
{
string CustomProperty { get; set; }
}
public class Form
{
public IList<IControl> Controls { get; set; }
}
public class ControlA : IControl
{
public string CustomProperty { get; set; } = "ControlA";
}
public class ControlB : IControl
{
public string CustomProperty { get; set; } = "ControlB";
}
static void Main(string[] args)
{
var form = new Form();
form.Controls = new List<IControl>();
form.Controls.Add(new ControlA());
form.Controls.Add(new ControlB());
var json = JsonConvert.SerializeObject(form);
var obj = JsonConvert.DeserializeObject<Form>(json);
}
それはまさにそれです!ありがとうございました。 – UserControl