Exemple #1
0
 public void ClassesAndStructsInListTest <TType>(IList <TType> _) where TType : Tester.ITestBase, new()
 {
     // Empty
     {
         using var stream = new MemoryStream();
         BinaronConvert.Serialize(new TType[0], stream);
         stream.Seek(0, SeekOrigin.Begin);
         var dest = BinaronConvert.Deserialize <TType[]>(stream);
         stream.Seek(0, SeekOrigin.Begin);
         var dest2 = (IEnumerable)BinaronConvert.Deserialize(stream);
         Assert.AreEqual(0, dest.Length);
         Assert.AreEqual(0, dest2.Count());
     }
     // Single
     {
         using var stream = new MemoryStream();
         BinaronConvert.Serialize(new[] { new TType {
                                              Value = 1
                                          } }, stream);
         stream.Seek(0, SeekOrigin.Begin);
         var dest = BinaronConvert.Deserialize <TType[]>(stream);
         stream.Seek(0, SeekOrigin.Begin);
         var dest2 = ((IEnumerable)BinaronConvert.Deserialize(stream)).ToArray();
         Assert.AreEqual(1, dest.Length);
         Assert.AreEqual(1, dest.Single().Value);
         Assert.AreEqual(1, dest2.Length);
         Assert.AreEqual(1, ((dynamic)dest2.Single()).Value);
     }
     // Multi
     {
         using var stream = new MemoryStream();
         BinaronConvert.Serialize(new[] { new TType {
                                              Value = 1
                                          }, new TType {
                                              Value = 2
                                          }, new TType {
                                              Value = 3
                                          } }, stream);
         stream.Seek(0, SeekOrigin.Begin);
         var dest = BinaronConvert.Deserialize <TType[]>(stream);
         Assert.AreEqual(3, dest.Length);
         stream.Seek(0, SeekOrigin.Begin);
         var dest2 = ((IEnumerable)BinaronConvert.Deserialize(stream)).ToArray();
         Assert.AreEqual(3, dest2.Length);
         {
             var expected = 0;
             foreach (var item in dest)
             {
                 Assert.AreEqual(++expected, item.Value);
             }
         }
         {
             var expected = 0;
             foreach (dynamic item in dest2)
             {
                 Assert.AreEqual(++expected, item.Value);
             }
         }
     }
 }
 public static T TestRoundTrip <T>(object val, SerializerOptions options)
 {
     using var stream = new MemoryStream();
     BinaronConvert.Serialize(val, stream, options);
     stream.Seek(0, SeekOrigin.Begin);
     return(BinaronConvert.Deserialize <T>(stream));
 }
Exemple #3
0
 public void NullableTest1(int?value)
 {
     using var stream = new MemoryStream();
     BinaronConvert.Serialize(value, stream);
     stream.Seek(0, SeekOrigin.Begin);
     Assert.AreEqual(value, BinaronConvert.Deserialize <int?>(stream));
 }
        public static (T, object) TestRoundTrip2 <T>(object val, SerializerOptions options)
        {
            using var stream = new MemoryStream();
            BinaronConvert.Serialize(val, stream, options);
            stream.Seek(0, SeekOrigin.Begin);
            var result1 = BinaronConvert.Deserialize <T>(stream);

            stream.Seek(0, SeekOrigin.Begin);
            var result2 = BinaronConvert.Deserialize(stream);

            return(result1, result2);
        }
Exemple #5
0
        private static void BinaronTest_Validate <T>(T input, Action <T> validate)
        {
            using var stream = new MemoryStream();
            BinaronConvert.Serialize(input, stream, new SerializerOptions {
                SkipNullValues = true
            });
            stream.Position = 0;

            var value = BinaronConvert.Deserialize <T>(stream);

            validate(value);
        }
Exemple #6
0
        public void DeserializeToDateTimeLocal()
        {
            var dt = DateTime.Now;

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(dt, stream);
            stream.Seek(0, SeekOrigin.Begin);
            var dest = BinaronConvert.Deserialize <DateTime>(stream, new DeserializerOptions {
                TimeZoneInfo = TimeZoneInfo.Local
            });

            Assert.AreEqual(dt, dest);
        }
Exemple #7
0
        private static void BinaronTest_Deserialize <T>(T input, int loop)
        {
            using var stream = new MemoryStream();
            BinaronConvert.Serialize(input, stream, new SerializerOptions {
                SkipNullValues = true
            });
            stream.Position = 0;

            for (var i = 0; i < loop; i++)
            {
                var _ = BinaronConvert.Deserialize <T>(stream);
                stream.Position = 0;
            }
        }
Exemple #8
0
        public void MemberSetterNullableType1(int?v1, string v2)
        {
            var tc1 = new TestClass1 {
                IntValue = v1, StringValue = v2
            };

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(tc1, stream);
            stream.Seek(0, SeekOrigin.Begin);
            var tc2 = BinaronConvert.Deserialize <TestClass1>(stream);

            Assert.AreEqual(v1, tc2.IntValue);
            Assert.AreEqual(v2, tc2.StringValue);
        }
Exemple #9
0
 private static void BinaronTest()
 {
     using var stream = new MemoryStream();
     for (var i = 0; i < 50; i++)
     {
         BinaronConvert.Serialize(Source, stream, new SerializerOptions {
             SkipNullValues = true
         });
         stream.Position = 0;
         var book = BinaronConvert.Deserialize <Book>(stream);
         stream.Position = 0;
         Trace.Assert(book.Title != null);
     }
 }
        public void ClassWithReadOnlyDictionaryTest()
        {
            var source = new ClassWithReadOnlyDictionary {
                Dictionary = new Dictionary <string, object> {
                    { "a key", "a value" }
                }
            };

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(source, stream);
            stream.Seek(0, SeekOrigin.Begin);
            dynamic dest = BinaronConvert.Deserialize(stream);

            Assert.AreEqual("a value", ((IDictionary <string, object>)dest.Dictionary)["a key"]);
        }
        public void EnumerableObjectEnumerableTest()
        {
            var multiLevelEnumerables = new InnerMultiLevelEnumerablesTestClass().Yield();

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(multiLevelEnumerables, stream, new SerializerOptions {
                SkipNullValues = true
            });
            stream.Position = 0;
            var response = BinaronConvert.Deserialize <IEnumerable <InnerMultiLevelEnumerablesTestClass> >(stream);
            var inner    = response.First();

            CollectionAssert.AreEqual(Enumerable.Range(0, 2), inner.MultiLevelEnumerables.First());
            CollectionAssert.AreEqual(Enumerable.Range(3, 4), inner.MultiLevelEnumerables.Last());
        }
Exemple #12
0
        public void RootListTest <TSourceItem>(Type sourceType, Type destType, object list, TSourceItem sourceItem, object destItem)
        {
            var source = list.DynamicCast(sourceType);

            if (source == null)
            {
                throw new InvalidCastException($"Bad test case. Can't cast source with type of '{list.GetType()}' to '{sourceType}'");
            }

            // Empty
            {
                using var stream = new MemoryStream();
                BinaronConvert.Serialize(source, stream);
                stream.Seek(0, SeekOrigin.Begin);
                var dest = (IEnumerable)Converter.Deserialize(destType, stream);
                stream.Seek(0, SeekOrigin.Begin);
                var dest2 = (IEnumerable)BinaronConvert.Deserialize(stream);
                Assert.AreEqual(0, dest.Count());
                Assert.AreEqual(0, dest2.Count());
            }
            // Single
            {
                source           = AddItem <TSourceItem>(sourceType, sourceItem, source);
                using var stream = new MemoryStream();
                BinaronConvert.Serialize(source, stream);
                stream.Seek(0, SeekOrigin.Begin);
                var dest = (IEnumerable)Converter.Deserialize(destType, stream);
                stream.Seek(0, SeekOrigin.Begin);
                var dest2 = (IEnumerable)BinaronConvert.Deserialize(stream);
                Assert.AreEqual(!IsValid(sourceItem, destItem, destType) ? 0 : 1, dest.Count());
                Assert.AreEqual(1, dest2.Count());
            }
            // Multi
            {
                source           = AddItem <TSourceItem>(sourceType, sourceItem, source);
                source           = AddItem <TSourceItem>(sourceType, sourceItem, source);
                using var stream = new MemoryStream();
                BinaronConvert.Serialize(source, stream);
                stream.Seek(0, SeekOrigin.Begin);
                var dest = (IEnumerable)Converter.Deserialize(destType, stream);
                stream.Seek(0, SeekOrigin.Begin);
                var dest2 = (IEnumerable)BinaronConvert.Deserialize(stream);
                Assert.AreEqual(!IsValid(sourceItem, destItem, destType) ? 0 : 3, dest.Count());
                Assert.AreEqual(3, dest2.Count());
            }
        }
Exemple #13
0
        public void ObjectActivatorTest()
        {
            const int testVal = 100;

            var val = new TestClass <int?>();

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(val, stream, new SerializerOptions {
                SkipNullValues = true
            });
            stream.Seek(0, SeekOrigin.Begin);
            var dest = BinaronConvert.Deserialize <TestClass <int?> >(stream, new DeserializerOptions {
                ObjectActivator = new MyObjectActivator(testVal)
            });

            Assert.AreEqual(testVal, dest.Value);
        }
        public void RootReadOnlyDictionaryTests(Type destType)
        {
            IReadOnlyDictionary <string, string> source = new Dictionary <string, string> {
                { "a key", "a value" }
            };

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(source, stream);
            stream.Seek(0, SeekOrigin.Begin);
            var dest = (IEnumerable)Converter.Deserialize(destType, stream);

            stream.Seek(0, SeekOrigin.Begin);
            var dest2 = (IEnumerable)BinaronConvert.Deserialize(stream);

            Assert.AreEqual(1, dest.Count());
            Assert.AreEqual(1, dest2.Count());
        }
Exemple #15
0
        private static Task BinaronTest()
        {
            var serializerOptions = new SerializerOptions {
                SkipNullValues = true
            };

            using var stream = new MemoryStream();
            for (var i = 0; i < Loop; i++)
            {
                BinaronConvert.Serialize(Source, stream, serializerOptions);
                stream.Position = 0;
                var book = BinaronConvert.Deserialize <Book>(stream);
                stream.Position = 0;
                Trace.Assert(book.Title != null);
            }

            return(Task.CompletedTask);
        }
Exemple #16
0
        public void ServiceProviderObjectActivatorTest()
        {
            var services = new ServiceCollection();

            services.AddSingleton(new BaseConfig());

            var obj = new
            {
                Score   = 1.0d,
                Numbers = new[] { 3, 2, 1 },
                Dict    = new
                {
                    A = "a",
                    B = "b"
                },
                Ids       = new[] { "Id1", "Id2" },
                SubConfig = new
                {
                    Numbers = new[] { 3, 2, 1 },
                    Dict    = new
                    {
                        A = "a",
                        B = "b"
                    },
                    Ids   = new[] { "Id1", "Id2" },
                    Extra = "extra"
                }
            };

            var serviceProvider = services.BuildServiceProvider();

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(obj, stream, new SerializerOptions {
                SkipNullValues = true
            });
            stream.Position = 0;
            var result = BinaronConvert.Deserialize <TestConfig>(stream, new DeserializerOptions {
                ObjectActivator = new ObjectActivator(serviceProvider)
            });

            Assert.AreEqual(obj.SubConfig.Ids, result.SubConfig.Ids);
        }
Exemple #17
0
        public void TestListOfNullables2()
        {
            var collection = new List <int?>()
            {
                1, null, 2
            };

            using (var ms1 = new MemoryStream())
            {
                var so = new SerializerOptions();
                BinaronConvert.Serialize(collection, ms1, so);
                using (var ms2 = new MemoryStream(ms1.ToArray()))
                {
                    var cr2 = BinaronConvert.Deserialize <List <int?> >(ms2);
                    Assert.AreEqual(3, cr2.Count);
                    Assert.AreEqual(1, cr2[0]);
                    Assert.AreEqual(null, cr2[1]);
                    Assert.AreEqual(2, cr2[2]);
                }
            }
        }
        public async Task MultiLevelWithCustomFactoryTest()
        {
            var obj = CreatedNestedStructure();

            await using var stream = new MemoryStream();
            BinaronConvert.Serialize(obj, stream, new SerializerOptions {
                SkipNullValues = true, CustomObjectIdentifierProviders = { new NodeObjectIdentifierProvider() }
            });

            stream.Position = 0;
            var result = BinaronConvert.Deserialize <Nested>(stream, new DeserializerOptions {
                CustomObjectFactories = { new NodeObjectFactory() }
            });

            stream.Position = 0;
            dynamic dynamicResult = BinaronConvert.Deserialize(stream);

            Assert.IsNull(result.Annotation1);
            Assert.IsNull(result.Annotation2);

            var derived1 = (DerivedNode)result.Nodes["One"];
            var derived2 = (DerivedNode)result.Nodes["Two"];

            Assert.AreEqual("some attributes", derived1.UserAttributes["attribute1"].SingleOrDefault());
            CollectionAssert.AreEqual(new [] { "Child1", "Child2", "Child3" }, derived1.Values[0].SelectedChildren.Select(element => element.Name));
            CollectionAssert.AreEqual(new [] { "Child4", "Child5" }, derived1.Values[1].SelectedChildren.Select(element => element.Name));

            Assert.AreEqual(true, derived2.IsSelected);
            var value = derived2.Values.SingleOrDefault();

            Assert.NotNull(value);
            Assert.AreEqual(100, value.Count);
            var innerNestedNode = value.InnerNested.Nodes["InnerNode1"];

            Assert.AreEqual("InnerDerivedNode1", innerNestedNode.Name);
            Assert.AreEqual(true, ((DerivedNode)innerNestedNode).IsSelected);

            Assert.NotNull(dynamicResult.Nodes["One"]);
        }
Exemple #19
0
 public IGeoMap FromBinaryFile(string dataFile)
 {
     return(new GeoMapIndex(BinaronConvert.Deserialize <GeoMapData>(File.OpenRead(dataFile))));
 }
Exemple #20
0
        public static AssetsPak LoadPak(Stream stream)
        {
            var pak = BinaronConvert.Deserialize <AssetsPak>(stream);

            return(pak);
        }