Ejemplo n.º 1
0
        public static IJsonApiDataModel ToObject(this JsonApiResourceIdentifierObject resourceIdentifierObject, Type type)
        {
            IJsonApiDataModel result = null;

            try
            {
                result = Activator.CreateInstance(type) as IJsonApiDataModel;
                result.SetIdFromString(resourceIdentifierObject.Id);
            }
            catch { }

            return(result);
        }
Ejemplo n.º 2
0
        public static JsonApiResourceObject Build(IJsonApiDataModel data, bool processRelations)
        {
            if (data == null)
            {
                throw new Exception("data is null");
            }

            JsonApiResourceObject result = new JsonApiResourceObject();

            result.Id   = data.GetIdAsString();
            result.Type = data.GetJsonApiClassName();

            var classType = data.GetType();

            // sort out properties
            var propertyInfos       = classType.GetProperties();
            var propoertyInfoLookup = propertyInfos.ToLookup(p => GetAttributeGroup(p));

            result.Attributes = new Dictionary <string, object>();
            foreach (var item in propoertyInfoLookup[AttributeGroup.Primitive])
            {
                var value = item.GetValueFast(data);
                if (value == null && item.JsonIsIgnoredIfNull())
                {
                    continue;
                }
                result.Attributes.Add(StringUtils.GetAttributeName(item), value);
            }

            foreach (var item in propoertyInfoLookup[AttributeGroup.PrimitiveCollection])
            {
                var value = item.GetValueFast(data);
                if (value == null && item.JsonIsIgnoredIfNull())
                {
                    continue;
                }
                result.Attributes.Add(StringUtils.GetAttributeName(item), value);
            }

            if (result.Attributes.Count < 1)
            {
                result.Attributes = null;
            }


            if (!processRelations)
            {
                return(result);
            }



            Dictionary <string, RelationShipInfo> relationships = new Dictionary <string, RelationShipInfo>();

            foreach (var item in propoertyInfoLookup[AttributeGroup.Reference])
            {
                var value = item.GetValueFast(data) as IJsonApiDataModel;
                var info  = new RelationShipInfo {
                    RelationshipKey = StringUtils.GetRelationShipName(item), RelationshipType = value.GetJsonApiClassName()
                };
                if (value != null)
                {
                    info.RelationshipIds.Add(value.GetIdAsString());
                }
                relationships.Add(item.Name, info);
            }

            foreach (var item in propoertyInfoLookup[AttributeGroup.ReferenceCollection])
            {
                var value = item.GetValueFast(data) as IEnumerable <IJsonApiDataModel>;
                var info  = new RelationShipInfo
                {
                    RelationshipKey  = StringUtils.GetRelationShipName(item),
                    RelationshipType = item.PropertyType.GetGenericArguments()[0].GetJsonApiClassName()
                };
                if (value != null)
                {
                    foreach (var relation in value)
                    {
                        info.RelationshipIds.Add(relation.GetIdAsString());
                    }
                }
                relationships.Add(item.Name, info);
            }

            foreach (var item in propoertyInfoLookup[AttributeGroup.PrimitiveId])
            {
                var value = item.GetValueFast(data);
                if (value == null)
                {
                    continue;
                }
                RelationShipInfo info = null;
                var idAttr            = item.GetCustomAttribute <JsonApiRelationIdAttribute>();
                if (!relationships.TryGetValue(idAttr.PropertyName, out info))
                {
                    throw new Exception($"no reference found for id backing field {idAttr.PropertyName}");
                }
                info.RelationshipIds.Add(value.ToString());
            }

            foreach (var item in propoertyInfoLookup[AttributeGroup.PrimitiveIdCollection])
            {
                var value = item.GetValueFast(data) as IEnumerable;
                if (value == null)
                {
                    continue;
                }
                RelationShipInfo info = null;
                var idAttr            = item.GetCustomAttribute <JsonApiRelationIdAttribute>();
                if (!relationships.TryGetValue(idAttr.PropertyName, out info))
                {
                    throw new Exception($"no reference found for id backing field {idAttr.PropertyName}");
                }
                foreach (var val in value)
                {
                    info.RelationshipIds.Add(val.ToString());
                }
            }

            result.Relationships = new Dictionary <string, JsonApiRelationshipObjectBase>();

            foreach (var r in relationships)
            {
                if (r.Value.RelationshipIds.Count < 1)
                {
                    continue;
                }
                if (r.Value.RelationshipIds.Count == 1)
                {
                    var relO = new JsonApiToOneRelationshipObject();
                    relO.Data = new JsonApiResourceIdentifierObject {
                        Id = r.Value.RelationshipIds.First(), Type = r.Value.RelationshipType
                    };
                    result.Relationships.Add(r.Value.RelationshipKey, relO);
                }
                else
                {
                    var relO = new JsonApiToManyRelationshipObject();
                    relO.Data = r.Value.RelationshipIds.Select(i => new JsonApiResourceIdentifierObject {
                        Id = i, Type = r.Value.RelationshipType
                    }).ToList();
                    result.Relationships.Add(r.Value.RelationshipKey, relO);
                }
            }
            if (result.Relationships.Count < 1)
            {
                result.Relationships = null;
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts the resource to a object.
        /// </summary>
        /// <returns></returns>
        public static IJsonApiDataModel ToObject(this JsonApiResourceObject resourceObject, Type t, bool processRelations = true)
        {
            IJsonApiDataModel result = null;

            // try to create object from attributes
            try
            {
                if (resourceObject.Attributes == null)
                {
                    result = Activator.CreateInstance(t) as IJsonApiDataModel;
                }
                else
                {
                    result = JObject.FromObject(resourceObject.Attributes).ToObject(t) as IJsonApiDataModel;
                }
                result.SetIdFromString(resourceObject.Id);
            }
            catch { }
            if (!processRelations || result == null || resourceObject.Relationships == null || resourceObject.Relationships.Count < 1)
            {
                return(result);
            }

            var idPropertyDict = t.GetProperties().Where(p => p.GetCustomAttribute <JsonApiRelationIdAttribute>() != null).ToDictionary(k => k.GetCustomAttribute <JsonApiRelationIdAttribute>().PropertyName, v => v);
            Dictionary <string, PropertyInfo> refPropertyDict = null;
            Dictionary <string, string>       refKeyToName    = null;

            {
                var refProperties = t.GetProperties().Where(p => typeof(IJsonApiDataModel).IsAssignableFrom(p.PropertyType) || typeof(IEnumerable <IJsonApiDataModel>).IsAssignableFrom(p.PropertyType));
                refPropertyDict = refProperties.ToDictionary(k => StringUtils.GetRelationShipName(k), v => v);
                refKeyToName    = refProperties.ToDictionary(k => StringUtils.GetRelationShipName(k), v => v.Name);
            }

            foreach (var relation in resourceObject.Relationships)
            {
                if (refKeyToName.TryGetValue(relation.Key, out var propName))
                {
                    if (idPropertyDict.TryGetValue(propName, out var propertyInfo))
                    {
                        if (propertyInfo.PropertyType is IEnumerable)
                        {
                            var relationConcrete = relation.Value as JsonApiToManyRelationshipObject;
                            if (relationConcrete.Data != null)
                            {
                                propertyInfo.SetValue(result, relationConcrete.Data.Select(r => StringUtils.ConvertId(r.Id, propertyInfo.PropertyType)));
                            }
                        }
                        else
                        {
                            var relationConcrete = relation.Value as JsonApiToOneRelationshipObject;
                            if (relationConcrete.Data != null)
                            {
                                propertyInfo.SetValue(result, StringUtils.ConvertId(relationConcrete.Data.Id, propertyInfo.PropertyType));
                            }
                        }
                    }
                }

                if (refPropertyDict.TryGetValue(relation.Key, out var refInfo))
                {
                    if (refInfo.PropertyType.IsNonStringEnumerable())
                    {
                        var innerType           = refInfo.PropertyType.GenericTypeArguments[0];
                        var constructedListType = typeof(List <>).MakeGenericType(innerType);
                        var collection          = Activator.CreateInstance(constructedListType) as IList;
                        var relationConcrete    = relation.Value as JsonApiToManyRelationshipObject;
                        foreach (var r in relationConcrete.Data)
                        {
                            var item = Activator.CreateInstance(innerType) as IJsonApiDataModel;
                            item.SetIdFromString(r?.Id);
                            collection.Add(item);
                        }
                        refInfo.SetValue(result, collection);
                    }
                    else
                    {
                        var relationConcrete = relation.Value as JsonApiToOneRelationshipObject;
                        var item             = Activator.CreateInstance(refInfo.PropertyType) as IJsonApiDataModel;
                        item.SetIdFromString(relationConcrete.Data.Id);
                        refInfo.SetValue(result, item);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
 public static JsonApiResourceObject FromObject(this JsonApiResourceObject resourceObject, IJsonApiDataModel dataModel, bool automaticallyProcessAllRelations = true)
 {
     return(JsonApiResourceBuilder.Build(dataModel, automaticallyProcessAllRelations));
 }
Ejemplo n.º 5
0
        public static void SetIdFromString(this IJsonApiDataModel obj, string stringId)
        {
            var info = obj.GetType().GetProperty("Id");

            info.SetValueFast(obj, BtbrdCoreIdConverters.ConvertFromString(stringId, info.PropertyType));
        }
Ejemplo n.º 6
0
        public static string GetIdAsString(this IJsonApiDataModel obj)
        {
            var info = obj.GetType().GetProperty("Id");

            return(BtbrdCoreIdConverters.ConvertToString(info.GetValueFast(obj)));
        }
Ejemplo n.º 7
0
 public static string GetJsonApiClassName(this IJsonApiDataModel obj)
 {
     return(obj.GetType().GetJsonApiClassName());
 }