/// <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;
            }));
        }
Beispiel #2
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
            });
        }
        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
            });
        }
        /// <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    = _fieldsToSerialize.GetAttributes(_primaryResourceType);
            var relationships = _fieldsToSerialize.GetRelationships(_primaryResourceType);

            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; }));
        }
        private ResourceObject BuildCachedResourceObjectFor(IIdentifiable resource)
        {
            Type resourceType = resource.GetType();
            IReadOnlyCollection <AttrAttribute>         attributes    = _fieldsToSerialize.GetAttributes(resourceType);
            IReadOnlyCollection <RelationshipAttribute> relationships = _fieldsToSerialize.GetRelationships(resourceType);

            ResourceObject resourceObject = Build(resource, attributes, relationships);

            _included.Add(resourceObject);

            return(resourceObject);
        }
        /// <summary>
        /// Gets the resource object for <paramref name="parent"/> by searching the included list.
        /// If it was not already built, it is constructed and added to the inclusion list.
        /// </summary>
        private ResourceObject GetOrBuildResourceObject(IIdentifiable parent)
        {
            var type         = parent.GetType();
            var resourceName = ResourceContextProvider.GetResourceContext(type).PublicName;
            var entry        = _included.SingleOrDefault(ro => ro.Type == resourceName && ro.Id == parent.StringId);

            if (entry == null)
            {
                entry = Build(parent, _fieldsToSerialize.GetAttributes(type), _fieldsToSerialize.GetRelationships(type));
                _included.Add(entry);
            }
            return(entry);
        }
 private (IReadOnlyCollection <AttrAttribute>, IReadOnlyCollection <RelationshipAttribute>) GetFieldsToSerialize()
 {
     return(_fieldsToSerialize.GetAttributes(_primaryResourceType), _fieldsToSerialize.GetRelationships(_primaryResourceType));
 }