-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
HttpClient.PostJsonAsync deserialize json failed #11210
Comments
Issue for Net Core Preview 6( runs fine in preview 5 )Same problem here with
Tis code is running fine on preview5 and fails silently on preview6:
@yan0lovesha 's workaround make the code running:
Additional info: the classes to deserialize in previous example ( "my code" ):
|
Seems like it might be a difference with how System.Text.Json deserializes. cc @ahsonkhan @yan0lovesha thanks for the sample JSON and classes, we can use that to see if we can repro the issue. |
@danroth27 Yes you're right. It was too late last night. I went a little bit deeper this morning. Below code statements on LinqPad6 can prove it:
Seems that we need to wait for next preview release on this. |
@yan0lovesha I believe the issue here is that System.Text.Json doesn't support doing serialization with public fields. Can you try using public properties instead? |
Here's what I tried with the settings static void Main(string[] args)
{
var jsonString = "{\"period\":\"May 09, 2019 - Jun 08, 2019\",\"dueDate\":\"Jul 01, 2019\",\"totalAmount\":326.03}";
var jsonObjFromDotnetCore = JsonSerializer.Parse<BillInfo>(jsonString, new JsonSerializerOptions {PropertyNamingPolicy = JsonNamingPolicy.CamelCase});
System.Console.WriteLine(jsonObjFromDotnetCore.Period);
}
public class BillInfo
{
public string Period { get; set; }
public string DueDate { get; set; }
public float TotalAmount { get; set; }
} I'd recommend filing an issue in https://github.com/dotnet/corefx/issues to consider adding support for public fields in a future release. |
Yes, the reason for the missing info is that fields are not supported in the new We have an issue open for this already: https://github.com/dotnet/corefx/issues/36505 I have added the initial repro/post to that issue. |
As this blog mentioned. I tried to use services.AddMvc().AddJsonOptions(...) in Startup class. The default WeatherForecast api works fine. It can respond a correct json string. But for my own api controller, it just output an empty json object "{}". With the same api controller code, AddNewtonsoftJson works fine. This maybe another issue? Or it needs some special configuration? |
Works for me changing public fields to public properties. Doesn't run:
Runs fine:
|
I ran into this very same issue with my Client side Blazor web app, after upgrading to Preview 6. Serialization just doesn't work. But for me it doesn't work even if I make my properties public. I only get an empty object back. |
@yan0lovesha \ @petroemil could you please share a simple application that reproduces the error? It's difficult to investigate without knowing what it is that's being serialized. |
My C# class looks like this: public class BlogPostMetadata
{
public bool IsDraft { get; set; }
public DateTimeOffset PublishDate { get; set; }
public string Title { get; set; }
public string HeroImageFile { get; set; }
public string SummaryMarkdownFile { get; set; }
public string? MarkdownFile { get; set; }
public string SocialSharingFile { get; set; }
} A sample JSON content for it looks like this {
"Title": "Hello World",
"PublishDate": "2019-05-17 16:19:34 -08:00",
"HeroImageFile": "heroimage.jpg",
"SummaryMarkdownFile": "summary.md",
"MarkdownFile": "post.md",
"SocialSharingFile": "socialshare.html"
} My referenced ASP.NET libraries are: <PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview6.19307.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.0.0-preview6.19307.2" PrivateAssets="all" /> I'm getting this JSON from a file with the I'm not getting any exception, just an empty object. |
@petroemil is the JSON produced from an MVC action? They're usually camel case which would explain why |
@pranavkm the JSON is coming from a file (wwwroot content) as shown in the example above. The JSON property names match the C# property names. |
Since the DateTimeOffset within the JSON is not ISO 8601 compliant (it contains spaces), the public class BlogPostMetadata
{
public bool IsDraft { get; set; }
public DateTimeOffset PublishDate { get; set; }
public string Title { get; set; }
public string HeroImageFile { get; set; }
public string SummaryMarkdownFile { get; set; }
public string MarkdownFile { get; set; }
public string SocialSharingFile { get; set; }
}
[Fact]
public static void TestingStuff()
{
string temp = @"{
""Title"": ""Hello World"",
""PublishDate"": ""2019-05-17 16:19:34 -08:00"", // spaces don't work
""HeroImageFile"": ""heroimage.jpg"",
""SummaryMarkdownFile"": ""summary.md"",
""MarkdownFile"": ""post.md"",
""SocialSharingFile"": ""socialshare.html""
}";
BlogPostMetadata foo = JsonSerializer.Parse<BlogPostMetadata>(temp);
Assert.Equal("Hello World", foo.Title);
}
cc @steveharter, @layomia |
@ahsonkhan wow, thank you very much. So even in Client side Blazor the serializer only got changed to |
@petroemil trying this out with preview7 I can see the error appear in the output: info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 4.3179ms 200 application/json
warn: Microsoft.AspNetCore.Components.Browser.Rendering.RemoteRenderer[100]
Unhandled exception rendering component: The JSON value could not be converted to System.DateTimeOffset. Path: $.PublishDate | LineNumber: 2 | BytePositionInLine: 45.
System.Text.Json.JsonException: The JSON value could not be converted to System.DateTimeOffset. Path: $.PublishDate | LineNumber: 2 | BytePositionInLine: 45.
at System.Text.Json.ThrowHelper.ThowJsonException(String message, Utf8JsonReader& reader, String path)
at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType, Utf8JsonReader& reader, String path)
at System.Text.Json.JsonPropertyInfoNotNullable`3.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Parse[TValue](String json, JsonSerializerOptions options)
at Microsoft.AspNetCore.Components.HttpClientJsonExtensions.GetJsonAsync[T](HttpClient httpClient, String requestUri) @yan0lovesha I'm closing this issue since it looks like using properties addressed the original issue you filed. Feel free to file a new work item if you're running in to further issues in this area. |
As a last note, if we are already here. I'm still having issues getting the properly deserialized object. Even if my class has I also noticed that |
Describe the bug
When I use below code in razor page to post a http request. The deserialized object is not correct. There is no exception happened.
I then changed to use Http.PostAsync to get the response string, then deserialize it by using Newtonsoft library. It works fine.
The response json string in my problem is:
The corresponding class is:
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The members of billInfo object have values.
The text was updated successfully, but these errors were encountered: