Exemple #1
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));
 }
Exemple #2
0
 public void CustomInvalidNonConcreteTest <TSource>(TSource source, Type destType)
 {
     using var stream = new MemoryStream();
     BinaronConvert.Serialize(source, stream);
     stream.Seek(0, SeekOrigin.Begin);
     Assert.Throws <NotSupportedException>(() => Converter.Deserialize(destType, stream));
 }
Exemple #3
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 #5
0
        private void DumpToBinaryFile(GeoMapData data)
        {
            if (File.Exists(options.BinaryFile))
            {
                File.Delete(options.BinaryFile);
            }

            using var stream = File.Open(options.BinaryFile, FileMode.CreateNew);
            BinaronConvert.Serialize(data, stream);
        }
Exemple #6
0
 private static void BinaronTest_Serialize <T>(T input, int loop)
 {
     using var stream = new MemoryStream();
     for (var i = 0; i < loop; i++)
     {
         BinaronConvert.Serialize(input, stream, new SerializerOptions {
             SkipNullValues = true
         });
         stream.Position = 0;
     }
 }
Exemple #7
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);
        }
        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 #9
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 #10
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);
     }
 }
Exemple #11
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);
        }
        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 #14
0
        private static void BinaronTest_Deserialize <T>(T input, int loop)
        {
            var serializerOptions = new SerializerOptions {
                SkipNullValues = true
            };

            using var stream = new MemoryStream();
            BinaronConvert.Serialize(input, stream, serializerOptions);
            stream.Position = 0;

            for (var i = 0; i < loop; i++)
            {
                var _ = BinaronConvert.Deserialize <T>(stream);
                stream.Position = 0;
            }
        }
        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 #16
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 #17
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);
        }
Exemple #18
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 #19
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 #20
0
        public void PopulateObjectTest()
        {
            const int testVal = 100;

            var val = new TestClass <int?> {
                RootValue = DateTime.MinValue
            };

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

            BinaronConvert.Populate(dest, stream);
            Assert.AreEqual(testVal, dest.Value);
            Assert.AreEqual(DateTime.MinValue, dest.RootValue);
        }
Exemple #21
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]);
                }
            }
        }
Exemple #22
0
        public void CustomInvalidNonConcreteTest <TSource>(TSource source, Type destType)
        {
            using var stream = new MemoryStream();
            BinaronConvert.Serialize(source, stream);
            stream.Seek(0, SeekOrigin.Begin);
            Assert.Throws <NotSupportedException>(() =>
            {
                try
                {
                    Converter.Deserialize(destType, stream);
                }
                catch (TypeInitializationException ex)
                {
                    if (ex.InnerException is NotSupportedException)
                    {
                        throw ex.InnerException;
                    }

                    throw;
                }
            });
        }
        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 #24
0
 public IGeoMap FromBinaryFile(string dataFile)
 {
     return(new GeoMapIndex(BinaronConvert.Deserialize <GeoMapData>(File.OpenRead(dataFile))));
 }
Exemple #25
0
        public static void Build(string pak_name, string manifest_file_path)
        {
            AssetsPak pak = new AssetsPak();

            BonFile manifest = BonFileReader.Parse(manifest_file_path);

            var output_path = Path.GetDirectoryName(manifest_file_path);

            Console.WriteLine($"Building PAK from manifest file: {manifest_file_path}");

            Console.WriteLine("Parsed BON File:");
            Console.WriteLine(manifest.ToString());

            foreach (var section in manifest.Sections)
            {
                if (section.Key == "Root")
                {
                    continue;
                }

                AssetTypes asset_type;

                Console.WriteLine($"Adding Asset: {section.Key}");

                Console.WriteLine($"Type: {section.Value.ValueProps["type"].GetValue()}");

                try
                {
                    asset_type = (AssetTypes)Enum.Parse(typeof(AssetTypes), section.Value.ValueProps["type"].GetValue());
                }
                catch (Exception)
                {
                    throw new Exception("Unrecognized 'type' property or 'type' property is missing.");
                }

                if (!section.Value.ValueProps.ContainsKey("file_path"))
                {
                    throw new Exception("Missing 'file_path' property.");
                }

                var asset_file_path = section.Value.ValueProps["file_path"].GetValue();

                switch (asset_type)
                {
                case AssetTypes.Font:

                    var font_image_path =
                        Path.Combine(output_path, asset_file_path);

                    var font_image = ResourceLoader.LoadImage(font_image_path);

                    if (!section.Value.ValueProps.ContainsKey("glyph_size"))
                    {
                        throw new Exception("Missing 'glyph_size' property.");
                    }

                    var glyph_size = section.Value.ValueProps["glyph_size"].GetIntValue();

                    var font_data = new FontData()
                    {
                        GlyphSize   = glyph_size,
                        Id          = section.Key,
                        ImageData   = font_image.ImageData,
                        ImageWidth  = font_image.Width,
                        ImageHeight = font_image.Height
                    };

                    pak.AddFont(font_data);

                    break;

                case AssetTypes.SpriteSheet:

                    var sheet_image_path = Path.Combine(output_path, asset_file_path);
                    var sheet_image      = ResourceLoader.LoadImage(sheet_image_path);

                    if (!section.Value.ValueProps.ContainsKey("cell_size"))
                    {
                        throw new Exception("Missing 'cell_size' property.");
                    }

                    var cell_size = section.Value.ValueProps["cell_size"].GetIntValue();

                    Dictionary <string, int> sprite_map = new Dictionary <string, int>();

                    if (section.Value.ListProps.ContainsKey("sprites"))
                    {
                        var sprite_map_pairs = section.Value.ListProps["sprites"].Items;

                        foreach (var sprite_map_pair in sprite_map_pairs)
                        {
                            if (!sprite_map_pair.Contains(':'))
                            {
                                continue;
                            }

                            var values = sprite_map_pair.Split(':');

                            sprite_map.Add(values[0], int.Parse(values[1]));
                        }
                    }

                    var sheet_data = new SpriteSheetData()
                    {
                        Id          = section.Key,
                        CellSize    = cell_size,
                        ImageData   = sheet_image.ImageData,
                        ImageWidth  = sheet_image.Width,
                        ImageHeight = sheet_image.Height,
                        SpriteMap   = sprite_map
                    };

                    pak.AddSheet(sheet_data);

                    break;
                }
            }

            if (!pak_name.Contains(".pak"))
            {
                pak_name += ".pak";
            }

            var pak_file_path = Path.Combine(output_path, pak_name);

            using var pak_output = File.OpenWrite(pak_file_path);

            BinaronConvert.Serialize(pak, pak_output);
        }
Exemple #26
0
        public static AssetsPak LoadPak(Stream stream)
        {
            var pak = BinaronConvert.Deserialize <AssetsPak>(stream);

            return(pak);
        }