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

[feature request] add possiblility to filter/modify object structure on serializing and deserializing #6

Closed
TMaddox opened this issue Aug 19, 2019 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@TMaddox
Copy link

TMaddox commented Aug 19, 2019

I have to serialize the same object in different ways, with JSON (Newtonsoft.Json library) this is possible with a custom contract, which can be added to the serializer (Reference: https://www.newtonsoft.com/json/help/html/CustomContractResolver.htm)

Do you plan on adding something like this?

@mcatanzariti
Copy link
Member

Hi TMaddox,

Could you please provide detailed examples of the different ways you would like to serialize the same object?

Newtonsoft json contracts have many options and I'm not sure which one you would like to see implemented

@TMaddox
Copy link
Author

TMaddox commented Aug 19, 2019

Sorry for the unclear OP, I'll try to find a small and accurate example

Basically I want to choose at runtime, wether a property or field should be serialized or not.

Consider the following classes:

public class Tree
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Tree() { }
        ....
    }

public class MyContract : CborBaseContract
    {
        private bool someOtherCondition; // Instanciated in ctor

        protected override IList<CborProperty> CreateProperties(Type type)
        {
            IList<CborProperty> properties = base.CreateProperties(type);

            if (typeof(Tree) == type && someOtherCondition)
            {
                properties.Where(p => p.PropertyName == "Name").ToList();
            }

            return properties;
        }
    }

Now I want to do this:

public void MyMethod()
    {
        CborContract myCustomCborContract = new MyContract();
        Cbor.Contract = myCustomCborContract;
        var serialized = Cbor.Serialize(new Tree("myTree", 10))      // I know this won't work, just a concept to keep it simple
        // serialized should contain the property "Name", but not "Age"
    }

@mcatanzariti
Copy link
Member

@mcatanzariti
Copy link
Member

mcatanzariti commented Aug 25, 2019

Syntax for your example:

public class Tree
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Tree() { }
    ....
}

public void MyMethod()
{
    CborOptions options = new CborOptions();
    options.Registry.ObjectMappingRegistry.Register<Tree>(objectMapping =>
        objectMapping
            .AutoMap()
            .ClearMemberMappings()
            .MapMember(o => o.Name)
    );

    var serialized = Cbor.Serialize(new Tree("myTree", 10), options);
}

@TMaddox
Copy link
Author

TMaddox commented Aug 26, 2019

Thank you very much for implementing all my featue requests! I really appriciate it!

@mcatanzariti
Copy link
Member

You're welcome!
Please let me know if all is working as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants