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

[question] Can I ignore all properties and field as default? #10

Closed
TMaddox opened this issue Aug 26, 2019 · 2 comments
Closed

[question] Can I ignore all properties and field as default? #10

TMaddox opened this issue Aug 26, 2019 · 2 comments
Labels
question Further information is requested

Comments

@TMaddox
Copy link

TMaddox commented Aug 26, 2019

I want to ignore all properties and fields in all classes for serialization by default and only serialize them when decorated with an [CborProperty("name")] attribute. Ist that possible? If not I would like to request this feature.

@mcatanzariti mcatanzariti added the question Further information is requested label Aug 26, 2019
@mcatanzariti
Copy link
Member

Yes you can but it will require a little bit of code:

        public class OptInObjectMappingConventionProvider : IObjectMappingConventionProvider
        {
            public IObjectMappingConvention GetConvention(Type type)
            {
                // here you could filter which type should be optIn and return null for other types
                return new OptInObjectMappingConvention();
            }
        }

        public class OptInObjectMappingConvention : IObjectMappingConvention
        {
            private readonly DefaultObjectMappingConvention _defaultConvention = new DefaultObjectMappingConvention();

            public void Apply<T>(SerializationRegistry registry, ObjectMapping<T> objectMapping) where T : class
            {
                _defaultConvention.Apply(registry, objectMapping);

                // restrict to members holding CborPropertyAttribute
                objectMapping.SetMemberMappings(objectMapping.MemberMappings
                    .Where(m => m.MemberInfo.IsDefined(typeof(CborPropertyAttribute)))
                    .ToList());
            }
        }

        public class OptInObject1
        {
            [CborProperty]
            public int Id { get; set; }

            public string Name { get; set; }
        }

        public class OptInObject2
        {
            public int Id { get; set; }

            [CborProperty]
            public string Name { get; set; }
        }

        public void OptInTest()
        {
            CborOptions options = new CborOptions();
            options.Registry.ObjectMappingConventionRegistry.RegisterProvider(
                new OptInObjectMappingConventionProvider()
            );

            OptInObject1 obj1 = new OptInObject1 { Id = 12, Name = "foo" };
            const string hexBuffer1 = "A16249640C";
            Cbor.Serialize(obj1, inputBuffer, options);

            OptInObject2 obj2 = new OptInObject2 { Id = 12, Name = "foo" };
            const string hexBuffer2 = "A1644E616D6563666F6F";
            Cbor.Serialize(obj2, inputBuffer, options);
        }

@TMaddox
Copy link
Author

TMaddox commented Aug 26, 2019

works like a charm, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants