public void TestCustomActivator() { var config = new YamlConfig(); config.Register(new LegacyTypeConverterFactory()); config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly); config.LookupAssemblies.Add(typeof(YamlSerializerTest).Assembly); var serializer = new Serializer(config); var yaml = @"%YAML 1.2 --- !System.Drawing.SolidBrush Color: Red ... "; SolidBrush b = null; try { b = (SolidBrush)serializer.Deserialize(yaml)[0]; } catch (MissingMethodException) { // SolidBrush has no default constructor! } config.AddActivator <SolidBrush>(() => new SolidBrush(Color.Black)); serializer = new Serializer(config); // Now the serializer knows how to activate an instance of SolidBrush. b = (SolidBrush)serializer.Deserialize(yaml)[0]; Assert.AreEqual(b.Color, Color.Red); }
public void InitSerializer() { var config = new YamlConfig(); config.Register(new LegacyTypeConverterFactory()); config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly); config.LookupAssemblies.Add(typeof(YamlSerializerTest).Assembly); serializer = new Serializer(config); }
public void CustomActivator() { var brush = new YamlMapping("Color", "Blue"); brush.Tag = "!System.Drawing.SolidBrush"; var config = new YamlConfig(); config.Register(new LegacyTypeConverterFactory()); config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly); Assert.Throws <MissingMethodException>(() => constructor.NodeToObject(brush, new SerializerContext(config))); config.AddActivator <System.Drawing.SolidBrush>(() => new System.Drawing.SolidBrush(System.Drawing.Color.Black)); Assert.AreEqual(System.Drawing.Color.Blue, ((System.Drawing.SolidBrush)constructor.NodeToObject(brush, new SerializerContext(config))).Color); }
public void CultureTest() { var config = new YamlConfig(); config.Register(new LegacyTypeConverterFactory()); config.LookupAssemblies.Add(typeof(System.Drawing.PointF).Assembly); config.Culture = new System.Globalization.CultureInfo("da-DK"); var serializer = new Serializer(config); object obj = new System.Drawing.PointF(1.2f, 3.1f); var yaml = serializer.Serialize(obj); Assert.AreEqual( BuildResult( "!System.Drawing.PointF", "X: 1,2", "Y: 3,1" ), yaml ); var restore = serializer.Deserialize(yaml)[0]; Assert.AreEqual(obj, restore); obj = new System.Drawing.Point(1, 3); yaml = serializer.Serialize(obj); Assert.AreEqual( BuildResult( "!System.Drawing.Point 1; 3" ), yaml ); restore = serializer.Deserialize(yaml)[0]; Assert.AreEqual(obj, restore); YamlNode.DefaultConfig.Culture = System.Globalization.CultureInfo.CurrentCulture; }
public void CustomActivator() { var brush = new YamlMapping("Color", "Blue"); brush.Tag = "!System.Drawing.SolidBrush"; var config = new YamlConfig(); config.Register(new LegacyTypeConverterFactory()); config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly); Assert.Throws<MissingMethodException>(() => constructor.NodeToObject(brush, new SerializerContext(config))); config.AddActivator<System.Drawing.SolidBrush>(() => new System.Drawing.SolidBrush(System.Drawing.Color.Black)); Assert.AreEqual(System.Drawing.Color.Blue, ((System.Drawing.SolidBrush)constructor.NodeToObject(brush, new SerializerContext(config))).Color); }
public void TestCustomActivator() { var config = new YamlConfig(); config.Register(new LegacyTypeConverterFactory()); config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly); config.LookupAssemblies.Add(typeof(YamlSerializerTest).Assembly); var serializer = new Serializer(config); var yaml = @"%YAML 1.2 --- !System.Drawing.SolidBrush Color: Red ... "; SolidBrush b = null; try { b = (SolidBrush)serializer.Deserialize(yaml)[0]; } catch ( MissingMethodException ) { // SolidBrush has no default constructor! } config.AddActivator<SolidBrush>(() => new SolidBrush(Color.Black)); serializer = new Serializer(config); // Now the serializer knows how to activate an instance of SolidBrush. b = (SolidBrush)serializer.Deserialize(yaml)[0]; Assert.AreEqual(b.Color, Color.Red); }