-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error in SWAN JSON Deserializer #240
Comments
Can you post a sample JSON and the definition of the class |
@AppsmithsLLC posted on Slack before opening this issue, giving the following sample JSON:
We still need the definition of |
SignInRequest Model:
|
The issue is because of the case, you need to specify that you are using camelCase instead of PascalCase. |
@geoperez would you be able to cite an example of how to do this? Since I am using |
@AppsmithsLLC the request deserializer callback to use can be specified at Web API module level, like this: var server = new WebServer("http://*:8080")
.WithWebApi("/api", RequestDeserializer.Json, m => m
.WithController<MyController>()); Or it can be given as a parameter to var myData = HttpContext.GetRequestDataAsync<MyData>(RequestDeserializer.Json); Unfortunately, there's currently no way to specify parameters for the JSON deserializer. Now for one of the most hated sentences in IT: it will be implemented in next version. 😬 As a workaround, since you already use JSON.NET in your project, you can use it for deserializing request bodies too. Just add this to your project: using System;
using System.Threading.Tasks;
using EmbedIO;
using Newtonsoft.Json;
using Swan.Logging;
namespace YOUR_NAMESPACE_HERE
{
/// <summary>
/// Provides alternative request deserialization callbacks.
/// </summary>
public static class OtherRequestDeserializer
{
/// <summary>
/// Asynchronously deserializes a request body in JSON format, using JSON.NET.
/// </summary>
/// <typeparam name="TData">The expected type of the deserialized data.</typeparam>
/// <param name="context">The <see cref="IHttpContext"/> whose request body is to be deserialized.</param>
/// <returns>A <see cref="Task{TResult}">Task</see>, representing the ongoing operation,
/// whose result will be the deserialized data.</returns>
public static async Task<TData> NewtonsoftJson<TData>(IHttpContext context)
{
using (var reader = context.OpenRequestText())
using (var jsonReader = new JsonTextReader(reader))
{
var serializer = new JsonSerializer();
try
{
return Task.FromResult(serializer.Deserialize<TData>(jsonReader));
}
catch (Exception)
{
$"[{context.Id}] Cannot convert JSON request body to {typeof(TData).Name}, sending 400 Bad Request..."
.Warn($"{nameof(OtherRequestDeserializer)}.{nameof(NewtonsoftJson)}");
throw HttpException.BadRequest("Incorrect request data format.");
}
}
}
}
} Then you can do this: var server = new WebServer("http://*:8080")
.WithWebApi("/api", OtherRequestDeserializer.NewtonsoftJson, m => m
.WithController<MyController>()); or this: var myData = HttpContext.GetRequestDataAsync<MyData>(OtherRequestDeserializer.NewtonsoftJson); |
Can I close this issue? |
i use to code but not JsonSerializerCase.CamelCase Only SerializerCase PascalCase... my Json object and MainController.Json method
Result Json string Case: PascalCase |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
var bodyAsString = await HttpContext.GetRequestBodyAsStringAsync(); This should resolve your problem |
Describe the bug
When deserializing JSON using
var data = await HttpContext.GetRequestDataAsync<SignInRequest>();
a new object is initialized, but the properties are all null.To Reproduce
Make a POST request:
```
public static async Task PostAsJsonAsync(this HttpClient httpClient, string url, T data, CancellationToken cancellationToken = default)
{
var json = JsonNetSerializer.Serialize(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
return await httpClient.PostAsync(url, content, cancellationToken);
}
The text was updated successfully, but these errors were encountered: