Skip to content

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

Closed
dotnet/corefx
#40654

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.

Activity

  1. dweatherman commented on Aug 26, 2019

    @dweatherman
    Author

    Closing... just noticed project is referencing preview 7. Will retest with 8.

  2. dweatherman commented on Aug 26, 2019

    @dweatherman
    Author

    Reopening... updated project to preview 8 and same issue occurs.

  3. transferred this issue fromdotnet/coreon Aug 26, 2019
  4. scalablecory commented on Aug 26, 2019

    @scalablecory
    Contributor
  5. ahsonkhan commented on Aug 27, 2019

    @ahsonkhan
    Contributor

    Looks like serialization works fine for ObservableCollection<T> which derives from Collection<T>, but deserialization fails.

    Is this behavior expected for 3.0, and is this issue a duplicate of https://github.com/dotnet/corefx/issues/40479?

    If so, we should be failing gracefully, potentially with NSE.

    [Fact]
    public static void ObservableCollection_Bool()
    {
        object result = JsonSerializer.Deserialize("[true,false]", typeof(ObservableCollection<bool>));
    }
    System.Text.Json.Serialization.Tests.CustomConverterTests.ObservableCollection_Bool [FAIL]
            System.Text.Json.JsonException : The JSON value could not be converted to System.Collections.Generic.ICollection`1[System.Boolean]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
            Stack Trace:
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\ThrowHelper.Serialization.cs(142,0): at System.Text.Json.ThrowHelper.ThrowJsonException(String message, Utf8JsonReader& reader, String path, Exception innerException)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\ThrowHelper.Serialization.cs(39,0): at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType, Utf8JsonReader& reader, String path, Exception innerException)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\ReadStackFrame.cs(195,0): at System.Text.Json.ReadStackFrame.CreateEnumerableValue(Utf8JsonReader& reader, ReadStack& state)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.HandleArray.cs(65,0): at System.Text.Json.JsonSerializer.HandleStartArray(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.cs(99,0): at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.Helpers.cs(22,0): at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs(74,0): at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options)
              E:\GitHub\Fork\corefx\src\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializer.Read.String.cs(60,0): at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
              E:\GitHub\Fork\corefx\src\System.Text.Json\tests\Serialization\CustomConverterTests.DerivedTypes.cs(16,0): at System.Text.Json.Serialization.Tests.CustomConverterTests.ObservableCollection_Bool()
    

    cc @steveharter, @layomia - please take a look.

  6. self-assigned this
    on Aug 27, 2019
  7. ahsonkhan commented on Aug 29, 2019

    @ahsonkhan
    Contributor

    Re-opening until we know which milestone to target.

    cc @ericstj - this issue is currently tagged as 3.0 but the fix is in master only.

    I believe support for built-in collection types outside of System.Collections and System.Collections.Generic
    namespaces is out-of-scope for 3.0. And this issue is about handling the collection type from System.Collections.ObjectModel. This can be vNext only if the change is too risky.

  8. ahsonkhan commented on Aug 30, 2019

    @ahsonkhan
    Contributor

    Moving this to 5.0 since it isn't critical for 3.0.

  9. ahsonkhan commented on Aug 30, 2019

    @ahsonkhan
    Contributor
  10. transferred this issue fromdotnet/corefxon Feb 1, 2020
  11. added this to the 5.0 milestone on Feb 1, 2020
  12. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions