'List<string> differ from cs to blazor page

I have a dictionary of string and object, and I am filling it with same values in cs class and in a blazor page:

Blazor page and Cs class:

ExportOptions exportOptions = new();
exportOptions.Options = new Dictionary<string, object>();
exportOptions.Options.Add("SheetNames", new List<string> { "Sheet1" });

When I am deserializing the value of the dictionary, I am getting the following error only when using the Cs class:

ERROR: System.Text.Json.JsonException: ''S' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'

Code used for deserializing:

JsonSerializer.Deserialize<List<string>>(exportOptions.Options.GetValueOrDefault("SheetNames").ToString());

When debugging, the only difference is the following:

Blazor debug:

enter image description here

Cs debug:

enter image description here



Solution 1:[1]

  • Supposing that exportOptions.Options is of type Dictionary<string, object> as shown in the OP.
  • Supposing that GetValueOrDefault is (equivalent to) this.
  • Supposing that the value returned by GetValueOrDefault is of type List<string> (Again OP code and obscure images).

One can evaluate exportOptions.Options.GetValueOrDefault("SheetNames").ToString() to be "System.Collections.Generic.List`1[System.String]"
This string is not a valid JSON, and start by a S. Hence the message 'S' is an invalid start of a value. [first line, first char].

The real question is : why one want to deserialize an object?
You deserialize a string to build an object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Orace