SparkleXrm icon indicating copy to clipboard operation
SparkleXrm copied to clipboard

DataContractJsonSerializer and oDataV4 from with CRM Plugin

Open twiga2013 opened this issue 5 years ago • 3 comments

Hi Scott,

I want to use the DataContactJsonSerializer from within CRM Plugin, where I need to serialize a payload from a web service that exposes OData V4. I am having difficulty serializing the payload.

How do I go about serializing without bringing in dependency to NewtonSoft Json?

Thanks

twiga2013 avatar Oct 20 '19 14:10 twiga2013

Here is some example code:

public static string SerialiseEntityToJSON(this Entity entity)
{
    var setting = new DataContractJsonSerializerSettings()
    {
        UseSimpleDictionaryFormat = true,
        KnownTypes = new Type[] {typeof(ActivityParty)}
    };
    var entityTyped = entity.ToEntity<Entity>();
    var serializer = new DataContractJsonSerializer(typeof(Entity), setting);
    using (var ms = new MemoryStream())
    {
        serializer.WriteObject(ms, entityTyped);
        ms.Position = 0;
        var sr = new StreamReader(ms);
        return sr.ReadToEnd();
    }
}

scottdurow avatar Oct 20 '19 16:10 scottdurow

I have used the same pattern for serializing but the serialized but returned is null. I am trying to serialize OData v4 response

twiga2013 avatar Oct 20 '19 18:10 twiga2013

Here is an example of a response I am trying to serialize using the DataContractJsonSerializer

{ @odata.context: "http://services.odata.org/V4/(S(tdsqxrjntoi2ta4i1nqyrtm3))/TripPinServiceRW/$metadata", value: [ { name: "Photos", kind: "EntitySet", url: "Photos" }, { name: "People", kind: "EntitySet", url: "People" }, { name: "Airlines", kind: "EntitySet", url: "Airlines" }, { name: "Airports", kind: "EntitySet", url: "Airports" }, { name: "Me", kind: "Singleton", url: "Me" }, { name: "GetNearestAirport", kind: "FunctionImport", url: "GetNearestAirport" } ] }

twiga2013 avatar Oct 21 '19 11:10 twiga2013