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

Add non-primitive value support for dictionaries in JsonSerializer #29337

Closed
5 tasks done
layomia opened this issue Apr 21, 2019 · 2 comments · Fixed by dotnet/corefx#37186
Closed
5 tasks done

Add non-primitive value support for dictionaries in JsonSerializer #29337

layomia opened this issue Apr 21, 2019 · 2 comments · Fixed by dotnet/corefx#37186
Assignees
Labels
area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work
Milestone

Comments

@layomia
Copy link
Contributor

layomia commented Apr 21, 2019

Currently, the serializer only supports primitives (int32, string, float etc.) as dictionary values.

Support for non-primitive types should also be added:

  • Dictionary<string, MyConcreteClass>
  • Dictionary<string, List<T>>
  • List<Dictionary<string, T>
  • Dictionary<string, Dictionary<string, T>>
  • Polymorphic support (serialize only): Dictionary<string, object>

cc @steveharter

@layomia layomia changed the title Extend value support for dictionaries in JsonSerializer Add non-primitive value support for dictionaries in JsonSerializer Apr 21, 2019
@layomia layomia assigned layomia and steveharter and unassigned layomia Apr 21, 2019
@Joelius300
Copy link

Will there ever be serialization-support for Dictionary<object, object>? Other json-libraries simply call .ToString() on the key in order to get the json-string-key. Currently there is no support for this even if you fully implement and override .ToString() for the object you want to use as key.
If it's not a possibility to make this the default behaviour, it would be nice to at least have the option for this maybe through an attribute or the serialization options.

What do you think?

Ps. if this is the wrong place for this kind of question I can also open an issue but I thought since this is very closely related to this issue I'll just comment on here.

@adrianm64
Copy link

adrianm64 commented Aug 6, 2019

polymorphic IReadOnlyDictionary doesn't work for me.
Primitive works fine
Works in Json.net

internal sealed class ValueCollection : IReadOnlyDictionary<string, object>
{
    private readonly List<KeyValuePair<string, object>> _values = new List<KeyValuePair<string, object>>();

    public void AddValue(string key, object value) => this._values.Add(new KeyValuePair<string, object>(key, value));

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() => this._values.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => this._values.GetEnumerator();

    int IReadOnlyCollection<KeyValuePair<string, object>>.Count { get => this._values.Count; }
    bool IReadOnlyDictionary<string, object>.ContainsKey(string key) => throw new System.NotImplementedException();
    bool IReadOnlyDictionary<string, object>.TryGetValue(string key, out object value) => throw new System.NotImplementedException();
    object IReadOnlyDictionary<string, object>.this[string key] { get => throw new System.NotImplementedException(); }
    IEnumerable<string> IReadOnlyDictionary<string, object>.Keys { get => throw new System.NotImplementedException(); }
    IEnumerable<object> IReadOnlyDictionary<string, object>.Values { get => throw new System.NotImplementedException(); }
}
    var vc = new ValueCollection();
    vc.AddValue("x", 1);
    
    JsonSerializer.Serialize<IReadOnlyDictionary<string, object>>(vc).Dump();

Error message is

Unable to cast object of type 'Enumerator[System.Collections.Generic.KeyValuePair`2[System.String,System.Object]]' to type 'System.Collections.IDictionaryEnumerator'.

Seems to be multiple problems:

  • Not detecting the object as a generic dictionary and calls non-generic IEnumerable.GetEnumerator
  • Assuming IEnumerable.GetEnumerator returns an IDictionaryEnumerator
    (might be a BCL convention)

update: using System.Text.Json preview7.19362.9 in LinqPad 5 (Framework 4.7.2)

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
dougbu added a commit to dotnet/aspnetcore that referenced this issue Aug 25, 2020
dougbu added a commit to dotnet/aspnetcore that referenced this issue Aug 30, 2020
dougbu added a commit to dotnet/aspnetcore that referenced this issue Sep 7, 2020
dougbu added a commit to dotnet/aspnetcore that referenced this issue Sep 9, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants