public ResourceDocument(List <Error> errors, IJsonApiProfile profile)
 {
     _profile             = profile;
     _innerExpando        = new ExpandoObject();
     _innerExpandoDict    = _innerExpando;
     _innerExpando.Errors = errors;
 }
 public ResourceDocument(ResourceObject data, IJsonApiProfile profile)
 {
     _profile           = profile;
     _innerExpando      = new ExpandoObject();
     _innerExpandoDict  = _innerExpando;
     _innerExpando.Data = data;
     ExtractIncludedLinks(data);
 }
        public ResourceDocument(List <ResourceObject> data, IJsonApiProfile profile)
        {
            _profile          = profile;
            _innerExpando     = new ExpandoObject();
            _innerExpandoDict = _innerExpando;

            ValidateResourceObjectCollectionSameType(data);
            ValidateResourceObjectCollectionUniqueness(data);
            data.ForEach(ExtractIncludedLinks);

            _innerExpando.Data = data;
        }
Exemplo n.º 4
0
        public ResourceObject ToResourceObject(IJsonApiProfile profile)
        {
            var resource = new ResourceObject(Obj, profile);

            if (Metadata != null && Metadata.Any())
            {
                var metaExpando = (IDictionary <string, object>)resource.Meta;
                foreach (var kvp in Metadata)
                {
                    metaExpando.Add(kvp.Key, kvp.Value);
                }
            }
            return(resource);
        }
Exemplo n.º 5
0
        public ResourceObject(object forObject, IJsonApiProfile profile)
        {
            _forObject = forObject;
            _profile   = profile;

            if (forObject != null)
            {
                ValidateResourceObjectAttribute(forObject);
                ValidateResourceFieldNames(forObject);
                _innerExpando      = TypeExtensions.InitializeExpandoFromPublicObjectProperties(forObject);
                _innerExpandoDict  = _innerExpando;
                _innerExpando.Id   = GetResourceId(forObject);
                _innerExpando.Type = GetResourceType(forObject, profile.Inflector);

                ValidatePropertyNameUniqueness(_innerExpando);
                Resourcify(forObject, _innerExpando, profile);
            }
        }
 public JsonApiMediaTypeFormatter(IJsonApiProfile profile)
 {
     _profile = profile;
     SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
     SerializerSettings.ContractResolver = new JsonApiContractResolver(_profile);
 }
 public JsonApiContractResolver(IJsonApiProfile profile)
 {
     _profile = profile;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Convert child objects marked with the [Resource] attribute into ResourceObject instances
        /// </summary>
        private static void Resourcify(object forObject, IDictionary <string, object> expandoDict, IJsonApiProfile withProfile)
        {
            foreach (var propertyInfo in forObject.GetType().GetPropertiesAndFields(TypeExtensions.PUBLIC_INSTANCE))
            {
                var resRel = propertyInfo.GetCustomAttribute <ResourceRelationshipAttribute>();
                if (resRel != null)
                {
                    var propValue = propertyInfo.GetValue(forObject);
                    if (propertyInfo.OfType == typeof(Uri) && propValue != null)
                    {
                        expandoDict[propertyInfo.Name] = LinkObject.LinkToUri((Uri)propValue);
                        continue;
                    }

                    Type enumerableType = propertyInfo.OfType.GetGenericIEnumerables().FirstOrDefault();
                    if (enumerableType != null)
                    {
                        if (propValue != null)
                        {
                            // It's a to-many relationship
                            var resources = (((IEnumerable)propValue))
                                            .Cast <object>()
                                            .Select(o => new ResourceObject(o, withProfile))
                                            .ToList();
                            expandoDict[propertyInfo.Name] = LinkObject.LinkToMany(resources, resRel.Sideload);
                        }
                        else
                        {
                            expandoDict[propertyInfo.Name] = LinkObject.Empty(LinkType.ToMany);
                        }
                    }
                    else
                    {
                        // It's a to-one relationship
                        if (propValue != null)
                        {
                            var resourceObject = new ResourceObject(propValue, withProfile);
                            expandoDict[propertyInfo.Name] = LinkObject.LinkToOne(resourceObject, resRel.Sideload);
                        }
                        else
                        {
                            expandoDict[propertyInfo.Name] = LinkObject.Empty(LinkType.ToOne);
                        }
                    }
                }
            }
        }