Example #1
0
        public void Options()
        {
            CborOptions options = new CborOptions
            {
                MapLengthMode = LengthMode.IndefiniteLength
            };

            Object obj = new Object
            {
                Id   = 12,
                Name = "foo"
            };

            const string hexBuffer = "BF6249640C644E616D6563666F6FFF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
        public void TestWrite()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingConventionRegistry.RegisterProvider(new ObjectMappingConventionProvider());

            var obj = new MyObject
            {
                C = 1,
                A = 2,
                B = 3
            };

            // {"A": 2, "B": 3, "C": 1}
            const string hexBuffer = "A3614102614203614301";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #3
0
        public EnumConverter(CborOptions options)
        {
            _options = options;

            string[] names  = Enum.GetNames(typeof(T));
            T[]      values = Enum.GetValues(typeof(T)).Cast <T>().ToArray();

            values2Names = new Dictionary <T, ReadOnlyMemory <byte> >(names.Length);

            for (int i = 0; i < names.Length; i++)
            {
                T value = values[i];
                ReadOnlyMemory <byte> name = Encoding.ASCII.GetBytes(names[i]);

                names2Values.Add(name.Span, value);
                values2Names[value] = name;
            }
        }
Example #4
0
        public void ReadReadOnlyFieldWithNonDefaultConstructor()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <Tree>(om =>
            {
                om.AutoMap();
                om.MapCreator(t => new Tree(t._name))
                .SetMemberNames("name");
            });

            const string hexBuffer = "A1646E616D6563666F6F";

            Tree tree = Helper.Read <Tree>(hexBuffer, options);

            Assert.NotNull(tree);
            Assert.Equal("foo", tree._name);
        }
Example #5
0
        public void TestWrite(RequirementPolicy requirementPolicy, string value, Type expectedExceptionType)
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <StringObject>(objectMapping =>
                                                                           objectMapping
                                                                           .AutoMap()
                                                                           .ClearMemberMappings()
                                                                           .MapMember(o => o.String).SetRequired(requirementPolicy)
                                                                           );

            StringObject obj = new StringObject
            {
                String = value
            };

            Helper.TestWrite(obj, expectedExceptionType, options);
        }
Example #6
0
        public void PropertyWithDefiniteLengthAttribute()
        {
            CborOptions options = new CborOptions
            {
                ArrayLengthMode = LengthMode.IndefiniteLength
            };

            ObjectWithPropertyWithDefiniteAttribute obj = new ObjectWithPropertyWithDefiniteAttribute
            {
                List = new List <int> {
                    1, 2, 3
                }
            };

            const string hexBuffer = "A1644C69737483010203";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
        public override ICborConverter?GetConverter(Type type, CborOptions options)
        {
            if (_converterTypes.TryGetValue(type, out Type? converterType))
            {
                return(CreateConverter(options, converterType));
            }

            if (type.IsEnum)
            {
                return(CreateGenericConverter(options, typeof(EnumConverter <>), type));
            }

            if (typeof(CborValue).IsAssignableFrom(type))
            {
                return(CreateConverter(options, typeof(CborValueConverter)));
            }

            return(null);
        }
Example #8
0
        public void ReadWithDiscriminator()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry));
            options.Registry.ObjectMappingRegistry.Register <InheritedObject>(objectMapping =>
                                                                              objectMapping
                                                                              .AutoMap()
                                                                              .SetDiscriminator("inherited")
                                                                              );

            const string hexBuffer = "A3625F7469696E686572697465646E496E6865726974656456616C75650D694261736556616C75650C";
            BaseObject   obj       = Helper.Read <BaseObject>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.IsType <InheritedObject>(obj);
            Assert.Equal(12, obj.BaseValue);
            Assert.Equal(13, ((InheritedObject)obj).InheritedValue);
        }
Example #9
0
        public void WriteWithNamingConvention()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <IntObject>(objectMapping =>
                                                                        objectMapping
                                                                        .AutoMap()
                                                                        .SetNamingConvention(new CamelCaseNamingConvention())
                                                                        );

            IntObject obj = new IntObject
            {
                IntValue = 12
            };

            const string hexBuffer = "A168696E7456616C75650C";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #10
0
        public bool ShouldSerialize(object obj, Type declaredType, CborOptions options)
        {
            if (_memberGetter == null)
            {
                throw new CborException($"No member getter for '{Encoding.UTF8.GetString(_memberName.Span)}'");
            }

            if (IgnoreIfDefault && EqualityComparer <TM> .Default.Equals(_memberGetter((T)obj), _defaultValue))
            {
                return(false);
            }

            if (_shouldSerializeMethod != null && !_shouldSerializeMethod(obj))
            {
                return(false);
            }

            return(true);
        }
        [InlineData(13, "A16249640D")] // { "Id": 13}
        public void TestCustomMethod(int id, string hexBuffer)
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithShouldSerialize2>(objectMapping =>
                                                                                         objectMapping
                                                                                         .AutoMap()
                                                                                         .ClearMemberMappings()
                                                                                         .MapMember(o => o.Id)
                                                                                         .SetShouldSerializeMethod(o => ((ObjectWithShouldSerialize2)o).Id != 12)
                                                                                         );

            ObjectWithShouldSerialize2 obj = new ObjectWithShouldSerialize2
            {
                Id = id
            };

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #12
0
        public void ReadWithMemberNameAndConverter()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithGuid>(objectMapping =>
                                                                             objectMapping
                                                                             .AutoMap()
                                                                             .ClearMemberMappings()
                                                                             .MapMember(o => o.Guid)
                                                                             .SetConverter(new GuidConverter())
                                                                             .SetMemberName("g")
                                                                             );

            const string   hexBuffer = "A1616750E2AA33E949D7AE42B8628AC805DF082F";
            ObjectWithGuid obj       = Helper.Read <ObjectWithGuid>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.Equal(Guid.Parse("E933AAE2-D749-42AE-B862-8AC805DF082F"), obj.Guid);
        }
Example #13
0
        public void IndefiniteLengthObjectMapping()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <Object>(objectMapping =>
                                                                     objectMapping
                                                                     .AutoMap()
                                                                     .SetLengthMode(LengthMode.IndefiniteLength)
                                                                     );

            Object obj = new Object
            {
                Id   = 12,
                Name = "foo"
            };

            const string hexBuffer = "BF6249640C644E616D6563666F6FFF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
        public void PropertyWithDefiniteLengthAttribute()
        {
            CborOptions options = new CborOptions
            {
                MapLengthMode = LengthMode.IndefiniteLength
            };

            ObjectWithPropertyWithDefiniteAttribute obj = new ObjectWithPropertyWithDefiniteAttribute
            {
                Dictionary = new Dictionary <int, string>
                {
                    { 1, "foo" },
                    { 2, "bar" }
                }
            };

            const string hexBuffer = "BF6A44696374696F6E617279A20163666F6F0263626172FF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #15
0
        public void PropertyWithDefiniteLengthAttribute()
        {
            CborOptions options = new CborOptions
            {
                MapLengthMode = LengthMode.IndefiniteLength
            };

            ObjectWithPropertyWithDefiniteAttribute obj = new ObjectWithPropertyWithDefiniteAttribute
            {
                Object = new Object
                {
                    Id   = 12,
                    Name = "foo"
                }
            };

            const string hexBuffer = "BF664F626A656374A26249640C644E616D6563666F6FFF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #16
0
        public void FactoryByApi(string hexBuffer, int expectedId, string expectedName, int expectedAge)
        {
            Factory factory = new Factory();
            Func <int, string, ObjectWithConstructor> creatorFunc = (int id, string name) => factory.NewObjectWithConstructor(id, name);

            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithConstructor>(objectMapping =>
                                                                                    objectMapping
                                                                                    .AutoMap()
                                                                                    .MapCreator(creatorFunc)
                                                                                    );

            ObjectWithConstructor obj = Helper.Read <ObjectWithConstructor>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.Equal(expectedId, obj.Id);
            Assert.Equal(expectedName, obj.Name);
            Assert.Equal(expectedAge, obj.Age);
        }
Example #17
0
        public void Interface()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <IFoo>(objectMapping =>
                                                                   objectMapping
                                                                   .AutoMap()
                                                                   .MapCreator(o => new Foo())
                                                                   );

            const string        hexBuffer = "A163466F6FA16249640C";
            ObjectWithInterface obj       = Helper.Read <ObjectWithInterface>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.NotNull(obj.Foo);
            Assert.IsType <Foo>(obj.Foo);
            Assert.Equal(12, obj.Foo.Id);

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
        public void TestWriteByApi()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <Tree>(objectMapping =>
            {
                objectMapping.AutoMap();
                objectMapping.MapMember(
                    typeof(Tree)
                    .GetField(nameof(Tree.Id), BindingFlags.Public | BindingFlags.Static),
                    typeof(string));
                objectMapping.MapMember(tree => tree.Name);
                objectMapping.MapMember(tree => Tree.WhatEver);
            });

            Tree         obj       = new Tree();
            const string hexBuffer = "A4644E616D65694C656D6F6E547265656249646A547265652E636C617373644E616D65694C656D6F6E547265656857686174457665720C";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #19
0
        public void TestReadByApi()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithCallbacks>(om =>
            {
                om.MapMember(o => o.Id);
                om.SetOnDeserializingMethod(o => o.OnDeserializing());
                om.SetOnDeserializedMethod(o => o.OnDeserialized());
            });

            const string        hexBuffer = "A16249640C"; // {"Id":12}
            ObjectWithCallbacks obj       = Helper.Read <ObjectWithCallbacks>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.True(obj.OnDeserializingCalled);
            Assert.True(obj.OnDeserializedCalled);
            Assert.False(obj.OnSerializingCalled);
            Assert.False(obj.OnSerializedCalled);
        }
        public void WriteWithCustomDiscriminator()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new CustomDiscriminatorConvention());
            options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject));

            const string hexBuffer = "A26A426173654F626A656374A364747970651A22134C83644E616D6563666F6F624964016A4E616D654F626A656374F6";

            BaseObjectHolder obj = new BaseObjectHolder
            {
                BaseObject = new NameObject
                {
                    Id   = 1,
                    Name = "foo"
                },
            };

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
        public void Test()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry, "serializer"));
            options.Registry.ObjectMappingRegistry.Register <Atom>(objectMapping =>
            {
                objectMapping.AutoMap();
                objectMapping.SetOrderBy(m => m.MemberName);
            });

            Atom obj = new Atom
            {
                A = 10,
                Z = 10
            };

            const string hexBuffer = "A361410A6A73657269616C697A65726461746F6D615A0A";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #22
0
        public void WriteWithDiscriminator()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry));
            options.Registry.ObjectMappingRegistry.Register <InheritedObject>(objectMapping =>
                                                                              objectMapping
                                                                              .SetDiscriminator("inherited")
                                                                              .AutoMap()
                                                                              );

            InheritedObject obj = new InheritedObject
            {
                BaseValue      = 12,
                InheritedValue = 13
            };

            const string hexBuffer = "A3625F7469696E686572697465646E496E6865726974656456616C75650D694261736556616C75650C";

            Helper.TestWrite <BaseObject>(obj, hexBuffer, null, options);
        }
Example #23
0
        public void PropertyIndefiniteLengthObjectMapping()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithProperty>(om =>
            {
                om.AutoMap();
                om.ClearMemberMappings();
                om.MapMember(o => o.List).SetLengthMode(LengthMode.IndefiniteLength);
            });

            ObjectWithProperty obj = new ObjectWithProperty
            {
                List = new List <int> {
                    1, 2, 3
                }
            };

            const string hexBuffer = "A1644C6973749F010203FF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #24
0
        public void AbstractClass()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <AbstractBar>(objectMapping =>
                                                                          objectMapping
                                                                          .AutoMap()
                                                                          .MapCreator(o => new Bar())
                                                                          );

            const string            hexBuffer = "A163426172A16249640C";
            ObjectWithAbstractClass obj       = Helper.Read <ObjectWithAbstractClass>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.NotNull(obj.Bar);
            Assert.IsType <Bar>(obj.Bar);
            Assert.Equal(12, obj.Bar.Id);

            Helper.TestRead(hexBuffer, (ObjectWithAbstractClass)null, typeof(CborException));

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #25
0
        public void WriteWithMemberNameAndConverter()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithGuid>(objectMapping =>
                                                                             objectMapping
                                                                             .AutoMap()
                                                                             .ClearMemberMappings()
                                                                             .MapMember(o => o.Guid)
                                                                             .SetConverter(new GuidConverter())
                                                                             .SetMemberName("g")
                                                                             );

            ObjectWithGuid obj = new ObjectWithGuid
            {
                Guid = Guid.Parse("E933AAE2-D749-42AE-B862-8AC805DF082F")
            };

            const string hexBuffer = "A1616750E2AA33E949D7AE42B8628AC805DF082F";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #26
0
        public void OptIn()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingConventionRegistry.RegisterProvider(
                new OptInObjectMappingConventionProvider()
                );

            OptInObject1 obj1 = new OptInObject1 {
                Id = 12, Name = "foo"
            };
            const string hexBuffer1 = "A16249640C";

            Helper.TestWrite(obj1, hexBuffer1, null, options);

            OptInObject2 obj2 = new OptInObject2 {
                Id = 12, Name = "foo"
            };
            const string hexBuffer2 = "A1644E616D6563666F6F";

            Helper.TestWrite(obj2, hexBuffer2, null, options);
        }
        public static void TestWrite <T>(T value, Type expectedExceptionType = null, CborOptions options = null)
        {
            if (expectedExceptionType != null)
            {
                bool exceptionCatched = false;

                try
                {
                    Write(value, options);
                }
                catch (Exception ex)
                {
                    Assert.IsType(expectedExceptionType, ex);
                    exceptionCatched = true;
                }

                Assert.True(exceptionCatched, $"Expected exception {expectedExceptionType}");
            }
            else
            {
                Write(value, options);
            }
        }
Example #28
0
        public void PropertyIndefiniteLengthObjectMapping()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithProperty>(om =>
            {
                om.AutoMap();
                om.ClearMemberMappings();
                om.MapMember(o => o.Object).SetLengthMode(LengthMode.IndefiniteLength);
            });

            ObjectWithProperty obj = new ObjectWithProperty
            {
                Object = new Object
                {
                    Id   = 12,
                    Name = "foo"
                }
            };

            const string hexBuffer = "A1664F626A656374BF6249640C644E616D6563666F6FFF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
        public void PropertyIndefiniteLengthObjectMapping()
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <ObjectWithProperty>(om =>
            {
                om.AutoMap();
                om.ClearMemberMappings();
                om.MapMember(o => o.Dictionary).SetLengthMode(LengthMode.IndefiniteLength);
            });

            ObjectWithProperty obj = new ObjectWithProperty
            {
                Dictionary = new Dictionary <int, string>
                {
                    { 1, "foo" },
                    { 2, "bar" }
                }
            };

            const string hexBuffer = "A16A44696374696F6E617279BF0163666F6F0263626172FF";

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
Example #30
0
        public void TestReadWithAttribute(string hexBuffer, Type expectedExceptionType)
        {
            CborOptions options = new CborOptions();

            Helper.TestRead <StringObjectWithAttribute>(hexBuffer, expectedExceptionType, options);
        }