public override Entity ReadJson(JsonReader reader, Type objectType, [AllowNull] Entity existingValue, bool hasExistingValue, JsonSerializer serializer) { if (hasExistingValue) { throw new NotImplementedException(); } var jObject = JObject.Load(reader); var dictionary = jObject.ToObject <Dictionary <string, object> >(); var entity = ODataResponseReader.ReadEntity(dictionary, _metadata, serializer); return(entity); }
public override EntityCollection ReadJson(JsonReader reader, Type objectType, [AllowNull] EntityCollection existingValue, bool hasExistingValue, JsonSerializer serializer) { if (hasExistingValue) { throw new NotImplementedException(); } var jObject = JObject.Load(reader); var collection = new EntityCollection(); if (jObject.TryGetValue("@odata.count", out var count)) { collection.Count = count.ToObject <int>(); } if (jObject.TryGetValue("@odata.nextLink", out var nextLink)) { collection.NextLink = nextLink.ToObject <Uri>(); } if (jObject.TryGetValue("@Microsoft.Dynamics.CRM.fetchxmlpagingcookie", out var pagingcookie)) { var xmlPagingCookie = System.Net.WebUtility.UrlDecode(pagingcookie.ToString()); var xmlDoc = System.Xml.Linq.XDocument.Parse(xmlPagingCookie); if (xmlDoc.Document?.Root?.Attribute("pagingcookie") != null) { collection.PagingCookie = System.Net.WebUtility.UrlDecode(xmlDoc.Document.Root.Attribute("pagingcookie")?.Value); } } if (jObject.TryGetValue("@odata.context", out var context) && ODataResponseReader.TryParseCollectionName(context.ToString(), out var collectionName)) { var entityMd = _metadata.GetEntityMetadata(x => x.EntitySetName == collectionName); collection.EntityName = entityMd.LogicalName; } var valueArr = jObject["value"]; if (valueArr == null) { return(collection); } foreach (var item in valueArr) { if (item is JObject jObjectEntity) { if (context != null) { jObjectEntity.Add("@odata.context", context); } var entity = jObjectEntity.ToObject <Entity>(serializer); collection.Entities.Add(entity); } } return(collection); }