Пример #1
0
        /// <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&gt;string, bool&lt; foundAttributes;
        /// var collection = jsonDocument.ToObject(Activator.CreateInstance&lt;ModelTypeApiResource&gt;(), typeof(IEnumerable&lt;ModelType&gt;, out foundAttributes));
        /// var singleItem = jsonDocument.ToObject(Activator.CreateInstance&lt;ModelTypeApiResource&gt;(), 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));
        }
Пример #2
0
 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));
 }
Пример #3
0
 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));
 }