Giter VIP home page Giter VIP logo

Comments (11)

MiKom avatar MiKom commented on May 24, 2024 4

@dave-bt To work around the problem before the fix is released you can force the Json serializer to always use lowercase property names like that:

    public class Post
    {
        [JsonPropertyName("author")]
        public string Author { get; set; } = "";

        [JsonPropertyName("content")]
        public string Content { get; set; } = "";
    }

You may need to add reference to the System.Text.Json package however if your class is located in netstandard2.1 project (default with Blazor .Shared projects):

  <ItemGroup>
    <PackageReference Include="System.Text.Json" Version="4.6.0-preview6.19303.8" />
  </ItemGroup>

from signalr.

dave-bt avatar dave-bt commented on May 24, 2024

I have the exact same issue. If I retrieve the object into my Blazor page using HttpClient it is populated, if I send the same object to my SignalR handler then the object's fields are all null. I may also be doing something very wrong - this is all rather new to me.

from signalr.

MiKom avatar MiKom commented on May 24, 2024

Ok, I found out that the problem is related to case-sensitivity of names of properties in my object.

The object that I was trying to send down SignalR looks like that:

    public class Post
    {
        public string Author { get; set; }
        public string Text { get; set; }
    }

Unfortunately, the System.Text.Json serializer on the server makes all property names lower-case before sending the object representation down the pipe. Client however, is more picky and expects exactly the same casing and thus, cannot match property names.

After changing my object to:

    public class Post
    {
        public string author { get; set; }
        public string text { get; set; }
    }

It started working.

from signalr.

MiKom avatar MiKom commented on May 24, 2024

I can see that I had .AddNewtonsoftJson() in my sample project after all, but it doesn't change anything because System.Text.Json and Newtonsoft Json both make serialized property names case-insensitive.

I can see that .Net client for SignalR core changed to also accept case-insensitive properties by default so it should be implemented as such in this client as well.

dotnet/aspnetcore#10774

from signalr.

MiKom avatar MiKom commented on May 24, 2024

This probably has to happen in HubConnection.cs where JsonSerializer.Parse<>() is called bunch of times.

That's where case-(in)sensivity matters.

Maybe it should be configurable when setting up the hub connection like with AddJsonProtocol of regular SignalR client. Or maybe this Blazor client has limited scope and we shouldn't be so flexible and enable only the default. I can prepare some PR for the latter one if that's the way the maintainers want to go with.

from signalr.

galvesribeiro avatar galvesribeiro commented on May 24, 2024

Hello @MiKom

Please open the PR if you are in the mood for that. It will be very much appreciated.

However, keep in mind that this package tries to only wrap the JS client as much as possible,

So if there is something that the C# is interfering we should indeed tackle.

Thanks!

from signalr.

MiKom avatar MiKom commented on May 24, 2024

I'll get to it tomorrow, it's getting late here :)

from signalr.

dave-bt avatar dave-bt commented on May 24, 2024

@MiKom Thank you very much indeed for that. Works a treat!

from signalr.

galvesribeiro avatar galvesribeiro commented on May 24, 2024

The preview7 package was published and it supports the JsonPropertyName. Please try it out.

from signalr.

dradovic avatar dradovic commented on May 24, 2024

The property names parsing is still not case-insensitive, also in preview7 right? Are there any plans to make it so?

from signalr.

galvesribeiro avatar galvesribeiro commented on May 24, 2024

Yes, the current System.Text.Json requires you to use JsonPropertyName.

from signalr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.