Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - JsonSerializer.DeserializeAsync<T> Not deserializing

I am working with my first Blazor app and running into a strange problem. I have a Web API that returns a fairly basic JSON response:

{
    "entityId": 26,
    "notifications": [
        {
            "type": "Success",
            "message": "The operation completed successfully."
        }
    ],
    "hasErrors": false,
    "hasWarnings": false
}

There is a standard POCO class that matches the above properties. From the Blazor app, when I try to get the response from an Http.PutAsJsonAsync<T>, an instance of the POCO class is created (so it's not null and doesn't throw an error), but none of the values above are actually present. The EntityId property is null and Notifications is instantiated but empty. The way I'm trying to access the response is:

var result = await Http.PutAsJsonAsync<ManufacturerModel>($"manufacturers", (ManufacturerModel)context.Model);
if (result.IsSuccessStatusCode)
{
  var response = await JsonSerializer.DeserializeAsync<EntityResponseModel>(result.Content.ReadAsStream());
    
  //response isn't null - it's just a newly created object with nothing mapped
}

Via the console in Chrome, I've confirmed the proper JSON is returned so it's really confusing why it'd create a new instance of that class, but not map any of the values.

Any ideas?

**** EDIT - including POCO definition ****

public class EntityResponseModel : BaseModel
{
    /// <summary>
    /// Gets or sets the Id of the affected entity
    /// </summary>
    public long? EntityId { get; set; }
}

public class BaseModel
{
    public BaseModel()
    {
        this.Notifications = new EntityNotificationCollection();
    }

    #region Validation

    /// <summary>
    /// Adds a notification to this model.
    /// </summary>
    /// <param name="type">The type of notification</param>
    /// <param name="message">The message to display</param>
    public void AddNotification(EntityNotificationTypes type, string message)
    {
        this.Notifications.Add(new EntityNotification { Type = type, Message = message });
    }

    /// <summary>
    /// Gets or sets the collection of notifications
    /// </summary>
    public EntityNotificationCollection Notifications { get; private set; }

    /// <summary>
    /// Gets whether errors exist on this model.
    /// </summary>
    //[JsonIgnore]
    public bool HasErrors { get => this.Notifications.HasErrors; }

    /// <summary>
    /// Gets whether warnings exist on this model
    /// </summary>
    //[JsonIgnore]
    public bool HasWarnings { get => this.Notifications.HasWarnings; }

    #endregion
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can specify your serialization settings and define it as being case sensitive or insensitive. CharlieFace provided this answer above.

Looks like you need to add your JsonAttribute to managing your case sensitivity.

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...