如何用NewtonSoft的Json序列化器替换OData V4默认的Json序列化器? [英] How do I replace the OData V4 default Json Serializer with NewtonSoft's Json Serializer?
问题描述
我有一个包含DynamicObjects列表的类.我有一个单元测试,可以确认Newtonsoft Json Serializer/Deserializer可以正确处理此问题.但是,默认的OData Json序列化器/反序列化器则没有.
I have a class that contains a List of DynamicObjects. I have a unit test that confirms that the Newtonsoft Json Serializer/Deserializer handles this correctly. However, the default OData Json Serializer/Deserializer does not.
我实现了自己的ODataEdmTypeDeserializer,如下所示:
I implemented my own ODataEdmTypeDeserializer like this:
public class JsonODataEdmTypeDeserializer : ODataEdmTypeDeserializer
{
public JsonODataEdmTypeDeserializer(ODataPayloadKind payloadKind) : base(payloadKind)
{
}
public JsonODataEdmTypeDeserializer(ODataPayloadKind payloadKind, ODataDeserializerProvider deserializerProvider) : base(payloadKind, deserializerProvider)
{
}
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
{
var data = readContext.Request.Content.ReadAsStringAsync().Result;
//Call to the NewtonSoft Deserializer
var ret = JsonConvert.DeserializeObject(data, type);
return ret;
}
}
连同它的DefaultODataDeserializerProvider:
along with it's DefaultODataDeserializerProvider:
public class JsonODataDeserializerProvider : DefaultODataDeserializerProvider
{
public override ODataEdmTypeDeserializer GetEdmTypeDeserializer(IEdmTypeReference edmType)
{
var kind = GetODataPayloadKind(edmType);
return new JsonODataEdmTypeDeserializer(kind, this);
}
private static ODataPayloadKind GetODataPayloadKind(IEdmTypeReference edmType)
{
switch (edmType.TypeKind())
{
case EdmTypeKind.Entity:
return ODataPayloadKind.Entry;
case EdmTypeKind.Primitive:
case EdmTypeKind.Complex:
return ODataPayloadKind.Property;
case EdmTypeKind.Collection:
IEdmCollectionTypeReference collectionType = edmType.AsCollection();
return collectionType.ElementType().IsEntity() ? ODataPayloadKind.Feed : ODataPayloadKind.Collection;
default:
return ODataPayloadKind.Entry;
}
}
}
这些工作正常,但是当我尝试创建自己的序列化实现时,遇到了一个障碍:
These work correctly, however when I tried to create my own Serialize implementation I ran into a roadblock:
public class JsonODataEntityTypeSerializer : ODataEntityTypeSerializer
{
public JsonODataEntityTypeSerializer(ODataSerializerProvider serializerProvider)
: base(serializerProvider)
{
}
public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
{
}
当我的控制器试图返回有问题的对象时,会调用
WriteObject,但是我不确定在这里该如何插入Newtonsoft Serializer.我下载了OData源代码并仔细检查了一下,但没有看到需要的钩子.
WriteObject gets called when my controller tries to return the object in question, but I'm not sure what to do here to insert the Newtonsoft Serializer. I downloaded the OData source code and looked through it but I'm not seeing the hooks I need.
推荐答案
您必须创建一个自定义DataWriter,例如NewtonsoftJsonDataWriter:ODataWriter.
You have to create a custom DataWriter, for example NewtonsoftJsonDataWriter:ODataWriter.
在那里看看:在示例中,它是一个实现的Csv编写器,我认为您随后可以使用简单的Json.Convert()覆盖其方法WriteStart,WriteHeader,WriteEntry和WriteEnd.
In the example it's a Csv writer which is implemented, I think you'll then be able to override its methods WriteStart, WriteHeader, WriteEntry and WriteEnd with a simple Json.Convert().
这篇关于如何用NewtonSoft的Json序列化器替换OData V4默认的Json序列化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!