SparkleXrm
SparkleXrm copied to clipboard
DataContractJsonSerializer and oDataV4 from with CRM Plugin
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
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();
}
}
I have used the same pattern for serializing but the serialized but returned is null. I am trying to serialize OData v4 response
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" } ] }