在Json.net使用自定义JsonConverter和TypeNameHandling [英] Using custom JsonConverter and TypeNameHandling in Json.net
问题描述
我有一样的接口类型属性的类:
I have a class with an interface-typed property like:
public class Foo
{
public IBar Bar{get;set;}
}
我也有多种具体实现在的伊巴尔
接口,可以在运行时设置。有些具体类的需要系列化和放大器定制JsonConverter;反序列化。
I also have multiple concrete implementations of the IBar
interface that can be set at runtime. Some of these concrete classes require a custom JsonConverter for serialization & deserialization.
利用 TypeNameHandling.Auto
选项不要求转换伊巴尔
类可以被序列化和反序列化完美。在另一方面,自定义序列化类没有 $类型
名称输出,同时预期它们将被序列,它们不能被反序列化到自己的具体类型。
Utilizing the TypeNameHandling.Auto
option the non-convertor requiring IBar
classes can be serialized and deserialized perfectly. The custom-serialized classes on the other hand have no $type
name output and while they are serialized as expected, they cannot be deserialized to their concrete type.
我试图写了 $类型
名称自定义内的元数据自己 JsonConverter
;然而,在反序列化转换器被当时正在完全绕过。
I attempted to write-out the $type
name metadata myself within the custom JsonConverter
; however, on deserialization the converter is then being bypassed entirely.
有没有解决办法或处理这种情况正确方法?
Is there a workaround or proper way of handling such a situation?
推荐答案
我解决了类似的问题,我发现了一个解决方案。这是不是很优雅,我觉得应该有一个更好的办法,但至少它的作品。所以我的想法是让 JsonConverter
每一类型实现伊巴尔
和一个变换器伊巴尔
本身。
I solved the similar problem and I found a solution. It's not very elegant and I think there should be a better way, but at least it works. So my idea was to have JsonConverter
per each type that implements IBar
and one converter for IBar
itself.
因此,让我们从模型开始:
So let's start from models:
public interface IBar { }
public class BarA : IBar { }
public class Foo
{
public IBar Bar { get; set; }
}
现在让我们创建转换器伊巴尔
。它将被用于反序列化只有当JSON。它会试图读取 $类型
变量,并调用转换器实现类型:
Now let's create converter for IBar
. It will be used only when deserializing JSON. It will try to read $type
variable and call converter for implementing type:
public class BarConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotSupportedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jObj = JObject.Load(reader);
var type = jObj.Value<string>("$type");
if (type == GetTypeString<BarA>())
{
return new BarAJsonConverter().ReadJson(reader, objectType, jObj, serializer);
}
// Other implementations if IBar
throw new NotSupportedException();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof (IBar);
}
public override bool CanWrite
{
get { return false; }
}
private string GetTypeString<T>()
{
var typeOfT = typeof (T);
return string.Format("{0}, {1}", typeOfT.FullName, typeOfT.Assembly.GetName().Name);
}
}
和这是转换器巴拉
类:
public class BarAJsonConverter : BarBaseJsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// '$type' property will be added because used serializer has TypeNameHandling = TypeNameHandling.Objects
GetSerializer().Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var existingJObj = existingValue as JObject;
if (existingJObj != null)
{
return existingJObj.ToObject<BarA>(GetSerializer());
}
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(BarA);
}
}
您可能会注意到它继承自 BarBaseJsonConverter
类,而不是 JsonConverter
。同时,我们也不会在 WriteJson
和 ReadJson
串行参数>方法。有使用定制的转换器在串行
参数有问题。你可以阅读更多 href=\"http://stackoverflow.com/questions/12314438/self-referencing-loop-in-json-net-jsonserializer-from-custom-jsonconverter-web\">。我们需要创建新实例 JsonSerializer
和基类是一个很好的候选人:
You may notice that it's inherited from BarBaseJsonConverter
class, not JsonConverter
. And also we do not use serializer
parameter in WriteJson
and ReadJson
methods. There is a problem with using serializer
parameter inside custom converters. You can read more here. We need to create new instance of JsonSerializer
and base class is a good candidate for that:
public abstract class BarBaseJsonConverter : JsonConverter
{
public JsonSerializer GetSerializer()
{
var serializerSettings = JsonHelper.DefaultSerializerSettings;
serializerSettings.TypeNameHandling = TypeNameHandling.Objects;
var converters = serializerSettings.Converters != null
? serializerSettings.Converters.ToList()
: new List<JsonConverter>();
var thisConverter = converters.FirstOrDefault(x => x.GetType() == GetType());
if (thisConverter != null)
{
converters.Remove(thisConverter);
}
serializerSettings.Converters = converters;
return JsonSerializer.Create(serializerSettings);
}
}
JsonHelper
只是一个类来创建 JsonSerializerSettings
:
public static class JsonHelper
{
public static JsonSerializerSettings DefaultSerializerSettings
{
get
{
return new JsonSerializerSettings
{
Converters = new JsonConverter[] { new BarConverter(), new BarAJsonConverter() }
};
}
}
}
现在将工作和你仍然可以使用自定义转换器序列化和反序列化:
Now it will work and you still can use your custom converters for both serialization and deserialization:
var obj = new Foo { Bar = new BarA() };
var json = JsonConvert.SerializeObject(obj, JsonHelper.DefaultSerializerSettings);
var dObj = JsonConvert.DeserializeObject<Foo>(json, JsonHelper.DefaultSerializerSettings);
这篇关于在Json.net使用自定义JsonConverter和TypeNameHandling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!