Exemple #1
0
        private static void WriteAnything(YamlWriter yaml, string propName, object obj)
        {
            if (obj == null)
            {
                // Exclude default values.
                // This provides more stable output as new things are added in the future.
                return;
            }
            else if (obj is bool valBool)
            {
                // Exclude default values.
                if (valBool)
                {
                    yaml.WriteProperty(propName, valBool);
                }
            }
            else if (obj is string valString)
            {
                yaml.WriteProperty(propName, valString, false);
            }
            else if (obj is int valInt)
            {
                yaml.WriteProperty(propName, valInt);
            }
            else if (obj is double valDouble)
            {
                yaml.WriteProperty(propName, valDouble);
            }
            else if (obj.GetType().IsEnum)
            {
                var valEnum = obj.ToString();
                yaml.WriteProperty(propName, valEnum, false);
            }
            else
            {
                // Dictionary / nested object?
                if (obj is IDictionary dict)
                {
                    if (dict.Count > 0)
                    {
                        var list = new List <KeyValuePair <string, object> >();
                        foreach (DictionaryEntry kv in dict)
                        {
                            list.Add(new KeyValuePair <string, object>((string)kv.Key, kv.Value));
                        }


                        yaml.WriteStartObject(propName);
                        WriteCanonicalList(yaml, list);
                        yaml.WriteEndObject();
                    }
                }
                else
                {
                    yaml.WriteStartObject(propName);
                    WriteObject(yaml, obj);
                    yaml.WriteEndObject();
                }
            }
        }
Exemple #2
0
        public void Write1()
        {
            var sw = new StringWriter();
            var yw = new YamlWriter(sw);

            yw.WriteProperty("P0", "abc");
            yw.WriteStartObject("Obj1");
            yw.WriteProperty("P1a", "A#B");     // induced multiline, |- since no trailing \n
            yw.WriteProperty("P1b", "B");
            yw.WriteStartObject("Obj2");
            yw.WriteProperty("P2a", "A");
            yw.WriteEndObject();
            yw.WriteProperty("P1c", "C");
            yw.WriteEndObject();

            var t        = sw.ToString();
            var expected = @"P0: =abc
Obj1:
    P1a: |-
        =A#B
    P1b: =B
    Obj2:
        P2a: =A
    P1c: =C
";

            Assert.AreEqual(expected.Replace("\r\n", "\n"), t.Replace("\r\n", "\n"));
        }