Skip to content
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

JsonSerializer.Deserialize<T>() not work #82016

Closed
GF-Huang opened this issue Feb 12, 2023 · 4 comments
Closed

JsonSerializer.Deserialize<T>() not work #82016

GF-Huang opened this issue Feb 12, 2023 · 4 comments

Comments

@GF-Huang
Copy link

Description

The JsonSerializer.Deserialize<T> not fill properties of Settings.

JSON file content:

image

Code:

public record ApiConfig {
    public string AppUrl { get; init; } = string.Empty;

    public string AuthenticationEndPoint { get; init; } = string.Empty;

    public string TokenEndPoint { get; init; } = string.Empty;

    public string AppKey { get; init; } = string.Empty;

    public string OpenApiBaseUrl { get; init; } = string.Empty;

    public string StreamingUrl { get; set; } = string.Empty;
}

public class Settings {
    public ApiConfig OpenApiConfig { get; } = new();
}
var text = ... // json content as above
// Why settings all properties are empty?
var settings = JsonSerializer.Deserialize<Settings>(text) ?? throw new InvalidDataException(...);

image

Reproduction Steps

As my code.

Expected behavior

The settings returned by JsonSerializer.Deserialize<T> be filled.

Actual behavior

The settings properties all keep empty.

Regression?

No response

Known Workarounds

No response

Configuration

.NET: 7.0.2
OS: 10.0.22621.0

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 12, 2023
@ghost
Copy link

ghost commented Feb 12, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The JsonSerializer.Deserialize<T> not fill properties of Settings.

JSON file content:

image

Code:

public record ApiConfig {
    public string AppUrl { get; init; } = string.Empty;

    public string AuthenticationEndPoint { get; init; } = string.Empty;

    public string TokenEndPoint { get; init; } = string.Empty;

    public string AppKey { get; init; } = string.Empty;

    public string OpenApiBaseUrl { get; init; } = string.Empty;

    public string StreamingUrl { get; set; } = string.Empty;
}

public class Settings {
    public ApiConfig OpenApiConfig { get; } = new();
}
var text = ... // json content as above
// Why settings all properties are empty?
var settings = JsonSerializer.Deserialize<Settings>(text) ?? throw new InvalidDataException(...);

image

Reproduction Steps

As my code.

Expected behavior

The settings returned by JsonSerializer.Deserialize<T> be filled.

Actual behavior

The settings properties all keep empty.

Regression?

No response

Known Workarounds

No response

Configuration

.NET: 7.0.2
OS: 10.0.22621.0

Other information

No response

Author: GF-Huang
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@Clockwork-Muse
Copy link
Contributor

... If these settings are for your own application, there are existing libraries for binding configuration.

That said, this is because Settings.OpenApiConfig is get only. If you change it to at least init it will work:

public class Settings {
    public ApiConfig OpenApiConfig { get; init; } = new();
}

@GF-Huang
Copy link
Author

God, there is still a gap between Systen.Text.Json and Newtonsoft.Json even in .NET 7.

@eiriktsarpalis
Copy link
Member

This is because System.Text.Json doesn't support populate behavior yet, but we're working on it.

Duplicate of #78556.

cc @krwq

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Feb 13, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants