Skip to content

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

Description

@logcorner

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.JsonquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions