Skip to content

JsonConverter for {ReadOnly}Memory doesn't deserialize nulls #95267

Closed

Description

@stephentoub

Given that this code runs successfully without exception or assert:

using System.Diagnostics;
using System.Text.Json;

ReadOnlyMemory<float> rom;

rom = default;
Debug.Assert(rom.IsEmpty);

rom = null;
Debug.Assert(rom.IsEmpty);

rom = (ReadOnlyMemory<float>)null;
Debug.Assert(rom.IsEmpty);

rom = ((float[])null!).AsMemory();
Debug.Assert(rom.IsEmpty);

rom = JsonSerializer.Deserialize<float[]>("null");
Debug.Assert(rom.IsEmpty);

it breaks my expectations that this:

rom = JsonSerializer.Deserialize<ReadOnlyMemory<float>>("null"); // boom

throws an exception:

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.ReadOnlyMemory`1[System.Single]. Path: $ | LineNumber: 0 | BytePositionInLine: 4.
   at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)

Activity

  1. ghost added
    untriaged
    on Nov 27, 2023
  2. eiriktsarpalis commented on Nov 27, 2023

    @eiriktsarpalis
    Member

    cc @jozkee, do you think we should try change this to support null JSON tokens?

  3. added
    enhancement
    and removed
    untriaged
    on Nov 27, 2023
  4. added this to the 9.0.0 milestone on Nov 27, 2023
  5. ghost added
    in-pr
    on Nov 27, 2023
  6. jozkee commented on Nov 27, 2023

    @jozkee
    Member

    I believe so, yes. Just FWIW, readonly struct ImmutableArray<T> also throws this exception, but I believe ImmutableArray<T> doesn't have any bonds with null as opposed to this one. I'm not aware of any more "struct collections" to use as a reference for the semantic.

  7. ghost removed
    in-pr
    on Nov 28, 2023
  8. locked and limited conversation to collaborators on Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions