-
Notifications
You must be signed in to change notification settings - Fork 14
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
Labels
question
Further information is requested
Comments
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);
} |
works like a charm, thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: