Skip to content

NullReference when JsonExtensionData dictionary have a custom JsonConverter #32903

Closed

Description

@jozkee

Repro:

private class ClassWithExtensionData
{
    [JsonExtensionData]
    public Dictionary<string, object> Overflow { get; set; }
}

public class DictionaryOverflowConverter : JsonConverter<Dictionary<string, object>>
{
    public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
    {
        // Do something.
    }
}

[Fact]
public static void QuickTest()
{
    var root = new ClassWithExtensionData();
    root.Overflow = new Dictionary<string, object>();
    root.Overflow.Add("test", "TestValue");

    var opts = new JsonSerializerOptions();
    opts.Converters.Add(new DictionaryOverflowConverter());

    string json = JsonSerializer.Serialize(root, opts);
}

This only happens in master.

@steveharter @layomia

Activity

  1. added
    untriaged
    on Feb 27, 2020
  2. added and removed
    untriaged
    on Feb 27, 2020
  3. added this to the 5.0 milestone on Feb 27, 2020
  4. layomia commented on Feb 28, 2020

    @layomia
    Contributor

    Note: custom converters for extension properties can only be used on serialization. Deserialization is handled by the serializer.

  5. added
    help wanted
    good first issue
    on Feb 28, 2020
  6. ahsonkhan commented on Mar 2, 2020

    @ahsonkhan
    Contributor

    can only be used on serialization. Deserialization is handled by the serializer.

    That's a bit strange. Is there a reason for that and should we (can we?) fix it?

  7. jozkee commented on Mar 2, 2020

    @jozkee
    MemberAuthor

    @ahsonkhan Overflow properties does not come on a particular order when reading the JSON and the Read method must return a Dictionary<string, JsonElement> or Dictionary<string, object> while overflow properties are parsed to JsonElement entries in such dictionary. That is why we don't use it on deserialization.

  8. layomia commented on Mar 2, 2020

    @layomia
    Contributor

    can only be used on serialization. Deserialization is handled by the serializer.

    That's a bit strange. Is there a reason for that and should we (can we?) fix it?

    @ahsonkhan it's because we may not have all the extra JSON in one go - we'd have to parse and wrap all the extension properties in one JSON object, or pass the entire payload to the converter to be filtered.

    FWIW Newtonsoft does not honor custom converters for both reading and writing extension data: https://dotnetfiddle.net/qwMrPS. Writing seems doable, so I think the action for this issue is to fix the NRE and make sure custom converters are honored on serialization.

    @steveharter, it seems that the assumption in

    is that only internal converters are used for serialization. Any thoughts?

  9. RezaJooyandeh commented on Apr 6, 2020

    @RezaJooyandeh
    Contributor

    I think for writing it makes sense to support custom serialization.

  10. added a commit that references this issue on Apr 14, 2020
    cfb109b
  11. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
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.Jsonbuggood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions