Blazored / LocalStorage

A library to provide access to local storage in Blazor applications
https://blazored.github.io/LocalStorage/
MIT License
1.23k stars 117 forks source link

Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported #178

Closed nssidhu closed 2 years ago

nssidhu commented 2 years ago

I am trying to store object that has

 public class AuthInfo
    {
        public string AuthToken { get; set; }
        public string FristName { get; set; }
        public string LastName { get; set; }

        public string DisplayName { get; set; }

        public string Email { get; set; }

        public string CountryDialingCode { get; set; }

        public string TelePhone { get; set; }

        public List<Claim> Claims { get; set; }
    }

await _localStorage.SetItemAsync<AuthInfo>("AuthInfo",authInfo);

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'System.Security.Claims.Claim'. Path: $.Claims[0] | LineNumber: 9 | BytePositionInLine: 5.

Do i have option to instead use newtonsoft for serialization & deserialization ?

nssidhu commented 2 years ago

I used the suggestion from this post and now using json.net since it provide the capability to write custom convert.

https://stackoverflow.com/questions/28155169/how-to-programmatically-choose-a-constructor-during-deserialization

chrissainty commented 2 years ago

Hi @nssidh,

The JSON serialiser is replaceable. See the server sample project for an example where it has been replaced with a Newtonsoft JSON one (samples/BlazorServer/Program.cs)

nssidhu commented 2 years ago

builder.Services.Replace(ServiceDescriptor.Scoped<IJsonSerializer, NewtonSoftJsonSerializer>()); This one replaces for the whole application.

Is there one just for your Blazored Component ( and that too may be temporarily for such odd objects and then revert back to default one) ?

chrissainty commented 2 years ago

This is for Blazored LocalStorage. The IJsonSerializer interface is from this library.

chrissainty commented 2 years ago

Closing as there is nothing else to do.