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

Unable to serialize object that contains a required property that is tagged with [JsonIgnore] #111906

Closed
TonyValenti opened this issue Jan 28, 2025 · 2 comments

Comments

@TonyValenti
Copy link

Description

I have a record that contains a non-serializable member. For logging purposes, I want to serialize all properties on this object except for the single non-serializable one. The record will never be deserialized.

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Serialization;

namespace ConsoleApp14
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var Value = new Test() {
                NeverSerializeThis = nameof(Test.NeverSerializeThis),
                Value = "Test",
            };

            var Text = JsonSerializer.Serialize(Value);

            Console.WriteLine(Text);

        }
    }

    public record Test {
        [JsonIgnore()]
        public required object NeverSerializeThis { get; init; }
        public required string Value { get; init; }
        
    }

}

Expected behavior

I expect the record to serialize just fine.
The error I am receiving, ```
System.InvalidOperationException: 'JsonPropertyInfo 'NeverSerializeThis' defined in type 'ConsoleApp14.Test' is marked required but does not specify a setter.'

is appropriate if I tried to deserialize the object but not appropriate since I am trying to serialize it.

### Actual behavior

I get the error message:

System.InvalidOperationException: 'JsonPropertyInfo 'NeverSerializeThis' defined in type 'ConsoleApp14.Test' is marked required but does not specify a setter.'


### Regression?

I don't think so.

### Known Workarounds

Create a custom object that does not have the non-serializable member.

### Configuration

.NET 9.0

### Other information

_No response_
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 28, 2025
Copy link
Contributor

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

@eiriktsarpalis
Copy link
Member

Duplicate of #92330

@eiriktsarpalis eiriktsarpalis marked this as a duplicate of #92330 Jan 28, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants