예제 #1
0
        public void Converter_ObjectFactoryListWithTypeHint()
        {
            var list = new List <Doodle>
            {
                { new Doodle {
                      x = 1, y = 2, z = 3
                  } },
                { new Doodle {
                      x = 4, y = 5, z = 5
                  } }
            };
            var json = Nson.ToNson(list);

            var settings = new NsonSettings {
                TypeConverters = new NsonTypeConverter[] { new ObjectFactoryConverter() }
            };
            var newList = Nson.FromNson <List <Doodle> >(json, settings);

            Assert.IsTrue(newList[0].wasCreatedByObjectFactory);
            Assert.IsTrue(newList[1].wasCreatedByObjectFactory);

            Assert.AreEqual(list[0].x, newList[0].x);
            Assert.AreEqual(list[0].y, newList[0].y);
            Assert.AreEqual(list[0].z, newList[0].z);

            Assert.AreEqual(list[1].x, newList[1].x);
            Assert.AreEqual(list[1].y, newList[1].y);
            Assert.AreEqual(list[1].z, newList[1].z);
        }
예제 #2
0
        public void Converter_ObjectFactoryNestedWithTypeHint()
        {
            var container = new DoodleContainer
            {
                firstDoodle = new Doodle {
                    x = 1, y = 2, z = 3
                },
                secondDoodle = new Doodle {
                    x = 4, y = 5, z = 5
                }
            };
            var json = Nson.ToNson(container);

            var settings = new NsonSettings {
                TypeConverters = new NsonTypeConverter[] { new ObjectFactoryConverter() }
            };
            var newContainer = Nson.FromNson <DoodleContainer>(json, settings);

            Assert.IsTrue(newContainer.firstDoodle.wasCreatedByObjectFactory);
            Assert.IsTrue(newContainer.secondDoodle.wasCreatedByObjectFactory);

            Assert.AreEqual(container.firstDoodle.x, newContainer.firstDoodle.x);
            Assert.AreEqual(container.firstDoodle.y, newContainer.firstDoodle.y);
            Assert.AreEqual(container.firstDoodle.z, newContainer.firstDoodle.z);

            Assert.AreEqual(container.secondDoodle.x, newContainer.secondDoodle.x);
            Assert.AreEqual(container.secondDoodle.y, newContainer.secondDoodle.y);
            Assert.AreEqual(container.secondDoodle.z, newContainer.secondDoodle.z);
        }
예제 #3
0
        public void LoadStruct()
        {
            var testStruct = Nson.FromNson <TestStruct>("Nez.Persistence.NsonTests.StructTypeTests+TestStruct(x:5,y:7)");

            Assert.AreEqual(5, testStruct.x);
            Assert.AreEqual(7, testStruct.y);
            Assert.AreEqual(0, testStruct.z);               // should not get assigned

            Assert.IsTrue(LoadCallbackFired);
        }
예제 #4
0
        public void DumpOuterClassWithNoTypeHintPropagatesToInnerClasses()
        {
            var outerClass = new OuterClass();

            outerClass.inner = new InnerClass();
            var nson = Nson.ToNson(outerClass);

            Assert.AreEqual("Nez.Persistence.NsonTests.ClassTypeTests+OuterClass(inner:Nez.Persistence.NsonTests.ClassTypeTests+InnerClass())", nson);

            var back = Nson.FromNson(nson);

            Assert.IsTrue(back.GetType() == typeof(OuterClass));
        }
예제 #5
0
        public void Converter_OnFoundCustomData()
        {
            var settings = new NsonSettings {
                TypeConverters = new NsonTypeConverter[] { new CustomDataConverter() }
            };
            var doodle = new Doodle {
                x = 5, y = 7, z = 9
            };
            var nson = Nson.ToNson(doodle, settings);

            var newDoodle = Nson.FromNson <Doodle>(nson, settings);

            Assert.AreEqual(2, newDoodle.totalOrphanedKeys);
            Assert.AreNotEqual(doodle.totalOrphanedKeys, newDoodle.totalOrphanedKeys);
        }
예제 #6
0
        public void LoadClass()
        {
            var testClass = Nson.FromNson <TestClass>("Nez.Persistence.NsonTests.ClassTypeTests+TestClass(date:\"2020-01-01T00:00:00Z\",x:5,y:7,list:[3,1,4])");

            Assert.AreEqual(new DateTime(2020, 1, 1), testClass.date);
            Assert.AreEqual(5, testClass.x);
            Assert.AreEqual(7, testClass.y);
            Assert.AreEqual(0, testClass.z);               // should not get assigned

            Assert.AreEqual(3, testClass.list.Count);
            Assert.AreEqual(3, testClass.list[0]);
            Assert.AreEqual(1, testClass.list[1]);
            Assert.AreEqual(4, testClass.list[2]);

            Assert.IsTrue(AfterDecodeCallbackFired);
        }
예제 #7
0
        public void Converter_ObjectFactoryWithTypeHint()
        {
            var doodle = new Doodle {
                x = 5, y = 7, z = 9
            };
            var json = Nson.ToNson(doodle);

            var settings = new NsonSettings {
                TypeConverters = new NsonTypeConverter[] { new ObjectFactoryConverter() }
            };
            var newDoodle = Nson.FromNson <Doodle>(json, settings);

            Assert.IsTrue(newDoodle.wasCreatedByObjectFactory);

            Assert.AreEqual(newDoodle.x, doodle.x);
            Assert.AreEqual(newDoodle.y, doodle.y);
            Assert.AreEqual(newDoodle.z, doodle.z);
        }
        public void ArrayTypeHint_Hints()
        {
            var entity = new Entity();

            entity.components = new List <Component> {
                new Component(), new Sprite {
                    entity = entity
                }
            };

            var json = Nson.ToNson(entity, new NsonSettings
            {
                PrettyPrint = true,
                PreserveReferencesHandling = true
            });

            var outEntity = Nson.FromNson <Entity>(json);

            Assert.IsInstanceOf(typeof(Sprite), outEntity.components[1]);
        }
        public void PreserveReferences_Preserves()
        {
            var entity = new Entity();

            entity.components = new List <Component> {
                new Component(), new Sprite {
                    entity = entity
                }
            };

            var json = Nson.ToNson(entity, new NsonSettings
            {
                PrettyPrint = true,
                PreserveReferencesHandling = true
            });

            var outEntity = Nson.FromNson <Entity>(json);

            Assert.AreEqual(outEntity, outEntity.components[1].entity);
        }
        public void TypeHintAuto()
        {
            var entity = new Entity
            {
                components = new List <Component> {
                    new Component(), new Sprite()
                }
            };
            var json = Nson.ToNson(entity, new NsonSettings
            {
                PrettyPrint = true
            });

            var outEntity = Nson.FromNson <Entity>(json);

            Assert.IsInstanceOf(typeof(Sprite), outEntity.components[1]);


            outEntity = Nson.FromNson <Entity>(json);
            Assert.IsInstanceOf(typeof(Sprite), outEntity.components[1]);
        }