Esempio n. 1
0
        public Task <InputFormatterResult> ReadAsync(InputFormatterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var request = context.HttpContext.Request;

            if (request.ContentLength == 0)
            {
                return(InputFormatterResult.SuccessAsync(null));
            }

            var loggerFactory = GetService <ILoggerFactory>(context);
            var logger        = loggerFactory?.CreateLogger <JsonApiInputFormatter>();

            try
            {
                var body           = GetRequestBody(context.HttpContext.Request.Body);
                var jsonApiContext = GetService <IJsonApiContext>(context);
                var model          = jsonApiContext.IsRelationshipPath ?
                                     JsonApiDeSerializer.DeserializeRelationship(body, jsonApiContext) :
                                     JsonApiDeSerializer.Deserialize(body, jsonApiContext);

                if (model == null)
                {
                    logger?.LogError("An error occurred while de-serializing the payload");
                }

                return(InputFormatterResult.SuccessAsync(model));
            }
            catch (JsonSerializationException ex)
            {
                logger?.LogError(new EventId(), ex, "An error occurred while de-serializing the payload");
                context.HttpContext.Response.StatusCode = 422;
                return(InputFormatterResult.FailureAsync());
            }
        }