'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:
Cs debug:
Solution 1:[1]
- Supposing that
exportOptions.Optionsis of typeDictionary<string, object>as shown in the OP. - Supposing that
GetValueOrDefaultis (equivalent to) this. - Supposing that the value returned by
GetValueOrDefaultis of typeList<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 |


