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));
            }

            try
            {
                var body  = GetRequestBody(context.HttpContext.Request.Body);
                var model = _jsonApiContext.IsRelationshipPath ?
                            _deSerializer.DeserializeRelationship(body) :
                            _deSerializer.Deserialize(body);

                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());
            }
        }
Example #2
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));
            }

            try
            {
                var body = GetRequestBody(context.HttpContext.Request.Body);

                object model = null;

                if (_jsonApiContext.IsRelationshipPath)
                {
                    model = _deSerializer.DeserializeRelationship(body);
                }
                else
                {
                    model = _deSerializer.Deserialize(body);
                }

                if (model == null)
                {
                    _logger?.LogError("An error occurred while de-serializing the payload");
                }
                return(InputFormatterResult.SuccessAsync(model));
            }
            catch (Exception ex)
            {
                _logger?.LogError(new EventId(), ex, "An error occurred while de-serializing the payload");
                context.ModelState.AddModelError(context.ModelName, ex, context.Metadata);
                return(InputFormatterResult.FailureAsync());
            }
        }