Skip to content

Use internal constructors in JSON deserialization with Blazor JS Interop #63124

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


I'm building classes in an RCL to use in Blazor JS Interop.

For internal properties I can use [JsonInclude].
I can't find a way to use internal constructors?
I notice there is a RespectRequiredConstructorParameters option (not sure if this would work anyways) but I don't see a way to apply that to a specific class during JS Interop deserialization.

So, I have a class the exposes a public constructor requiring an Enum value, and a public constructor for JS Interop:

        [JsonConstructor]
        public Geometry() { }

        public Geometry(GeometryType geometryType)
        {
            Type = geometryType.ToString();
        }

        [JsonInclude]
        internal string Type { get; set; } = string.Empty;

[JsonInclude] does not work with constructors.

I would rather make the JsonConstructor internal and force any external instances to use the constructor with the Enum value.

Unless there is some method to already do this?


Original Comments

Aeron Jay Omega (CSI INTERFUSION INC) [MSFT] on 7/30/2025, 10:10 AM:

We have converted this feedback item to a problem. This change was done to better reflect the feedback’s nature. It will allow other developers to easily find it and engage on it.

Feedback Bot on 7/31/2025, 05:22 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Douglas Marquardt on 8/2/2025, 06:05 PM:

Also, I’d like to mention that PreferredObjectCreationHandling appears to be ignored during deserialization.
It was my understanding that if the option is set then ‘get’ only properties should populate, i.e.

[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
public class MyClass
{
    public double? Zoom { get; }
|

However, the only way I could get the value to populate on deserialization was to add [JsonInclude] and an internal set, i.e.

[JsonInclude] public double? Zoom { get; internal set; }

Unless I am missing something here as well?

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Author: Migration Bot 🤖The issue was created by a issue mover bot. The author may not be the actual author.Status: Resolvedarea-blazorIncludes: Blazor, Razor Componentsquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions