|
| 1 | +using Dahomey.Cbor.Serialization; |
| 2 | +using Dahomey.Cbor.Serialization.Converters.Mappings; |
| 3 | +using System; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Dahomey.Cbor.Tests.Issues |
| 7 | +{ |
| 8 | + public class Issue0051 |
| 9 | + { |
| 10 | + public class ObjectMappingConventionProvider : IObjectMappingConventionProvider |
| 11 | + { |
| 12 | + private readonly IObjectMappingConvention _objectMappingConvention; |
| 13 | + |
| 14 | + public ObjectMappingConventionProvider() |
| 15 | + { |
| 16 | + _objectMappingConvention = new ObjectMappingConvention(); |
| 17 | + } |
| 18 | + |
| 19 | + public IObjectMappingConvention GetConvention(Type type) |
| 20 | + { |
| 21 | + return _objectMappingConvention; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public class ObjectMappingConvention : IObjectMappingConvention |
| 26 | + { |
| 27 | + private readonly DefaultObjectMappingConvention _defaultObjectMappingConvention = new DefaultObjectMappingConvention(); |
| 28 | + |
| 29 | + public void Apply<T>(SerializationRegistry registry, ObjectMapping<T> objectMapping) where T : class |
| 30 | + { |
| 31 | + _defaultObjectMappingConvention.Apply<T>(registry, objectMapping); |
| 32 | + objectMapping.SetOrderBy(m => m.MemberName); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public class MyObject |
| 37 | + { |
| 38 | + public int C { get; set; } |
| 39 | + public int A { get; set; } |
| 40 | + public int B { get; set; } |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void TestWrite() |
| 45 | + { |
| 46 | + CborOptions options = new CborOptions(); |
| 47 | + options.Registry.ObjectMappingConventionRegistry.RegisterProvider(new ObjectMappingConventionProvider()); |
| 48 | + |
| 49 | + var obj = new MyObject |
| 50 | + { |
| 51 | + C = 1, |
| 52 | + A = 2, |
| 53 | + B = 3 |
| 54 | + }; |
| 55 | + |
| 56 | + // {"A": 2, "B": 3, "C": 1} |
| 57 | + const string hexBuffer = "A3614102614203614301"; |
| 58 | + |
| 59 | + Helper.TestWrite(obj, hexBuffer, null, options); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments