예제 #1
0
        private JToken SerializeData(SerializationContext serializationContext, IDictionary <string, JToken> properties)
        {
            var data = SerializeMinimalData(serializationContext, properties);

            var attributes = SerializeAttributes(serializationContext, properties);

            if (attributes != null)
            {
                data["attributes"] = attributes;
            }

            var relationships = SerializeRelationships(serializationContext, properties);

            if (relationships != null)
            {
                data["relationships"] = relationships;
            }

            if (serializationContext.IsCollection)
            {
                data["links"] = AddUrl(
                    serializationContext.BaseUrl,
                    new JObject(),
                    "self",
                    _urlBuilder.BuildCanonicalPath(serializationContext.Resource, EnsureHasId(properties, serializationContext.Resource).Value <string>()));
            }

            return(data);
        }
예제 #2
0
        private JObject SerializeNode(ResourceGraphNode node, bool isCollection)
        {
            var response = new JObject
            {
                ["type"] = node.Key.Type,
                ["id"]   = JToken.FromObject(node.Key.Id)
            };

            if (isCollection)
            {
                var self = _urlBuilder.BuildCanonicalPath(node.Resource, node.Key.Id.ToString());

                if (!string.IsNullOrEmpty(self) && node.Resource.LinkType.HasFlag(LinkType.Self))
                {
                    response["links"] = AddUrl(new JObject(), "self", self);
                }
            }

            var attributes = SerializeAttributes(node);

            if (attributes != null)
            {
                response["attributes"] = attributes;
            }

            var relationships = SerializeRelationships(node);

            if (relationships != null)
            {
                response["relationships"] = relationships;
            }

            return(response);
        }
예제 #3
0
        private JToken SerializeData(IDictionary <string, JToken> properties)
        {
            var data = SerializeMinimalData(properties);

            data["attributes"]    = SerializeAttributes(properties);
            data["relationships"] = SerializeRelationships(properties);

            if (_isCollection)
            {
                data["links"] = AddUrl(
                    new JObject(),
                    "self",
                    _urlBuilder.BuildCanonicalPath(_resource, EnsureHasId(properties, _resource).Value <string>()));
            }

            return(data);
        }
예제 #4
0
        private JToken CreateTopLevelLinks(int count, string id = null)
        {
            var result = new JObject();

            // to preserve back compatibility if Self is enabled, then we also render it. Or if TopSelf is enabled
            if (_resource.LinkType.HasFlag(LinkType.TopSelf) || _resource.LinkType.HasFlag(LinkType.Self))
            {
                if (id != null && !_baseUrl.AbsolutePath.EndsWith(id, StringComparison.InvariantCultureIgnoreCase))
                {
                    AddUrl(result, "self", _urlBuilder.BuildCanonicalPath(_resource, id));
                }
                else
                {
                    result.Add("self", _baseUrl.ToString());
                }
            }

            var queryStrings = new PaginationQuery(_paginationContext, _value);

            var left = _baseUrl.GetLeftPart(UriPartial.Path);

            if (queryStrings.FirstPage != null)
            {
                result["first"] = new Uri(left + queryStrings.FirstPage);
            }

            if (queryStrings.NextPage != null && count >= _paginationContext.PerPage)
            {
                result["next"] = new Uri(left + queryStrings.NextPage);
            }

            if (queryStrings.PreviousPage != null)
            {
                result["prev"] = new Uri(left + queryStrings.PreviousPage);
            }

            if (queryStrings.LastPage != null)
            {
                result["last"] = new Uri(left + queryStrings.LastPage);
            }

            return(result);
        }
예제 #5
0
        private JObject SerializeNode(ResourceGraphNode node, bool isCollection)
        {
            var response = new JObject
            {
                ["type"] = node.Key.Type,
                ["id"]   = JToken.FromObject(node.Key.Id)
            };

            if (isCollection)
            {
                var self = _urlBuilder.BuildCanonicalPath(node.Resource, node.Key.Id.ToString());

                if (!string.IsNullOrEmpty(self) && node.Resource.LinkType.HasFlag(LinkType.Self))
                {
                    response["links"] = AddUrl(new JObject(), "self", self);
                }
            }

            JObject attributes = null;

            if (_fieldsetContext != null && _fieldsetContext.Properties.Count(property => property.Type == node.Key.Type) > 0)
            {
                FieldsetProperty fieldset = _fieldsetContext.Properties.Where(property => property.Type == node.Key.Type).First();
                attributes = SerializeAttributes(node, fieldset);
            }
            else
            {
                attributes = SerializeAttributes(node);
            }

            if (attributes != null)
            {
                response["attributes"] = attributes;
            }

            var relationships = SerializeRelationships(node);

            if (relationships != null)
            {
                response["relationships"] = relationships;
            }

            return(response);
        }