LocalStorage icon indicating copy to clipboard operation
LocalStorage copied to clipboard

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

Open nssidhu opened this issue 3 years ago • 4 comments

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 avatar Apr 17 '22 00:04 nssidhu

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

nssidhu avatar Apr 17 '22 02:04 nssidhu

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)

chrissainty avatar Apr 17 '22 05:04 chrissainty

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) ?

nssidhu avatar Apr 18 '22 12:04 nssidhu

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

chrissainty avatar Apr 18 '22 16:04 chrissainty

Closing as there is nothing else to do.

chrissainty avatar Aug 27 '22 22:08 chrissainty