/// <summary> /// Extract primary Data from the JsonApiDocument. /// </summary> /// <param name="apiResource">instance of JsonApiResource used to extract the Data.</param> /// <param name="targetType">The Type of the extracted Data.</param> /// <param name="foundAttributes">A function to determine which attributes were found in the JsonDocument's primary data</param> /// <returns>An instance containing the model data.</returns> /// <example> /// <code> /// Func>string, bool< foundAttributes; /// var collection = jsonDocument.ToObject(Activator.CreateInstance<ModelTypeApiResource>(), typeof(IEnumerable<ModelType>, out foundAttributes)); /// var singleItem = jsonDocument.ToObject(Activator.CreateInstance<ModelTypeApiResource>(), typeof(ModelType), out foundAttributes); /// </code> /// </example> public static object ToObjectInternal(this JsonApiDocument document, JsonApiResource apiResource, Type targetType, out Func <int, string, bool> foundAttributes) { var attrs = document.Data.Attributes; var relations = document.Data.Relationships; foundAttributes = (idx, attrName) => (attrs?.ContainsKey(attrName.ToJsonAttributeName()) ?? false) || (relations?.ContainsKey(attrName.ToRelationshipName(apiResource.GetType())) ?? false); return(document.ToObject(apiResource, targetType)); }
public static object ToObjectCollection(this JsonApiDocument document, JsonApiResource apiResource, Type targetType, out Func <string, bool> foundAttributes) { foundAttributes = (attrName) => (document.Data.Attributes?.ContainsKey(attrName.ToJsonAttributeName()) ?? false) || (document.Data.Relationships?.ContainsKey(attrName.ToRelationshipName(apiResource.GetType())) ?? false); return(document.ToObjectCollection(apiResource, targetType)); }
internal static object ToObjectInternal(this JsonApiCollectionDocument document, JsonApiResource apiResource, Type targetType, out Func <int, string, bool> foundAttributes) { foundAttributes = (idx, attrName) => (document.Data.ElementAt(idx)?.Attributes?.ContainsKey(attrName.ToJsonAttributeName()) ?? false) || (document.Data.ElementAt(idx)?.Relationships?.ContainsKey(attrName.ToRelationshipName(apiResource.GetType())) ?? false); return(document.ToObjectInternal(apiResource, targetType)); }