Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Clean up input and output formatters #169

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 62 additions & 55 deletions src/Utf8Json.AspNetCoreMvcFormatter/Formatter.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,91 @@
using Microsoft.AspNetCore.Mvc.Formatters;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Formatters;
using System.Threading.Tasks;

namespace Utf8Json.AspNetCoreMvcFormatter
{
public class JsonOutputFormatter : IOutputFormatter //, IApiResponseTypeMetadataProvider
public class Utf8JsonOutputFormatter : IOutputFormatter
{
const string ContentType = "application/json";
static readonly string[] SupportedContentTypes = new[] { ContentType };
private const string ContentType = "application/json";
private readonly IJsonFormatterResolver _jsonFormatterResolver;

readonly IJsonFormatterResolver resolver;

public JsonOutputFormatter()
: this(null)
{

}
public JsonOutputFormatter(IJsonFormatterResolver resolver)
public Utf8JsonOutputFormatter() : this(null)
{
this.resolver = resolver ?? JsonSerializer.DefaultResolver;
}

//public IReadOnlyList<string> GetSupportedContentTypes(string contentType, Type objectType)
//{
// return SupportedContentTypes;
//}
public Utf8JsonOutputFormatter(
IJsonFormatterResolver jsonFormatterResolver)
=> _jsonFormatterResolver =
jsonFormatterResolver ?? JsonSerializer.DefaultResolver;

public bool CanWriteResult(OutputFormatterCanWriteContext context)
{
return true;
}
=> context.HttpContext.Request.Headers.TryGetValue(
"Accept",
out var acceptValues) &&
acceptValues.All(accept => accept.Contains(
ContentType));

public Task WriteAsync(OutputFormatterWriteContext context)
{
context.HttpContext.Response.ContentType = ContentType;
var response = context.HttpContext.Response;
response.ContentType = ContentType;

// when 'object' use the concrete type(object.GetType())
if (context.ObjectType == typeof(object))
{
return JsonSerializer.NonGeneric.SerializeAsync(context.HttpContext.Response.Body, context.Object, resolver);
}
else
{
return JsonSerializer.NonGeneric.SerializeAsync(context.ObjectType, context.HttpContext.Response.Body, context.Object, resolver);
}
var objectType = context.ObjectType;
var obj = context.Object;

var serializeType = objectType == typeof(object)
? obj.GetType()
: objectType;

return JsonSerializer.NonGeneric.SerializeAsync(
serializeType,
response.Body,
obj,
_jsonFormatterResolver);
}
}

public class JsonInputFormatter : IInputFormatter // , IApiRequestFormatMetadataProvider
public class Utf8JsonInputFormatter : IInputFormatter
{
const string ContentType = "application/json";
static readonly string[] SupportedContentTypes = new[] { ContentType };

readonly IJsonFormatterResolver resolver;
private const string ContentType = "application/json";
private readonly IJsonFormatterResolver _jsonFormatterResolver;

public JsonInputFormatter()
: this(null)
public Utf8JsonInputFormatter() : this(null)
{

}

public JsonInputFormatter(IJsonFormatterResolver resolver)
{
this.resolver = resolver ?? JsonSerializer.DefaultResolver;
}

//public IReadOnlyList<string> GetSupportedContentTypes(string contentType, Type objectType)
//{
// return SupportedContentTypes;
//}
public Utf8JsonInputFormatter(
IJsonFormatterResolver jsonFormatterResolver)
=> _jsonFormatterResolver =
jsonFormatterResolver ?? JsonSerializer.DefaultResolver;

public bool CanRead(InputFormatterContext context)
{
return true;
}
=> context.HttpContext.Request.ContentType ==
ContentType;

public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
public async Task<InputFormatterResult> ReadAsync(
InputFormatterContext context)
{
var request = context.HttpContext.Request;
var result = JsonSerializer.NonGeneric.Deserialize(context.ModelType, request.Body, resolver);
return InputFormatterResult.SuccessAsync(result);
var body = context.HttpContext.Request.Body;
if (body.CanSeek && body.Length == 0)
{
return InputFormatterResult.NoValue();
}

try
{
var model = await JsonSerializer.NonGeneric
.DeserializeAsync(
context.ModelType,
body,
_jsonFormatterResolver)
.ConfigureAwait(false);
return InputFormatterResult.Success(model);
}
catch (JsonParsingException)
{
return InputFormatterResult.Failure();
}
}
}
}
}