Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
HughPH opened this issue Sep 14, 2019 · 2 comments
Closed

Comments

@HughPH
Copy link

HughPH commented Sep 14, 2019

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.
@HughPH HughPH changed the title JsonSerializer.Deserialize<T> cannot serialize into custom IEnumerable JsonSerializer.Deserialize<T> cannot deserialize into custom IEnumerable Sep 14, 2019
@layomia layomia self-assigned this Nov 5, 2019
@layomia layomia removed their assignment Dec 3, 2019
@layomia
Copy link
Contributor

layomia commented Jan 31, 2020

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.

@layomia layomia changed the title JsonSerializer.Deserialize<T> cannot deserialize into custom IEnumerable Provide option for the serializer to treat IEnumerables like objects with members Jan 31, 2020
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@layomia
Copy link
Contributor

layomia commented Feb 1, 2020

Closing as dup of #1808.

@layomia layomia closed this as completed Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants