Skip to content

Preview 8 System.Text.Json error deserializing ObservableCollection #30686

@dweatherman

Description

@dweatherman

Preview 8 System.Text.Json error deserializing ObservableCollection

Example

ObservableCollection<bool> col = new ObservableCollection<bool>();

col.Add(true);
col.Add(false);

string json = System.Text.Json.JsonSerializer.Serialize(col, typeof(ObservableCollection<bool>));
ObservableCollection<bool> result = (ObservableCollection<bool>)System.Text.Json.JsonSerializer.Deserialize(json, typeof(ObservableCollection<bool>));

Result is an exception;

System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.ICollection`1[System.Boolean]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
   at System.Text.Json.ThrowHelper.ThrowJsonException(String message, Utf8JsonReader& reader, String path, Exception innerException)
   at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType, Utf8JsonReader& reader, String path, Exception innerException)
   at System.Text.Json.ReadStackFrame.CreateEnumerableValue(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.HandleStartArray(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)`

If I can help with more information please just ask.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions