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

Disable serialization for specific types #862

Open
EdwardCooke opened this issue Oct 30, 2023 Discussed in #861 · 1 comment
Open

Disable serialization for specific types #862

EdwardCooke opened this issue Oct 30, 2023 Discussed in #861 · 1 comment

Comments

@EdwardCooke
Copy link
Collaborator

Discussed in #861

Originally posted by JamesTheButler October 30, 2023
Hi everyone!

I am serializing a class that contains a public Action field. Consider the following example:

record class Example
{
  public int Int { get; private set; }

  public Action? SomeAction;
};

public void Test()
{
  var serializer = new SerializerBuilder().Build();
  var output = serializer.Serialize(new DemoClass());
  // output:
  //  SomeAction: 
  //  Int: 0
}

SomeAction gets serialized to an empty string in yaml, which is a bit annoying but still okay. It get's fun if we actually subscribe to SomeAction when the serializer generates ~5.000 lines of yaml to describe the attached action.

Is it possible to not serialize members of a given type? I know about [YamlIgnore] but I would rather set up my serializer to ignore all members of the type Action (and possibly other types as well) in any of my serializable classes.

@EdwardCooke
Copy link
Collaborator Author

You can do this with a custom type inspector.

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.TypeInspectors;

var data = new Receiver();

var serializer = new SerializerBuilder()
    .WithTypeInspector(inner=>new IgnoreTypeTypeInspector(inner), where=>where.OnBottom())
    .Build();
var yaml = serializer.Serialize(data);
Console.WriteLine(yaml);

public class IgnoreTypeTypeInspector : TypeInspectorSkeleton
{
    private readonly ITypeInspector innerTypeDescriptor;

    public IgnoreTypeTypeInspector(ITypeInspector innerTypeDescriptor)
    {
        this.innerTypeDescriptor = innerTypeDescriptor ?? throw new ArgumentNullException(nameof(innerTypeDescriptor));
    }

    public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object? container)
    {
        var properties = innerTypeDescriptor.GetProperties(type, container)
            .Where(p => p.Type != typeof(int));
        return properties;
    }
}

public class Receiver
{
    public string ReceiverType = Guid.NewGuid().ToString();
    public string ReceiverName = Guid.NewGuid().ToString();
    public int IgnoreMyIntType = 0;
}

Results in

ReceiverType: 24329920-d193-4e5b-b849-1b033e46c3a6
ReceiverName: f9e33caa-86e3-4c12-8931-ec303a74b59f

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

1 participant