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;
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.
user1892134
source share