コード例 #1
0
        public JsonApiCollectionDocument(IEnumerable <T> data, JsonApiErrors errors, object metadata = null, bool hasLinks = false, bool hasRelationships = false, bool hasIncludedData = false, Func <T, string> customIdResolver = null)
        {
            if (customIdResolver == null)
            {
                if (typeof(ICustomJsonModelId).IsAssignableFrom(typeof(T)))
                {
                    customIdResolver = d => (d as ICustomJsonModelId)?.CustomJsonModelId;
                }
                else
                {
                    customIdResolver = d => d.Id.ToString();
                }
            }
            Meta   = metadata;
            Errors = errors;
            if (data == null)
            {
                Data     = null;
                Included = null;
            }
            else
            {
                Data = new List <JsonApiResourceObject <T> >();

                foreach (var item in data)
                {
                    JsonClassAttribute classAttrib = item.GetType().GetCustomAttributes(typeof(JsonClassAttribute), false).FirstOrDefault() as JsonClassAttribute;
                    if (classAttrib == null)
                    {
                        throw new Exception("No class attribute specified for " + item.GetType().Name + ".");
                    }
                    var newDataItem = new JsonApiResourceObject <T>(customIdResolver(item), classAttrib.Name, item);
                    if (hasRelationships)
                    {
                        newDataItem.Relationships = new JsonApiRelationshipsObject();
                    }
                    Data.Add(newDataItem);
                }

                //TODO :replace is a hack to remove the "Model"-Part from the name, solve this as parameter in the future
                Included = !hasIncludedData ? null : new JsonApiInclusionCollection();//new List<JsonApiResourceObject>();
            }

            Links = hasLinks ? new JsonApiLinksObject() : null;
        }
コード例 #2
0
        public IEnumerable <JsonApiResourceObject <T> > ExtractFromIncludes <T>() where T : JsonBaseModel
        {
            // create container for storing the result set
            List <JsonApiResourceObject <T> > results = new List <JsonApiResourceObject <T> >();

            // gather meta info
            Type   modeltype    = typeof(T);
            var    classattribs = modeltype.GetCustomAttributes(typeof(JsonClassAttribute), false);
            string modelName    = (classattribs?.FirstOrDefault() as JsonClassAttribute)?.Name;

            //find relevant attributes in includes
            var data = Included.Where(x => x.Type.Equals(modelName));

            //try to convert the included attributes to datamodels
            foreach (var resource in data)
            {
                string jsonString = resource.Attributes as string;
                JsonApiResourceObject <T> model = null;
                try
                {
                    model = Newtonsoft.Json.Linq.JObject.Parse(jsonString)?.ToObject <JsonApiResourceObject <T> >();
                }
                catch
                {
                }
                if (model == null)
                {
                    continue;
                }
                //int id = -1;
                //int.TryParse(resource.Id, out id);
                //model.Id = id;
                //var resourceT = new JsonApiResourceObject<T>(resource.Id, resource.Type, model);
                //resourceT.Links = resource.Links;
                //resourceT.Relationships = resource.Relationships;
                results.Add(model);
            }

            return(results);
        }