Skip to content

Why does my Json Deserializer return 0 for all numeric values, but Swagger returns the correct values? #43729

@rfalanga

Description

@rfalanga

Type of issue

Other (describe below)

Description

I'm working on writing a Blazor app using .NET 8. I've got a database with column names that have embedded whitespace. When I deserialize it all the numeric values (SMALLINT in a SQL Server database) are 0 (for example the column "Video Number"), which is wrong and doesn't correctly show what's in the table. However, when I check the values using Swagger, it is perfectly correct. I had thought that the attribute JsonPropertyName would resolve my issue, but it doesn't. Here's my class

public partial class Entry
{
    [Key, Column("Video Number", Order = 0)]
    [JsonPropertyName("Video Number")]
    public short VideoNumber { get; set; }  

    [Key, Column("Entry", Order = 1)]
    public byte Entry1 { get; set; }

    [Required]
    public short ShowCode { get; set; }

    [Required]
    public string EpisodeName { get; set; } = null!;

    public DateTime? ActualPlayTime { get; set; }

    public bool Completed { get; set; }

    public string? Comment { get; set; }

    public TimeOnly? ActualPlayTime1 { get; set; }

    public virtual Show Show { get; set; } = null!;

    public virtual Video VideoNumberNavigation { get; set; } = null!;
}

And here's the code snippet where I'm trying to read the data out of the table:

var contentStream = await httpResponse.Content.ReadAsStreamAsync();
var bozo = await System.Text.Json.JsonSerializer.DeserializeAsync<Entry[]>(contentStream, new System.Text.Json.JsonSerializerOptions { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull, PropertyNameCaseInsensitive = true });
return bozo;

Page URL

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties

Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/serialization/system-text-json/customize-properties.md

Document Version Independent Id

27c18218-d28f-a524-b7e1-8262005b7026

Article author

@gewarren

Metadata

  • ID: 7bee4ecb-7464-b321-937a-e6d4586556eb
  • Service: dotnet-fundamentals

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions