Skip to content

Provide option for the serializer to treat IEnumerables like objects with members #30855

Closed

Description

@Kira-PH

My class is designed to get the items from an Amazon DynamoDB query and convert them to T through the magic of the Enumerator:

    public class DynamoQueryResult<T> : IEnumerable<T> where T : class, new()
    {
        public int Count;
        public Dictionary<string, DynamoValue>[] Items;
        public IEnumerator<T> GetEnumerator()
        {
            return new DynamoQueryResultEnumerator<T>(this);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new DynamoQueryResultEnumerator<T>(this);
        }
    }

Json Deserializers detect IEnumerable and expect the JSON at that level to therefore be a set (where this class represents a complex object containing both set metadata and set members). I can override that behaviour with Newtonsoft by adding the [JsonObject] attribute to the object and it does the job perfectly - and Newtonsoft's exception message gives a load of help which leads to this. I can't find a way to do the same for System.Text.Json. Also, really descriptive exception messages that guide the developer are one of my favourite things to create and see.

(Changing to an IReadOnlyCollection doesn't help, but ReadOnly IEnumerables could be a way to 'hint' that they're not meant to be deserialized into?)

  1. I know there is a big library of junk for dealing with Amazon stuff. That is not the answer I'm looking for - it's likely to just add delay and potentially more questions. This is a working example of where this falls over.
  2. In the immediate term, I'm likely to just create a customised collection to replace the array of dictionaries, but it would be cool if I could just convince the deserializer to fill the object.

Activity

  1. changed the title [-]JsonSerializer.Deserialize<T> cannot serialize into custom IEnumerable[/-] [+]JsonSerializer.Deserialize<T> cannot deserialize into custom IEnumerable[/+] on Sep 14, 2019
  2. self-assigned this
    on Nov 5, 2019
  3. removed their assignment
    on Dec 3, 2019
  4. layomia commented on Jan 31, 2020

    @layomia
    Contributor

    ReadOnly IEnumerables could be a way to 'hint' that they're not meant to be deserialized into

    I don't think this is enough information for the serializer to change the normal behavior.

    It seems that the way to move forward here is if there were an explicit way to tell the serializer to treat an IEnumerable like an object with properties, similar to Newtonsoft's [JsonObject] attribute. I'll rename this issue to reflect this.

  5. changed the title [-]JsonSerializer.Deserialize<T> cannot deserialize into custom IEnumerable[/-] [+]Provide option for the serializer to treat IEnumerables like objects with members[/+] on Jan 31, 2020
  6. transferred this issue fromdotnet/corefxon Feb 1, 2020
  7. added this to the 5.0 milestone on Feb 1, 2020
  8. layomia commented on Feb 1, 2020

    @layomia
    Contributor

    Closing as dup of #1808.

  9. 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

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions