diff --git a/lib/yaml/YamlSerializer.XML b/lib/yaml/YamlSerializer.XML
index 5023c52ab..06d1a45d7 100644
--- a/lib/yaml/YamlSerializer.XML
+++ b/lib/yaml/YamlSerializer.XML
@@ -4,6 +4,2014 @@
+ Type type;
+ AttributeType attr = type.GetAttribute<AttributeType>();
+
+ PropertyInfo propInfo;
+ AttributeType attr = propInfo.GetAttribute<AttributeType>();
+
+ string name;
+ Type type = TypeUtils.GetType(name); // search from all assembly loaded
+
+
+
+
+ var serializer = new YamlSerializer();
+ object obj = GetObjectToSerialize();
+ string yaml = serializer.Serialize(obj);
+ object restored = serializer.Deserialize(yaml);
+ Assert.AreEqual(obj, restored);
+
+ What kind of objects can be serialized?
+
+
+ object obj = new object[]{
+ null,
+ "abc",
+ true,
+ 1,
+ (Byte)1,
+ 1.0,
+ "1",
+ new double[]{ 1.1, 2, -3 },
+ new string[]{ "def", "ghi", "1" },
+ new System.Drawing.Point(1,3),
+ new System.Drawing.SolidBrush(Color.Blue)
+ };
+
+ var serializer = new YamlSerializer();
+ string yaml = serializer.Serialize(obj);
+ // %YAML 1.2
+ // ---
+ // - null
+ // - abc
+ // - True
+ // - 1
+ // - !System.Byte 1
+ // - !!float 1
+ // - "1"
+ // - !<!System.Double[]%gt; [1.1, 2, -3]
+ // - !<!System.String[]%gt;
+ // - def
+ // - ghi
+ // - !System.Drawing.Point 1, 3
+ // - !System.Drawing.SolidBrush
+ // Color: Blue
+ // ...
+
+ object restored;
+ try {
+ restored = YamlSerializer.Deserialize(yaml)[0];
+ } catch(MissingMethodException) {
+ // default constructor is missing for SolidBrush
+ }
+
+ // Let the library know how to activate an instance of SolidBrush.
+ YamlNode.DefaultConfig.AddActivator<System.Drawing.SolidBrush>(
+ () => new System.Drawing.SolidBrush(Color.Black /* dummy */));
+
+ // Then, all the objects can be restored correctly.
+ restored = serializer.Deserialize(yaml)[0];
+
+ var yaml = "";
+ var serializer = new YamlSerializer();
+ yaml += serializer.Serialize("a");
+ yaml += serializer.Serialize(1);
+ yaml += serializer.Serialize(1.1);
+ // %YAML 1.2
+ // ---
+ // a
+ // ...
+ // %YAML 1.2
+ // ---
+ // 1
+ // ...
+ // %YAML 1.2
+ // ---
+ // 1.1
+ // ...
+
+ object[] objects = serializer.Deserialize(yaml);
+ // objects[0] == "a"
+ // objects[1] == 1
+ // objects[2] == 1.1
+
+
+ Serializing structures and classes
+
+ Serialization methods
+
+
+ public class Test1
+ {
+ public int PublicProp { get; set; } // processed (by assign)
+ protected int ProtectedProp { get; set; } // Ignored
+ private int PrivateProp { get; set; } // Ignored
+ internal int InternalProp { get; set; } // Ignored
+
+ public int PublicField; // processed (by assign)
+ protected int ProtectedField; // Ignored
+ private int PrivateField; // Ignored
+ internal int InternalField; // Ignored
+
+ public List<string> ClassPropByAssign // processed (by assign)
+ { get; set; }
+
+ public int ReadOnlyValueProp { get; private set; } // Ignored
+ public List<string> ReadOnlyClassProp // processed (by content)
+ { get; private set; }
+
+ [YamlSerialize(YamlSerializeMethod.Content)]
+ public List<string> ClassPropByContent// processed (by content)
+ { get; set; }
+
+ public int[] IntArrayField = // processed (by assign)
+ new int[10];
+
+ [YamlSerialize(YamlSerializeMethod.Binary)]
+ public int[] IntArrayFieldBinary = // processed (as binary)
+ new int[100];
+
+ [YamlSerialize(YamlSerializeMethod.Never)]
+ public int PublicPropHidden; // Ignored
+
+ public Test1()
+ {
+ ClassPropByAssign = new List<string>();
+ ReadOnlyClassProp = new List<string>();
+ ClassPropByContent = new List<string>();
+ }
+ }
+
+ public void TestPropertiesAndFields1()
+ {
+ var test1 = new Test1();
+ test1.ClassPropByAssign.Add("abc");
+ test1.ReadOnlyClassProp.Add("def");
+ test1.ClassPropByContent.Add("ghi");
+ var rand = new Random(0);
+ for ( int i = 0; i < test1.IntArrayFieldBinary.Length; i++ )
+ test1.IntArrayFieldBinary[i] = rand.Next();
+
+ var serializer = new YamlSerializer();
+ string yaml = serializer.Serialize(test1);
+ // %YAML 1.2
+ // ---
+ // !YamlSerializerTest.Test1
+ // PublicProp: 0
+ // ClassPropByAssign:
+ // Capacity: 4
+ // ICollection.Items:
+ // - abc
+ // ReadOnlyClassProp:
+ // Capacity: 4
+ // ICollection.Items:
+ // - def
+ // ClassPropByContent:
+ // Capacity: 4
+ // ICollection.Items:
+ // - ghi
+ // PublicField: 0
+ // IntArrayField: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+ // IntArrayFieldBinary: |+2
+ // Gor1XAwenmhGkU5ib9NxR11LXxp1iYlH5LH4c9hImTitWSB9Z78II2UvXSXV99A79fj6UBn3GDzbIbd9
+ // yBDjAyslYm58iGd/NN+tVjuLRCg3cJBo+PWMbIWm9n4AEC0E7LKXWV5HXUNk7I13APEDWFMM/kWTz2EK
+ // s7LzFw2gBjpKugkmQJqIfinpQ1J1yqhhz/XjA3TBxDBsEuwrD+SNevQSqEC+/KRbwgE6D011ACMeyRt0
+ // BOG6ZesRKCtL0YU6tSnLEpgKVBz+R300qD3/W0aZVk+1vHU+auzyGCGUaHCGd6dpRoEhXoIg2m3+AwJX
+ // EJ37T+TA9BuEPJtyGoq+crQMFQtXj1Zriz3HFbReclLvDdVpZlcOHPga/3+3Y509EHZ7UyT7H1xGeJxn
+ // eXPrDDb0Ul04MfZb4UYREOfR3HNzNTUYGRsIPUvHOEW7AaoplIfkVQp19DvGBrBqlP2TZ9atlWUHVdth
+ // 7lIBeIh0wiXxoOpCbQ7qVP9GkioQUrMkOcAJaad3exyZaOsXxznFCA==
+ // ...
+ }
+
+
+ Default values of fields and properties
+
+
+ using System.ComponentModel;
+
+ public class Test2
+ {
+ [DefaultValue(0)]
+ public int Default0 = 0;
+
+ [DefaultValue("a")]
+ public string Defaulta = "a";
+
+ public int DynamicDefault = 0;
+
+ bool ShouldSerializeDynamicDefault()
+ {
+ return Default0 != DynamicDefault;
+ }
+ }
+
+ public void TestDefaultValue()
+ {
+ var test2 = new Test2();
+ var serializer = new YamlSerializer();
+
+ // All properties have defalut values.
+ var yaml = serializer.Serialize(test2);
+ // %YAML 1.2
+ // ---
+ // !YamlSerializerTest.Test2 {}
+ // ...
+
+ test2.Defaulta = "b";
+ yaml = serializer.Serialize(test2);
+ // %YAML 1.2
+ // ---
+ // !YamlSerializerTest.Test2
+ // Defaulta: b
+ // ...
+
+ test2.Defaulta = "a";
+ var yaml = serializer.Serialize(test2);
+ // %YAML 1.2
+ // ---
+ // !YamlSerializerTest.Test2 {}
+ // ...
+
+ test2.DynamicDefault = 1;
+ yaml = serializer.Serialize(test2);
+ // %YAML 1.2
+ // ---
+ // !YamlSerializerTest.Test2
+ // DynamicDefault: 1
+ // ...
+
+ test2.Default0 = 1;
+ yaml = serializer.Serialize(test2);
+ // %YAML 1.2
+ // ---
+ // !YamlSerializerTest.Test2
+ // Default0: 1
+ // ...
+ }
+
+
+ Collection classes
+
+ Multitime appearance of a same object
+
+
+ public class TestClass
+ {
+ public List<TestClass> list =
+ new List<TestClass>();
+ }
+
+ public class ChildClass: TestClass
+ {
+ }
+
+ void RecursiveObjectsTest()
+ {
+ var a = new TestClass();
+ var b = new ChildClass();
+ a.list.Add(a);
+ a.list.Add(a);
+ a.list.Add(b);
+ a.list.Add(a);
+ a.list.Add(b);
+ b.list.Add(a);
+ var serializer = new YamlSerializer();
+ string yaml = serializer.Serialize(a);
+ // %YAML 1.2
+ // ---
+ // &A !TestClass
+ // list:
+ // Capacity: 8
+ // ICollection.Items:
+ // - *A
+ // - *A
+ // - &B !ChildClass
+ // list:
+ // Capacity: 4
+ // ICollection.Items:
+ // - *A
+ // - *A
+ // - *B
+ // ...
+
+ var restored = (TestClass)serializer.Deserialize(yaml)[0];
+ Assert.IsTrue(restored == restored.list[0]);
+ Assert.IsTrue(restored == restored.list[1]);
+ Assert.IsTrue(restored == restored.list[3]);
+ Assert.IsTrue(restored == restored.list[5]);
+ Assert.IsTrue(restored.list[2] == restored.list[4]);
+ }
+
+
+
+ // 1000 chars
+ string long_str =
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
+ "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
+ string short_str = "12345";
+ object obj = new object[] { long_str, long_str, short_str, short_str };
+ var serializer = new YamlSerializer();
+ string yaml = serializer.Serialize(obj);
+ // %YAML 1.2
+ // ---
+ // - &A 01234567890123456789012345678901234567890123456789 ... (snip) ... 789
+ // - *A
+ // - "12345"
+ // - "12345"
+ // ...
+
+
+ YAML text written / read by
+
+
+ [Flags]
+ enum TestEnum: uint
+ {
+ abc = 1,
+ あいう = 2
+ }
+
+ public void TestVariousFormats()
+ {
+ var dict = new Dictionary<object, object>();
+ dict.Add(new object[] { 1, "a" }, new object());
+ object obj = new object[]{
+ dict,
+ null,
+ "abc",
+ "1",
+ "a ",
+ "- a",
+ "abc\n",
+ "abc\ndef\n",
+ "abc\ndef\nghi",
+ new double[]{ 1.1, 2, -3, 3.12, 13.2 },
+ new int[,] { { 1, 3}, {4, 5}, {10, 1} },
+ new string[]{ "jkl", "mno\npqr" },
+ new System.Drawing.Point(1,3),
+ TestEnum.abc,
+ TestEnum.abc | TestEnum.あいう,
+ };
+ var config = new YamlConfig();
+ config.ExplicitlyPreserveLineBreaks = false;
+ var serializer = new YamlSerializer(config);
+ string yaml = serializer.Serialize(obj);
+
+ // %YAML 1.2
+ // ---
+ // - !<!System.Collections.Generic.Dictionary%602[[System.Object,...],[System.Object,...]]>
+ // Keys: {}
+ // Values: {}
+ // IDictionary.Entries:
+ // ? - 1
+ // - a
+ // : !System.Object {}
+ // - null
+ // - abc
+ // - "1"
+ // - "a "
+ // - "- a"
+ // - "abc\n"
+ // - |+2
+ // abc
+ // def
+ // - |-2
+ // abc
+ // def
+ // ghi
+ // - !<!System.Double[]> [1.1, 2, -3, 3.12, 13.2]
+ // - !<!System.Int32[,]> [[1, 3], [4, 5], [10, 1]]
+ // - !<!System.String[]>
+ // - jkl
+ // - |-2
+ // mno
+ // pqr
+ // - !System.Drawing.Point 1, 3
+ // - !TestEnum abc
+ // - !TestEnum abc, あいう
+ // ...
+ }
+
+
+
+ var serializer = new YamlSerializer();
+ var yaml = serializer.Serialize("abc");
+ // %YAML 1.2\r\n // line breaks are explicitly shown in this example
+ // ---\r\n
+ // abc\r\n
+ // ...\r\n
+
+ var config = new YamlConfig();
+ config.LineBreakForOutput = "\n";
+ serializer = new YamlSerializer(config);
+ var yaml = serializer.Serialize("abc");
+ // %YAML 1.2\n
+ // ---\n
+ // abc\n
+ // ...\n
+
+ YamlNode.DefaultConfig.LineBreakForOutput = "\n";
+
+ var serializer = new YamlSerializer();
+ serializer = new YamlSerializer();
+ var yaml = serializer.Serialize("abc");
+ // %YAML 1.2\n
+ // ---\n
+ // abc\n
+ // ...\n
+
+
+ Line breaks in YAML text
+
+
+ var serializer = new YamlSerializer();
+ var text = "abc\r\n def\r\nghi\r\n";
+ // abc
+ // def
+ // ghi
+
+ // By default, line breaks explicitly appears in escaped form.
+ var yaml = serializer.Serialize(text);
+ // %YAML 1.2
+ // ---
+ // "abc\r\n\
+ // \ def\r\n\
+ // ghi\r\n"
+ // ...
+
+ // Original line breaks are preserved
+ var restored = (string)serializer.Deserialize(yaml)[0];
+ // "abc\r\n def\r\nghi\r\n"
+
+
+ YamlNode.DefaultConfig.ExplicitlyPreserveLineBreaks = false;
+
+ // Literal style is easier to be read.
+ var yaml = serializer.Serialize(text);
+ // %YAML 1.2
+ // ---
+ // |+2
+ // abc
+ // def
+ // ghi
+ // ...
+
+ // Original line breaks are lost.
+ var restored = (string)serializer.Deserialize(yaml)[0];
+ // "abc\n def\nghi\n"
+
+
+
+ var serializer = new YamlSerializer();
+
+ // text with mixed line breaks
+ var text = "abc\r def\nghi\r\n";
+ // abc\r // line breaks are explicitly shown in this example
+ // def\n
+ // ghi\r\n
+
+ YamlNode.DefaultConfig.ExplicitlyPreserveLineBreaks = false;
+
+ // By default, all line breaks are normalized to "\r\n" when serialized.
+ var yaml = serializer.Serialize(text);
+ // %YAML 1.2\r\n
+ // ---\r\n
+ // |+2\r\n
+ // abc\r\n
+ // def\r\n
+ // ghi\r\n
+ // ...\r\n
+
+ // When deserialized, line breaks are normalized into "\n".
+ var restored = (string)serializer.Deserialize(yaml)[0];
+ // "abc\n def\nghi\n"
+
+ // Line breaks are normalized into "\r\n" instead of "\n".
+ YamlNode.DefaultConfig.LineBreakForInput = "\r\n";
+ restored = (string)serializer.Deserialize(yaml)[0];
+ // "abc\r\n def\r\nghi\r\n"
+
+ // Line breaks are written as is,
+ YamlNode.DefaultConfig.NormalizeLineBreaks = false;
+ var yaml = serializer.Serialize(text);
+ // %YAML 1.2\r\n
+ // ---\r\n
+ // |+2\r\n
+ // abc\r
+ // def\n
+ // ghi\r\n
+ // ...\r\n
+
+ // and are read as is.
+ restored = (string)serializer.Deserialize(yaml)[0];
+ // "abc\r def\nghi\r\n"
+
+ // Note that when the line breaks of YAML stream is changed
+ // between serialization and deserialization, the original
+ // line breaks are lost.
+ yaml = yaml.Replace("\r\n", "\n").Replace("\r", "\n");
+ restored = (string)serializer.Deserialize(yaml)[0];
+ // "abc\n def\nghi\n"
+
+
+
+ object obj;
+ YamlNode node = YamlRepresenter.ObjectToNode(obj);
+
+
+ var node_tree = seq(
+ str("abc"),
+ str("def"),
+ map(
+ str("key"), str("value"),
+ str("key2"), seq( str("value2a"), str("value2b") )
+ ),
+ str("2"), // !!str
+ str("!!int", "2")
+ );
+
+ string yaml = node_tree.ToYaml();
+
+ // %YAML 1.2
+ // ---
+ // - abc
+ // - def
+ // - key: value
+ // key2: [ value2a, value2b ]
+ // - "2" # !!str
+ // - 2 # !!int
+ // ...
+
+
+ var node_tree = seq(
+ str("abc"),
+ str("def"),
+ map(
+ str("key"), str("value"),
+ str("key2"), seq( str("value2a"), str("value2b") )
+ ),
+ str("2"), // !!str
+ str("!!int", "2")
+ );
+
+ string yaml = node_tree.ToYaml();
+
+ // %YAML 1.2
+ // ---
+ // - abc
+ // - def
+ // - key: value
+ // key2: [ value2a, value2b ]
+ // - "2" # !!str
+ // - 2 # !!int
+ // ...
+
+
+ return RewindUnless(()=>
+ text[p++] == 't' &&
+ text[p++] == 'e' &&
+ text[p++] == 'x' &&
+ text[p++] == 't'
+ );
+
+
+ return
+ SomeObsoleteReductionRule() &&
+ WarningIf(
+ context != Context.IndeedObsolete,
+ "Obsolete");
+
+
+ return
+ SomeObsoleteReductionRule() &&
+ WarningUnless(
+ context != Context.NotObsolete,
+ "Obsolete");
+
+
+ return
+ SomeObsoleteReductionRule() &&
+ Warning("Obsolete");
+
+ true
.
+ bool Name()
+ {
+ return RewindUnless(()=>
+ FirstName() &&
+ Optional(MiddleName) &&
+ LastName()
+ );
+ }
+
+
+ bool LinesOrEmpty()
+ {
+ return
+ Repeat(Line);
+ }
+
+
+
+ bool LinesOrEmpty()
+ {
+ return
+ Repeat(()=>
+ RewindUnless(()=>
+ Text() &&
+ LineBreak()
+ )
+ );
+ }
+
+
+ bool Lines()
+ {
+ return
+ Repeat(Line);
+ }
+
+
+ bool Lines()
+ {
+ return
+ Repeat(()=>
+ RewindUnless(()=>
+ Text() &&
+ LineBreak()
+ )
+ );
+ }
+
+ n
times repeatition.
+
+ bool FourLines()
+ {
+ return
+ Repeat(4, ()=>
+ RewindUnless(()=>
+ Text() &&
+ LineBreak()
+ )
+ );
+ }
+
+
+ bool Google()
+ {
+ return
+ RewindUnless(()=>
+ text[p++] == 'g' &&
+ Repeat(2, 100,
+ RewindUnless(()=>
+ text[p++] == 'o'
+ )
+ )
+ text[p++] == 'g' &&
+ text[p++] == 'l' &&
+ text[p++] == 'e'
+ );
+ }
+
+
+ bool File()
+ {
+ return
+ Optional(Header()) &&
+ Body() &&
+ Optional(Footer());
+ }
+
+
+ bool File()
+ {
+ return
+ Optional(Header) &&
+ Body() &&
+ Optional(Footer);
+ }
+
+
+ num ::= [0-9]
+ alpha-num :: alpha | num
+ word ::= alpha ( alpha-num )*
+
+ Func<char,bool> Alpha = Charset( c =>
+ ( 'A' <= c && c <= 'Z' ) ||
+ ( 'a' <= c && c <= 'z' )
+ );
+ Func<char,bool> Num = Charset( c =>
+ '0' <= c && c <= '9'
+ );
+ Func<char,bool> AlphaNum = Charset( c =>
+ Alpha(c) || Num(c)
+ );
+ bool Word()
+ {
+ return
+ Accept(Alpha) &&
+ Repeat(AlphaNum);
+ // No need for RewindUnless
+ }
+
+
+ bool YMCA()
+ {
+ return
+ RewindUnless(()=>
+ Accept('Y') &&
+ Accept('M') &&
+ Accept('C') &&
+ Accept('A')
+ );
+ }
+
+ -or-
+
+ bool YMCA()
+ {
+ return
+ RewindUnless(()=>
+ text[p++] == 'Y' &&
+ text[p++] == 'M' &&
+ text[p++] == 'C' &&
+ text[p++] == 'A'
+ );
+ }
+
+
+ bool YMCA()
+ {
+ return
+ Accept("YMCA");
+ }
+
+ n
times repetition of characters.
+ min
times, at most max
times
+ repetition of characters.
+
+ static class YamlCharsets: Charsets
+ {
+ Func<char, bool> cPrintable;
+ Func<char, bool> sWhite;
+
+ static YamlCharsets()
+ {
+ cPrintable = CacheResult(c =>
+ /* ( 0x10000 < c && c < 0x110000 ) || */
+ ( 0xe000 <= c && c <= 0xfffd ) ||
+ ( 0xa0 <= c && c <= 0xd7ff ) ||
+ ( c < 0x100 && ( // to improve performance
+ c == 0x85 ||
+ ( 0x20 <= c && c <= 0x7e ) ||
+ c == 0x0d ||
+ c == 0x0a ||
+ c == 0x09
+ ) )
+ );
+ sWhite = CacheResult(c =>
+ c < 0x100 && ( // to improve performance
+ c == '\t' ||
+ c == ' '
+ )
+ );
+ }
+ }
+
+ static class YamlCharsets: Charsets
+ {
+ Func<char, bool> cPrintable;
+ Func<char, bool> sWhite;
+
+ static YamlCharsets()
+ {
+ cPrintable = CacheResult(c =>
+ /* ( 0x10000 < c && c < 0x110000 ) || */
+ ( 0xe000 <= c && c <= 0xfffd ) ||
+ ( 0xa0 <= c && c <= 0xd7ff ) ||
+ ( c < 0x100 && ( // to improve performance
+ c == 0x85 ||
+ ( 0x20 <= c && c <= 0x7e ) ||
+ c == 0x0d ||
+ c == 0x0a ||
+ c == 0x09
+ ) )
+ );
+ sWhite = CacheResult(c =>
+ c < 0x100 && ( // to improve performance
+ c == '\t' ||
+ c == ' '
+ )
+ );
+ }
+ }
+
+ bool SomeRule()
+ {
+ return
+ Save(()=> SubRule(), s => MessageBox.Show(s));
+ }
+
+ bool SomeRule()
+ {
+ return
+ SubRule() &&
+ Action(()=> do_some_action());
+ }
+
+ I don't remember why this class was needed....
+
+ object obj = GetObjectToConvert();
+
+ // Check if the type has [TypeConverter] attribute.
+ if( EasyTypeConverter.IsTypeConverterSpecified(type) ) {
+
+ // Convert the object to string.
+ string s = EasyTypeConverter.ConvertToString(obj);
+
+ // Convert the string to an object of the spific type.
+ object restored = EasyTypeConverter.ConvertFromString(s, type);
+
+ Assert.AreEqual(obj, restored);
+
+ }
+
+
+ YAML Ain’t Markup Language (YAML™) Version 1.2
+ 3rd Edition (2009-07-21)
+ http://yaml.org/spec/1.2/spec.html
+ string yaml = LoadYamlSource();
+ YamlParser parser = new YamlParser();
+ Node[] result = null;
+ try {
+ result = parser.Parse(yaml);
+ ...
+ // you can reuse parser as many times you want
+ ...
+
+ } catch( ParseErrorException e ) {
+ MessageBox.Show(e.Message);
+ }
+ if(result != null) {
+ ...
+
+ }
+
+
- var node_tree = seq(
- str("abc"),
- str("def"),
- map(
- str("key"), str("value"),
- str("key2"), seq( str("value2a"), str("value2b") )
- ),
- str("2"), // !!str
- str("!!int", "2")
- );
-
- string yaml = node_tree.ToYaml();
-
- // %YAML 1.2
- // ---
- // - abc
- // - def
- // - key: value
- // key2: [ value2a, value2b ]
- // - "2" # !!str
- // - 2 # !!int
- // ...
-
-
- var node_tree = seq(
- str("abc"),
- str("def"),
- map(
- str("key"), str("value"),
- str("key2"), seq( str("value2a"), str("value2b") )
- ),
- str("2"), // !!str
- str("!!int", "2")
- );
-
- string yaml = node_tree.ToYaml();
-
- // %YAML 1.2
- // ---
- // - abc
- // - def
- // - key: value
- // key2: [ value2a, value2b ]
- // - "2" # !!str
- // - 2 # !!int
- // ...
-
-
- return RewindUnless(()=>
- text[p++] == 't' &&
- text[p++] == 'e' &&
- text[p++] == 'x' &&
- text[p++] == 't'
- );
-
-
- return
- SomeObsoleteReductionRule() &&
- WarningIf(
- context != Context.IndeedObsolete,
- "Obsolete");
-
-
- return
- SomeObsoleteReductionRule() &&
- WarningUnless(
- context != Context.NotObsolete,
- "Obsolete");
-
-
- return
- SomeObsoleteReductionRule() &&
- Warning("Obsolete");
-
- true
.
- bool Name()
- {
- return RewindUnless(()=>
- FirstName() &&
- Optional(MiddleName) &&
- LastName()
- );
- }
-
-
- bool LinesOrEmpty()
- {
- return
- Repeat(Line);
- }
-
-
-
- bool LinesOrEmpty()
- {
- return
- Repeat(()=>
- RewindUnless(()=>
- Text() &&
- LineBreak()
- )
- );
- }
-
-
- bool Lines()
- {
- return
- Repeat(Line);
- }
-
-
- bool Lines()
- {
- return
- Repeat(()=>
- RewindUnless(()=>
- Text() &&
- LineBreak()
- )
- );
- }
-
- n
times repeatition.
-
- bool FourLines()
- {
- return
- Repeat(4, ()=>
- RewindUnless(()=>
- Text() &&
- LineBreak()
- )
- );
- }
-
-
- bool Google()
- {
- return
- RewindUnless(()=>
- text[p++] == 'g' &&
- Repeat(2, 100,
- RewindUnless(()=>
- text[p++] == 'o'
- )
- )
- text[p++] == 'g' &&
- text[p++] == 'l' &&
- text[p++] == 'e'
- );
- }
-
-
- bool File()
- {
- return
- Optional(Header()) &&
- Body() &&
- Optional(Footer());
- }
-
-
- bool File()
- {
- return
- Optional(Header) &&
- Body() &&
- Optional(Footer);
- }
-
-
- num ::= [0-9]
- alpha-num :: alpha | num
- word ::= alpha ( alpha-num )*
-
- Func<char,bool> Alpha = Charset( c =>
- ( 'A' <= c && c <= 'Z' ) ||
- ( 'a' <= c && c <= 'z' )
- );
- Func<char,bool> Num = Charset( c =>
- '0' <= c && c <= '9'
- );
- Func<char,bool> AlphaNum = Charset( c =>
- Alpha(c) || Num(c)
- );
- bool Word()
- {
- return
- Accept(Alpha) &&
- Repeat(AlphaNum);
- // No need for RewindUnless
- }
-
-
- bool YMCA()
- {
- return
- RewindUnless(()=>
- Accept('Y') &&
- Accept('M') &&
- Accept('C') &&
- Accept('A')
- );
- }
-
- -or-
-
- bool YMCA()
- {
- return
- RewindUnless(()=>
- text[p++] == 'Y' &&
- text[p++] == 'M' &&
- text[p++] == 'C' &&
- text[p++] == 'A'
- );
- }
-
-
- bool YMCA()
- {
- return
- Accept("YMCA");
- }
-
- n
times repetition of characters.
- min
times, at most max
times
- repetition of characters.
-
- static class YamlCharsets: Charsets
- {
- Func<char, bool> cPrintable;
- Func<char, bool> sWhite;
-
- static YamlCharsets()
- {
- cPrintable = CacheResult(c =>
- /* ( 0x10000 < c && c < 0x110000 ) || */
- ( 0xe000 <= c && c <= 0xfffd ) ||
- ( 0xa0 <= c && c <= 0xd7ff ) ||
- ( c < 0x100 && ( // to improve performance
- c == 0x85 ||
- ( 0x20 <= c && c <= 0x7e ) ||
- c == 0x0d ||
- c == 0x0a ||
- c == 0x09
- ) )
- );
- sWhite = CacheResult(c =>
- c < 0x100 && ( // to improve performance
- c == '\t' ||
- c == ' '
- )
- );
- }
- }
-
- static class YamlCharsets: Charsets
- {
- Func<char, bool> cPrintable;
- Func<char, bool> sWhite;
-
- static YamlCharsets()
- {
- cPrintable = CacheResult(c =>
- /* ( 0x10000 < c && c < 0x110000 ) || */
- ( 0xe000 <= c && c <= 0xfffd ) ||
- ( 0xa0 <= c && c <= 0xd7ff ) ||
- ( c < 0x100 && ( // to improve performance
- c == 0x85 ||
- ( 0x20 <= c && c <= 0x7e ) ||
- c == 0x0d ||
- c == 0x0a ||
- c == 0x09
- ) )
- );
- sWhite = CacheResult(c =>
- c < 0x100 && ( // to improve performance
- c == '\t' ||
- c == ' '
- )
- );
- }
- }
-
- bool SomeRule()
- {
- return
- Save(()=> SubRule(), s => MessageBox.Show(s));
- }
-
- bool SomeRule()
- {
- return
- SubRule() &&
- Action(()=> do_some_action());
- }
-
- var serializer = new YamlSerializer();
- object obj = GetObjectToSerialize();
- string yaml = serializer.Serialize(obj);
- object restored = serializer.Deserialize(yaml);
- Assert.AreEqual(obj, restored);
-
- What kind of objects can be serialized?
-
-
- object obj = new object[]{
- null,
- "abc",
- true,
- 1,
- (Byte)1,
- 1.0,
- "1",
- new double[]{ 1.1, 2, -3 },
- new string[]{ "def", "ghi", "1" },
- new System.Drawing.Point(1,3),
- new System.Drawing.SolidBrush(Color.Blue)
- };
-
- var serializer = new YamlSerializer();
- string yaml = serializer.Serialize(obj);
- // %YAML 1.2
- // ---
- // - null
- // - abc
- // - True
- // - 1
- // - !System.Byte 1
- // - !!float 1
- // - "1"
- // - !<!System.Double[]%gt; [1.1, 2, -3]
- // - !<!System.String[]%gt;
- // - def
- // - ghi
- // - !System.Drawing.Point 1, 3
- // - !System.Drawing.SolidBrush
- // Color: Blue
- // ...
-
- object restored;
- try {
- restored = YamlSerializer.Deserialize(yaml)[0];
- } catch(MissingMethodException) {
- // default constructor is missing for SolidBrush
- }
-
- // Let the library know how to activate an instance of SolidBrush.
- YamlNode.DefaultConfig.AddActivator<System.Drawing.SolidBrush>(
- () => new System.Drawing.SolidBrush(Color.Black /* dummy */));
-
- // Then, all the objects can be restored correctly.
- restored = serializer.Deserialize(yaml)[0];
-
- var yaml = "";
- var serializer = new YamlSerializer();
- yaml += serializer.Serialize("a");
- yaml += serializer.Serialize(1);
- yaml += serializer.Serialize(1.1);
- // %YAML 1.2
- // ---
- // a
- // ...
- // %YAML 1.2
- // ---
- // 1
- // ...
- // %YAML 1.2
- // ---
- // 1.1
- // ...
-
- object[] objects = serializer.Deserialize(yaml);
- // objects[0] == "a"
- // objects[1] == 1
- // objects[2] == 1.1
-
-
- Serializing structures and classes
-
- Serialization methods
-
-
- public class Test1
- {
- public int PublicProp { get; set; } // processed (by assign)
- protected int ProtectedProp { get; set; } // Ignored
- private int PrivateProp { get; set; } // Ignored
- internal int InternalProp { get; set; } // Ignored
-
- public int PublicField; // processed (by assign)
- protected int ProtectedField; // Ignored
- private int PrivateField; // Ignored
- internal int InternalField; // Ignored
-
- public List<string> ClassPropByAssign // processed (by assign)
- { get; set; }
-
- public int ReadOnlyValueProp { get; private set; } // Ignored
- public List<string> ReadOnlyClassProp // processed (by content)
- { get; private set; }
-
- [YamlSerialize(YamlSerializeMethod.Content)]
- public List<string> ClassPropByContent// processed (by content)
- { get; set; }
-
- public int[] IntArrayField = // processed (by assign)
- new int[10];
-
- [YamlSerialize(YamlSerializeMethod.Binary)]
- public int[] IntArrayFieldBinary = // processed (as binary)
- new int[100];
-
- [YamlSerialize(YamlSerializeMethod.Never)]
- public int PublicPropHidden; // Ignored
-
- public Test1()
- {
- ClassPropByAssign = new List<string>();
- ReadOnlyClassProp = new List<string>();
- ClassPropByContent = new List<string>();
- }
- }
-
- public void TestPropertiesAndFields1()
- {
- var test1 = new Test1();
- test1.ClassPropByAssign.Add("abc");
- test1.ReadOnlyClassProp.Add("def");
- test1.ClassPropByContent.Add("ghi");
- var rand = new Random(0);
- for ( int i = 0; i < test1.IntArrayFieldBinary.Length; i++ )
- test1.IntArrayFieldBinary[i] = rand.Next();
-
- var serializer = new YamlSerializer();
- string yaml = serializer.Serialize(test1);
- // %YAML 1.2
- // ---
- // !YamlSerializerTest.Test1
- // PublicProp: 0
- // ClassPropByAssign:
- // Capacity: 4
- // ICollection.Items:
- // - abc
- // ReadOnlyClassProp:
- // Capacity: 4
- // ICollection.Items:
- // - def
- // ClassPropByContent:
- // Capacity: 4
- // ICollection.Items:
- // - ghi
- // PublicField: 0
- // IntArrayField: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- // IntArrayFieldBinary: |+2
- // Gor1XAwenmhGkU5ib9NxR11LXxp1iYlH5LH4c9hImTitWSB9Z78II2UvXSXV99A79fj6UBn3GDzbIbd9
- // yBDjAyslYm58iGd/NN+tVjuLRCg3cJBo+PWMbIWm9n4AEC0E7LKXWV5HXUNk7I13APEDWFMM/kWTz2EK
- // s7LzFw2gBjpKugkmQJqIfinpQ1J1yqhhz/XjA3TBxDBsEuwrD+SNevQSqEC+/KRbwgE6D011ACMeyRt0
- // BOG6ZesRKCtL0YU6tSnLEpgKVBz+R300qD3/W0aZVk+1vHU+auzyGCGUaHCGd6dpRoEhXoIg2m3+AwJX
- // EJ37T+TA9BuEPJtyGoq+crQMFQtXj1Zriz3HFbReclLvDdVpZlcOHPga/3+3Y509EHZ7UyT7H1xGeJxn
- // eXPrDDb0Ul04MfZb4UYREOfR3HNzNTUYGRsIPUvHOEW7AaoplIfkVQp19DvGBrBqlP2TZ9atlWUHVdth
- // 7lIBeIh0wiXxoOpCbQ7qVP9GkioQUrMkOcAJaad3exyZaOsXxznFCA==
- // ...
- }
-
-
- Default values of fields and properties
-
-
- using System.ComponentModel;
-
- public class Test2
- {
- [DefaultValue(0)]
- public int Default0 = 0;
-
- [DefaultValue("a")]
- public string Defaulta = "a";
-
- public int DynamicDefault = 0;
-
- bool ShouldSerializeDynamicDefault()
- {
- return Default0 != DynamicDefault;
- }
- }
-
- public void TestDefaultValue()
- {
- var test2 = new Test2();
- var serializer = new YamlSerializer();
-
- // All properties have defalut values.
- var yaml = serializer.Serialize(test2);
- // %YAML 1.2
- // ---
- // !YamlSerializerTest.Test2 {}
- // ...
-
- test2.Defaulta = "b";
- yaml = serializer.Serialize(test2);
- // %YAML 1.2
- // ---
- // !YamlSerializerTest.Test2
- // Defaulta: b
- // ...
-
- test2.Defaulta = "a";
- var yaml = serializer.Serialize(test2);
- // %YAML 1.2
- // ---
- // !YamlSerializerTest.Test2 {}
- // ...
-
- test2.DynamicDefault = 1;
- yaml = serializer.Serialize(test2);
- // %YAML 1.2
- // ---
- // !YamlSerializerTest.Test2
- // DynamicDefault: 1
- // ...
-
- test2.Default0 = 1;
- yaml = serializer.Serialize(test2);
- // %YAML 1.2
- // ---
- // !YamlSerializerTest.Test2
- // Default0: 1
- // ...
- }
-
-
- Collection classes
-
- Multitime appearance of a same object
-
-
- public class TestClass
- {
- public List<TestClass> list =
- new List<TestClass>();
- }
-
- public class ChildClass: TestClass
- {
- }
-
- void RecursiveObjectsTest()
- {
- var a = new TestClass();
- var b = new ChildClass();
- a.list.Add(a);
- a.list.Add(a);
- a.list.Add(b);
- a.list.Add(a);
- a.list.Add(b);
- b.list.Add(a);
- var serializer = new YamlSerializer();
- string yaml = serializer.Serialize(a);
- // %YAML 1.2
- // ---
- // &A !TestClass
- // list:
- // Capacity: 8
- // ICollection.Items:
- // - *A
- // - *A
- // - &B !ChildClass
- // list:
- // Capacity: 4
- // ICollection.Items:
- // - *A
- // - *A
- // - *B
- // ...
-
- var restored = (TestClass)serializer.Deserialize(yaml)[0];
- Assert.IsTrue(restored == restored.list[0]);
- Assert.IsTrue(restored == restored.list[1]);
- Assert.IsTrue(restored == restored.list[3]);
- Assert.IsTrue(restored == restored.list[5]);
- Assert.IsTrue(restored.list[2] == restored.list[4]);
- }
-
-
-
- // 1000 chars
- string long_str =
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +
- "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
- string short_str = "12345";
- object obj = new object[] { long_str, long_str, short_str, short_str };
- var serializer = new YamlSerializer();
- string yaml = serializer.Serialize(obj);
- // %YAML 1.2
- // ---
- // - &A 01234567890123456789012345678901234567890123456789 ... (snip) ... 789
- // - *A
- // - "12345"
- // - "12345"
- // ...
-
-
- YAML text written / read by
-
-
- [Flags]
- enum TestEnum: uint
- {
- abc = 1,
- あいう = 2
- }
-
- public void TestVariousFormats()
- {
- var dict = new Dictionary<object, object>();
- dict.Add(new object[] { 1, "a" }, new object());
- object obj = new object[]{
- dict,
- null,
- "abc",
- "1",
- "a ",
- "- a",
- "abc\n",
- "abc\ndef\n",
- "abc\ndef\nghi",
- new double[]{ 1.1, 2, -3, 3.12, 13.2 },
- new int[,] { { 1, 3}, {4, 5}, {10, 1} },
- new string[]{ "jkl", "mno\npqr" },
- new System.Drawing.Point(1,3),
- TestEnum.abc,
- TestEnum.abc | TestEnum.あいう,
- };
- var config = new YamlConfig();
- config.ExplicitlyPreserveLineBreaks = false;
- var serializer = new YamlSerializer(config);
- string yaml = serializer.Serialize(obj);
-
- // %YAML 1.2
- // ---
- // - !<!System.Collections.Generic.Dictionary%602[[System.Object,...],[System.Object,...]]>
- // Keys: {}
- // Values: {}
- // IDictionary.Entries:
- // ? - 1
- // - a
- // : !System.Object {}
- // - null
- // - abc
- // - "1"
- // - "a "
- // - "- a"
- // - "abc\n"
- // - |+2
- // abc
- // def
- // - |-2
- // abc
- // def
- // ghi
- // - !<!System.Double[]> [1.1, 2, -3, 3.12, 13.2]
- // - !<!System.Int32[,]> [[1, 3], [4, 5], [10, 1]]
- // - !<!System.String[]>
- // - jkl
- // - |-2
- // mno
- // pqr
- // - !System.Drawing.Point 1, 3
- // - !TestEnum abc
- // - !TestEnum abc, あいう
- // ...
- }
-
-
-
- var serializer = new YamlSerializer();
- var yaml = serializer.Serialize("abc");
- // %YAML 1.2\r\n // line breaks are explicitly shown in this example
- // ---\r\n
- // abc\r\n
- // ...\r\n
-
- var config = new YamlConfig();
- config.LineBreakForOutput = "\n";
- serializer = new YamlSerializer(config);
- var yaml = serializer.Serialize("abc");
- // %YAML 1.2\n
- // ---\n
- // abc\n
- // ...\n
-
- YamlNode.DefaultConfig.LineBreakForOutput = "\n";
-
- var serializer = new YamlSerializer();
- serializer = new YamlSerializer();
- var yaml = serializer.Serialize("abc");
- // %YAML 1.2\n
- // ---\n
- // abc\n
- // ...\n
-
-
- Line breaks in YAML text
-
-
- var serializer = new YamlSerializer();
- var text = "abc\r\n def\r\nghi\r\n";
- // abc
- // def
- // ghi
-
- // By default, line breaks explicitly appears in escaped form.
- var yaml = serializer.Serialize(text);
- // %YAML 1.2
- // ---
- // "abc\r\n\
- // \ def\r\n\
- // ghi\r\n"
- // ...
-
- // Original line breaks are preserved
- var restored = (string)serializer.Deserialize(yaml)[0];
- // "abc\r\n def\r\nghi\r\n"
-
-
- YamlNode.DefaultConfig.ExplicitlyPreserveLineBreaks = false;
-
- // Literal style is easier to be read.
- var yaml = serializer.Serialize(text);
- // %YAML 1.2
- // ---
- // |+2
- // abc
- // def
- // ghi
- // ...
-
- // Original line breaks are lost.
- var restored = (string)serializer.Deserialize(yaml)[0];
- // "abc\n def\nghi\n"
-
-
-
- var serializer = new YamlSerializer();
-
- // text with mixed line breaks
- var text = "abc\r def\nghi\r\n";
- // abc\r // line breaks are explicitly shown in this example
- // def\n
- // ghi\r\n
-
- YamlNode.DefaultConfig.ExplicitlyPreserveLineBreaks = false;
-
- // By default, all line breaks are normalized to "\r\n" when serialized.
- var yaml = serializer.Serialize(text);
- // %YAML 1.2\r\n
- // ---\r\n
- // |+2\r\n
- // abc\r\n
- // def\r\n
- // ghi\r\n
- // ...\r\n
-
- // When deserialized, line breaks are normalized into "\n".
- var restored = (string)serializer.Deserialize(yaml)[0];
- // "abc\n def\nghi\n"
-
- // Line breaks are normalized into "\r\n" instead of "\n".
- YamlNode.DefaultConfig.LineBreakForInput = "\r\n";
- restored = (string)serializer.Deserialize(yaml)[0];
- // "abc\r\n def\r\nghi\r\n"
-
- // Line breaks are written as is,
- YamlNode.DefaultConfig.NormalizeLineBreaks = false;
- var yaml = serializer.Serialize(text);
- // %YAML 1.2\r\n
- // ---\r\n
- // |+2\r\n
- // abc\r
- // def\n
- // ghi\r\n
- // ...\r\n
-
- // and are read as is.
- restored = (string)serializer.Deserialize(yaml)[0];
- // "abc\r def\nghi\r\n"
-
- // Note that when the line breaks of YAML stream is changed
- // between serialization and deserialization, the original
- // line breaks are lost.
- yaml = yaml.Replace("\r\n", "\n").Replace("\r", "\n");
- restored = (string)serializer.Deserialize(yaml)[0];
- // "abc\n def\nghi\n"
-
-
-
+ YamlNode node;
+ YamlPresenter.ToYaml(node);
+
+ YamlNode node1;
+ YamlNode node2;
+ YamlNode node3;
+ YamlPresenter.ToYaml(node1, node2, node3);
+
+
- Type type;
- AttributeType attr = type.GetAttribute<AttributeType>();
-
- PropertyInfo propInfo;
- AttributeType attr = propInfo.GetAttribute<AttributeType>();
-
- string name;
- Type type = TypeUtils.GetType(name); // search from all assembly loaded
-
-
-
-
- YAML Ain’t Markup Language (YAML™) Version 1.2
- 3rd Edition (2009-07-21)
- http://yaml.org/spec/1.2/spec.html
- string yaml = LoadYamlSource();
- YamlParser parser = new YamlParser();
- Node[] result = null;
- try {
- result = parser.Parse(yaml);
- ...
- // you can reuse parser as many times you want
- ...
-
- } catch( ParseErrorException e ) {
- MessageBox.Show(e.Message);
- }
- if(result != null) {
- ...
-
- }
-
-
- object obj;
- YamlNode node = YamlRepresenter.ObjectToNode(obj);
-
-
- YamlNode node;
- YamlPresenter.ToYaml(node);
-
- YamlNode node1;
- YamlNode node2;
- YamlNode node3;
- YamlPresenter.ToYaml(node1, node2, node3);
-
-
- I don't remember why this class was needed....
-
- object obj = GetObjectToConvert();
-
- // Check if the type has [TypeConverter] attribute.
- if( EasyTypeConverter.IsTypeConverterSpecified(type) ) {
-
- // Convert the object to string.
- string s = EasyTypeConverter.ConvertToString(obj);
-
- // Convert the string to an object of the spific type.
- object restored = EasyTypeConverter.ConvertFromString(s, type);
-
- Assert.AreEqual(obj, restored);
-
- }
-
-