How to replace standard Json Serializer OData V4 with NewtonSoft Json Serializer?

I have a class containing a list of DynamicObjects. I have a unit test that confirms that Newtonsoft Json Serializer / Deserializer is handling this correctly. However, by default, OData Json Serializer / Deserializer does not.

I applied my own ODataEdmTypeDeserializer as follows:

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; } } 

along with it 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; } } } 

They work correctly, however, when I tried to create my own implementation of Serialize, I ran into roadblock:

 public class JsonODataEntityTypeSerializer : ODataEntityTypeSerializer { public JsonODataEntityTypeSerializer(ODataSerializerProvider serializerProvider) : base(serializerProvider) { } public override void WriteObject(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext) { } 

WriteObject is 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 do not see the required hooks.

+7
c # serialization odata deserialization
source share
2 answers

You need to create a custom DataWriter, such as NewtonsoftJsonDataWriter: ODataWriter.

Take a look there: tutorial-sample-odatalib-custom-payload-format

In the example, this is implemented by Csv-writer, I think that you can override its WriteStart, WriteHeader, WriteEntry and WriteEnd methods with a simple Json.Convert ().

+2
source share

I looked at it before, I do not think it is possible. It seems that it is not possible to add an XML serializer: - (

This is possible on Web Api, though

+1
source share

All Articles