-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from itsharppro/dev
(#18) update requests payloads serialization options
- Loading branch information
Showing
13 changed files
with
181 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
|
||
namespace Nuar.Formatters | ||
{ | ||
public class NetJsonInputFormatter : TextInputFormatter | ||
{ | ||
public NetJsonInputFormatter() | ||
{ | ||
SupportedMediaTypes.Add("application/json"); | ||
SupportedEncodings.Add(Encoding.UTF8); | ||
} | ||
|
||
protected override bool CanReadType(Type type) | ||
{ | ||
return type != null; | ||
} | ||
|
||
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) | ||
{ | ||
var request = context.HttpContext.Request; | ||
using (var reader = new StreamReader(request.Body, encoding)) | ||
{ | ||
var body = await reader.ReadToEndAsync(); | ||
var result = NetJSON.NetJSON.Deserialize(context.ModelType, body); | ||
return await InputFormatterResult.SuccessAsync(result); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
|
||
namespace Nuar.Formatters | ||
{ | ||
public class NetJsonOutputFormatter : TextOutputFormatter | ||
{ | ||
public NetJsonOutputFormatter() | ||
{ | ||
SupportedMediaTypes.Add("application/json"); | ||
SupportedEncodings.Add(Encoding.UTF8); | ||
} | ||
|
||
protected override bool CanWriteType(Type type) | ||
{ | ||
return type != null; | ||
} | ||
|
||
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) | ||
{ | ||
var response = context.HttpContext.Response; | ||
var json = NetJSON.NetJSON.Serialize(context.Object); | ||
return response.WriteAsync(json); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,51 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Newtonsoft.Json; | ||
using NetJSON; | ||
|
||
namespace Nuar.Requests | ||
{ | ||
internal sealed class PayloadBuilder : IPayloadBuilder | ||
{ | ||
public PayloadBuilder() | ||
{ | ||
NetJSON.NetJSON.DateFormat = NetJSON.NetJSONDateFormat.ISO; | ||
NetJSON.NetJSON.SkipDefaultValue = false; | ||
NetJSON.NetJSON.TimeZoneFormat = NetJSON.NetJSONTimeZoneFormat.Utc; | ||
} | ||
|
||
public async Task<string> BuildRawAsync(HttpRequest request) | ||
{ | ||
var content = string.Empty; | ||
if (request.Body == null) | ||
{ | ||
return content; | ||
return string.Empty; | ||
} | ||
|
||
using (var reader = new StreamReader(request.Body)) | ||
{ | ||
content = await reader.ReadToEndAsync(); | ||
} | ||
|
||
Console.WriteLine($"Incoming Payload: {content}"); | ||
|
||
|
||
} | ||
return content; | ||
} | ||
|
||
public async Task<T> BuildJsonAsync<T>(HttpRequest request) where T : class, new() | ||
{ | ||
var payload = await BuildRawAsync(request); | ||
|
||
return string.IsNullOrWhiteSpace(payload) ? new T() : JsonConvert.DeserializeObject<T>(payload); | ||
if (string.IsNullOrWhiteSpace(payload)) | ||
{ | ||
return new T(); | ||
} | ||
|
||
var deserializedPayload = NetJSON.NetJSON.Deserialize<T>(payload); | ||
Console.WriteLine($"Deserialized Payload: {NetJSON.NetJSON.Serialize(deserializedPayload)}"); | ||
|
||
return deserializedPayload; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.