CreateResourceRepresentation() public method

public CreateResourceRepresentation ( object objectGraph, IResourceMapping resourceMapping, Context context ) : SingleResource
objectGraph object
resourceMapping IResourceMapping
context Context
return NJsonApi.Serialization.Representations.Resources.SingleResource
Esempio n. 1
0
        public CompoundDocument Transform(object objectGraph, Context context)
        {
            Type innerObjectType = Reflection.GetObjectType(objectGraph);

            transformationHelper.VerifyTypeSupport(innerObjectType);
            transformationHelper.AssureAllMappingsRegistered(innerObjectType, configuration);

            var result = new CompoundDocument
            {
                Meta = transformationHelper.GetMetadata(objectGraph)
            };

            var resource        = transformationHelper.UnwrapResourceObject(objectGraph);
            var resourceMapping = configuration.GetMapping(innerObjectType);

            var resourceList       = transformationHelper.UnifyObjectsToList(resource);
            var representationList = resourceList.Select(
                o => transformationHelper.CreateResourceRepresentation(o, resourceMapping, context));

            result.Data  = transformationHelper.ChooseProperResourceRepresentation(resource, representationList);
            result.Links = transformationHelper.GetTopLevelLinks(objectGraph, context.RequestUri);

            if (resourceMapping.Relationships.Any())
            {
                result.Included = transformationHelper.CreateIncludedRepresentations(resourceList, resourceMapping, context);
            }

            return(result);
        }
Esempio n. 2
0
        public CompoundDocument Transform(object objectGraph, Context context)
        {
            CompoundDocument result = objectGraph as CompoundDocument;

            if (result != null)
            {
                return(result);
            }

            Type innerObjectType = TransformationHelper.GetObjectType(objectGraph);

            TransformationHelper.VerifyTypeSupport(innerObjectType);
            TransformationHelper.AssureAllMappingsRegistered(innerObjectType, context.Configuration);

            result = new CompoundDocument
            {
                Meta = TransformationHelper.GetMetadata(objectGraph)
            };

            var resource        = TransformationHelper.UnwrapResourceObject(objectGraph);
            var resourceMapping = context.Configuration.GetMapping(innerObjectType);

            var resourceList       = TransformationHelper.UnifyObjectsToList(resource);
            var representationList = resourceList.Select(o => TransformationHelper.CreateResourceRepresentation(o, resourceMapping, context));
            var primaryResource    = TransformationHelper.ChooseProperResourceRepresentation(resource, representationList);

            result.Data = primaryResource;

            if (resourceMapping.Relationships.Any())
            {
                result.Included = TransformationHelper.CreateIncludedRepresentations(resourceList, resourceMapping, context);
            }

            return(result);
        }