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

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

Open
rfalanga opened this issue Nov 23, 2024 · 0 comments
Labels
dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 ⌚ Not Triaged Not triaged

Comments

@rfalanga
Copy link

rfalanga commented Nov 23, 2024

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

@issues-automation issues-automation bot added dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 labels Nov 23, 2024
@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 ⌚ Not Triaged Not triaged
Projects
None yet
Development

No branches or pull requests

1 participant