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

Each parameter in the deserialization constructor on type 'SpeechCreatedEvent' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.' #69547

Closed
logcorner opened this issue May 19, 2022 · 4 comments
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@logcorner
Copy link

logcorner commented May 19, 2022

Hi I'm migrating from newtonsoft.json to System.Text.Json using .NET6 I have an issues when deserializing json string into object.

The error message is Each parameter in the deserialization constructor on type 'SpeechCreatedEvent' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.'

You can find the sample code in the following repository https://github.com/logcorner/DotNetJsonSerilizer

It works fine with newtonsoft.json but raise an exception when using System.Text.Json

 JsonSerializerOptions settings = new JsonSerializerOptions
            {
                IncludeFields = true
            };
            var result = (TEvent)JsonSerializer.Deserialize(serializedEvent, Type.GetType(eventType), settings);`

With Newtonsoft.Json I have implemented a DefaultContractResolver as following

private class PrivateSetterContractResolver : DefaultContractResolver
        {
            protected override JsonProperty CreateProperty(
                MemberInfo member,
                MemberSerialization memberSerialization)
            {
                var prop = base.CreateProperty(member, memberSerialization);
                if (!prop.Writable)
                {
                    var property = member as PropertyInfo;
                    if (property != null)
                    {
                        var hasPrivateSetter = property.GetSetMethod(true) != null;
                        prop.Writable = hasPrivateSetter;
                    }
                }
                return prop;
            }
        }

Do I need to implement a JsonConverter with System.Text.Json ?
Regards

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 19, 2022
@ghost
Copy link

ghost commented May 19, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Hi I'm migrating from newtonsoft.json to System.Text.Json using .NET6 I have an issues when deserializing json string into object.

The error message is Each parameter in the deserialization constructor on type 'SpeechCreatedEvent' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.'

You can find the sample code in the following repository https://github.com/logcorner/DotNetJsonSerilizer

It works fine with newtonsoft.json but raise an exception when using System.Text.Json
JsonSerializerOptions settings = new JsonSerializerOptions { IncludeFields = true }; var result = (TEvent)JsonSerializer.Deserialize(serializedEvent, Type.GetType(eventType), settings);

With Newtonsoft.Json I have implemented a DefaultContractResolver as following
private class PrivateSetterContractResolver : DefaultContractResolver { protected override JsonProperty CreateProperty( MemberInfo member, MemberSerialization memberSerialization) { var prop = base.CreateProperty(member, memberSerialization); if (!prop.Writable) { var property = member as PropertyInfo; if (property != null) { var hasPrivateSetter = property.GetSetMethod(true) != null; prop.Writable = hasPrivateSetter; } } return prop; } }

Do I need to implement a JsonConverter with System.Text.Json ?
Regards

Author: logcorner
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis eiriktsarpalis added question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels May 19, 2022
@eiriktsarpalis
Copy link
Member

Hi, we plan to support custom contract resolution in .NET 7 via #63686. Until then, it won't be possible to modify the list of serializable properties I'm afraid.

Duplicate of #63686

@logcorner
Copy link
Author

logcorner commented May 19, 2022

So I'm suspending the migration for now.

I can't use System.Text.Json if I can't deserialize complex types like private setters or inherited properties

I would like this feature to be implemented soon
Regard

@danmoseley
Copy link
Member

#43563

@ghost ghost locked as resolved and limited conversation to collaborators Jun 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants