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

[System.Text.Json] Deserializing dictionary with non-primitive types does not work #29504

Closed
pranavkm opened this issue May 9, 2019 · 4 comments · Fixed by dotnet/corefx#37686
Assignees
Labels
area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work bug
Milestone

Comments

@pranavkm
Copy link
Contributor

pranavkm commented May 9, 2019

class Program
{
    static void Main(string[] args)
    {
        var dictionary = new Dictionary<string, object>
        {
            ["key"] = new Poco { Id = 10 },
        };

        var instance = JsonSerializer.ToString(dictionary);
        Console.WriteLine(instance);
    }
}

class Poco
{
    public int Id { get; set; }
}

This throws:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Cannot bind to the target method because its signature is not compatible with that of the delegate type.
   at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure)
   at System.Text.Json.Serialization.JsonPropertyInfoCommon`3..ctor(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type elementType, JsonSerializerOptions options)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Text.Json.Serialization.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)

Using Microsoft.NETCore.App 3.0.0-preview6-27709-05

@pranavkm
Copy link
Contributor Author

pranavkm commented May 9, 2019

@steveharter it looks like this should have been covered by https://github.com/dotnet/corefx/issues/37077. Am I missing something?

@pranavkm
Copy link
Contributor Author

In particular with Blazor, this is going to be a fairly common scenario to run in to. I worked around this in our tests, but it would be great if we fix this soon.

@steveharter
Copy link
Member

I assume this was handled already. System.Object is special because it used the JsonElement. @JeremyKuhne

@pranavkm
Copy link
Contributor Author

System.Object is special because it used the JsonElement

@steveharter this is during serialization. Shouldn't that use the reflected type and serialize the way object types are serialized elsewhere?

@JeremyKuhne JeremyKuhne self-assigned this May 15, 2019
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 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 bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants