public void DumpClassPrettyPrint() { var testClass = new TestClass { x = 5, y = 7, z = 0 }; testClass.list = new List <int> { 3, 1, 4 }; var settings = new NsonSettings() { PrettyPrint = true }; var json = Nson.ToNson(testClass, settings); Assert.AreEqual(@"Nez.Persistence.NsonTests.ClassTypeTests+TestClass( date: ""2020-01-01T00:00:00Z"", x: 5, y: 7, list: [ 3, 1, 4 ] )".Replace(" ", "\t"), json); }
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); }
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); }
public void DumpStruct() { var testStruct = new TestStruct { x = 5, y = 7, z = 0 }; var json = Nson.ToNson(testStruct); Assert.AreEqual("Nez.Persistence.NsonTests.StructTypeTests+TestStruct(x:5,y:7)", json); }
public void Converter_WriteJson() { var doodle = new Doodle { x = 5, y = 7, z = 9 }; var json = Nson.ToNson(doodle, new CustomDataConverter()); Assert.IsTrue(json.Contains("key-that-isnt-on-object")); Assert.IsTrue(json.Contains("another_key")); }
public void Converter_WantsExclusiveWrite() { var doodle = new Doodle { x = 5, y = 7, z = 9 }; var json = Nson.ToNson(doodle, new WantsExclusiveWriteConverter()); Assert.IsTrue(json.Contains("key-that-isnt-on-object")); Assert.IsTrue(json.Contains("another_key")); Assert.IsTrue(json.Contains("string_array")); Assert.IsFalse(json.Contains("x")); }
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)); }
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); }
public void DumpClass() { var testClass = new TestClass { x = 5, y = 7, z = 0 }; testClass.list = new List <int> { 3, 1, 4 }; var json = Nson.ToNson(testClass); Assert.AreEqual("Nez.Persistence.NsonTests.ClassTypeTests+TestClass(date:\"2020-01-01T00:00:00Z\",x:5,y:7,list:[3,1,4])", json); Assert.IsTrue(BeforeEncodeCallbackFired); }
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]); }