コード例 #1
0
        /// <summary>
        /// Converts a single resource into a serialized <see cref="Document" />.
        /// </summary>
        /// <remarks>
        /// This method is internal instead of private for easier testability.
        /// </remarks>
        internal string SerializeSingle(IIdentifiable resource)
        {
            if (resource != null && _fieldsToSerialize.ShouldSerialize)
            {
                _resourceDefinitionAccessor.OnSerialize(resource);
            }

            IReadOnlyCollection <AttrAttribute>         attributes    = _fieldsToSerialize.GetAttributes(_primaryResourceType);
            IReadOnlyCollection <RelationshipAttribute> relationships = _fieldsToSerialize.GetRelationships(_primaryResourceType);

            Document       document       = Build(resource, attributes, relationships);
            ResourceObject resourceObject = document.SingleData;

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            AddTopLevelObjects(document);

            return(SerializeObject(document, _options.SerializerSettings, serializer =>
            {
                serializer.NullValueHandling = NullValueHandling.Include;
            }));
        }
コード例 #2
0
        /// <summary>
        /// Converts a single resource into a serialized <see cref="Document"/>.
        /// </summary>
        /// <remarks>
        /// This method is internal instead of private for easier testability.
        /// </remarks>
        internal string SerializeSingle(IIdentifiable resource)
        {
            var(attributes, relationships) = GetFieldsToSerialize();
            var document       = Build(resource, attributes, relationships);
            var resourceObject = document.SingleData;

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            AddTopLevelObjects(document);

            return(SerializeObject(document, _options.SerializerSettings, serializer => { serializer.NullValueHandling = NullValueHandling.Include; }));
        }
コード例 #3
0
        private AtomicResultObject SerializeOperation(OperationContainer operation)
        {
            ResourceObject resourceObject = null;

            if (operation != null)
            {
                _request.CopyFrom(operation.Request);
                _fieldsToSerialize.ResetCache();

                var resourceType  = operation.Resource.GetType();
                var attributes    = _fieldsToSerialize.GetAttributes(resourceType);
                var relationships = _fieldsToSerialize.GetRelationships(resourceType);

                resourceObject = ResourceObjectBuilder.Build(operation.Resource, attributes, relationships);
            }

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            return(new AtomicResultObject
            {
                Data = resourceObject
            });
        }
コード例 #4
0
        private AtomicResultObject SerializeOperation(OperationContainer operation)
        {
            ResourceObject resourceObject = null;

            if (operation != null)
            {
                _request.CopyFrom(operation.Request);
                _fieldsToSerialize.ResetCache();
                _evaluatedIncludeCache.Set(null);

                _resourceDefinitionAccessor.OnSerialize(operation.Resource);

                Type resourceType = operation.Resource.GetType();
                IReadOnlyCollection <AttrAttribute>         attributes    = _fieldsToSerialize.GetAttributes(resourceType);
                IReadOnlyCollection <RelationshipAttribute> relationships = _fieldsToSerialize.GetRelationships(resourceType);

                resourceObject = ResourceObjectBuilder.Build(operation.Resource, attributes, relationships);
            }

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            return(new AtomicResultObject
            {
                Data = resourceObject
            });
        }
コード例 #5
0
        /// <inheritdoc />
        public IList <ResourceObject> Build()
        {
            if (_included.Any())
            {
                // cleans relationship dictionaries and adds links of resources.
                foreach (var resourceObject in _included)
                {
                    if (resourceObject.Relationships != null)
                    {
                        foreach (var relationshipName in resourceObject.Relationships.Keys.ToArray())
                        {
                            var resourceContext = ResourceContextProvider.GetResourceContext(resourceObject.Type);
                            var relationship    = resourceContext.Relationships.Single(rel => rel.PublicName == relationshipName);

                            if (!IsRelationshipInSparseFieldSet(relationship))
                            {
                                resourceObject.Relationships.Remove(relationshipName);
                            }
                        }

                        // removes relationship entries (<see cref="RelationshipEntry"/>s) if they're completely empty.
                        var pruned = resourceObject.Relationships.Where(p => p.Value.IsPopulated || p.Value.Links != null).ToDictionary(p => p.Key, p => p.Value);
                        if (!pruned.Any())
                        {
                            pruned = null;
                        }
                        resourceObject.Relationships = pruned;
                    }
                    resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
                }
                return(_included.ToArray());
            }
            return(null);
        }
コード例 #6
0
        /// <summary>
        /// Convert a single entity into a serialized <see cref="Document"/>
        /// </summary>
        /// <remarks>
        /// This method is set internal instead of private for easier testability.
        /// </remarks>
        internal string SerializeSingle(IIdentifiable entity)
        {
            if (RequestRelationship != null)
            {
                return(JsonConvert.SerializeObject(((ResponseResourceObjectBuilder)_resourceObjectBuilder).Build(entity, RequestRelationship)));
            }

            var(attributes, relationships) = GetFieldsToSerialize();
            var document       = Build(entity, attributes, relationships);
            var resourceObject = document.SingleData;

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            AddTopLevelObjects(document);
            return(JsonConvert.SerializeObject(document));
        }
コード例 #7
0
        /// <inheritdoc />
        public IList <ResourceObject> Build()
        {
            if (_included.Any())
            {
                // cleans relationship dictionaries and adds links of resources.
                foreach (var resourceObject in _included)
                {
                    if (resourceObject.Relationships != null)
                    {
                        UpdateRelationships(resourceObject);
                    }

                    resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
                }
                return(_included.ToArray());
            }
            return(null);
        }
コード例 #8
0
        /// <inheritdoc />
        public IList <ResourceObject> Build()
        {
            if (_included.Any())
            {
                // Cleans relationship dictionaries and adds links of resources.
                foreach (ResourceObject resourceObject in _included)
                {
                    if (resourceObject.Relationships != null)
                    {
                        UpdateRelationships(resourceObject);
                    }

                    resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
                }

                return(_included.ToArray());
            }

            return(_queryStringAccessor.Query.ContainsKey("include") ? Array.Empty <ResourceObject>() : null);
        }
コード例 #9
0
        /// <summary>
        /// Convert a list of entities into a serialized <see cref="Document"/>
        /// </summary>
        /// <remarks>
        /// This method is set internal instead of private for easier testability.
        /// </remarks>
        internal string SerializeMany(IEnumerable <IIdentifiable> entities)
        {
            var(attributes, relationships) = GetFieldsToSerialize();
            var document = Build(entities, attributes, relationships);

            foreach (ResourceObject resourceObject in document.ManyData)
            {
                // sven omit links
                var links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
                if (links == null)
                {
                    break;
                }

                //resourceObject.Links = links;
            }

            AddTopLevelObjects(document);

            return(SerializeObject(document, _options.SerializerSettings, serializer => { serializer.NullValueHandling = NullValueHandling.Include; }));
        }
 /// <inheritdoc/>
 public List <ResourceObject> Build()
 {
     if (_included.Any())
     {
         // cleans relationship dictionaries and adds links of resources.
         foreach (var resourceObject in _included)
         {
             if (resourceObject.Relationships != null)
             {   /// removes relationship entries (<see cref="RelationshipEntry"/>s) if they're completely empty.
                 var pruned = resourceObject.Relationships.Where(p => p.Value.IsPopulated || p.Value.Links != null).ToDictionary(p => p.Key, p => p.Value);
                 if (!pruned.Any())
                 {
                     pruned = null;
                 }
                 resourceObject.Relationships = pruned;
             }
             resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
         }
         return(_included.ToList());
     }
     return(null);
 }