我有一个包含JSON对象数组的流,它有两种不同的格式,但包含相同类型的数据,我想将这两种格式反序列化为相同的类型,因此我的视图不需要自定义逻辑两种格式的数据。目前我正在使用自定义JsonConverter处理它。
这是我的模特:
[JsonObject]
[JsonConverter(typeof(MyCommonObjectJsonConverter))]
public class MyCommonObject {
// some common fields, e.g.
public String Id { get; set; }
public string Text { get; set; }
}
这是我的自定义JsonConverter:
public class MyCommonObjectJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// don't need to worry about serialization in this case, only
// reading data
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jObject = JObject.Load(reader);
MyCustomObject result;
if (IsFormatOne(jObject))
{
// the structure of the object matches the first format,
// so just deserialize it directly using the serializer
result = serializer.Deserialize<MyCustomObject>(reader);
}
else if (IsFormatTwo(jObject))
{
result = new MyCustomObject();
// initialize values from the JObject
// ...
}
else
{
throw new InvalidOperationException("Unknown format, cannot deserialize");
}
return result;
}
public override bool CanConvert(Type objectType)
{
return typeof(MyCustomObject).IsAssignableFrom(objectType);
}
// Definitions of IsFormatOne and IsFormatTwo
// ...
}
然而,当我反序列化第一个格式的对象时,我得到一个错误,说它无法加载JObject,因为JsonReader有TokenType“EndToken”。我不确定为什么会发生这种情况,我加载的数据格式正确。关于我应该注意什么的任何建议?
答案 0 :(得分:2)
您希望在阅读MyCommonObject
时回退到默认反序列化,但是:
JObject.Load(reader)
的调用已经使读者超越了对象,并且serializer.Deserialize<MyCustomObject>(reader)
无论如何都会导致无限递归。在这种情况下,ReadJson()
中避免不必要的递归的惯用方法是手动分配结果,然后调用serializer.Populate(jObject.CreateReader(), result)
。即:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
JObject jObject = JObject.Load(reader);
MyCommonObject result;
if (IsFormatOne(jObject))
{
result = (existingValue as MyCommonObject ?? (MyCommonObject)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator()); // Reuse existingValue if present
// the structure of the object matches the first format,
// so just deserialize it directly using the serializer
using (var subReader = jObject.CreateReader())
serializer.Populate(subReader, result);
}
else if (IsFormatTwo(jObject))
{
result = (existingValue as MyCommonObject ?? (MyCommonObject)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator());
// initialize values from the JObject
// ...
}
else
{
throw new InvalidOperationException("Unknown format, cannot deserialize");
}
return result;
}
答案 1 :(得分:0)
在写这篇文章时把它弄清楚,调用serializer.Deserialize<MyCustomObject>(reader)
再次递归到我的转换器中,此时读者将从加载到JObject中到达结束标记,使得TokenType等于EndToken。我应该更仔细地检查堆栈跟踪。现在我要为两种格式编写自定义初始化逻辑,使我的模型类与格式无关。