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(); } } }
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")); }
public void NewLinesRoundtrip(string value) { var sw = new StringWriter(); var yw = new YamlWriter(sw); yw.WriteProperty("Foo", value); var text = sw.ToString(); var normalizedValue = NormNewlines(value); // Validate it passes YamlDotNet var valueFromYaml = ParseSinglePropertyViaYamlDotNot(text); Assert.AreEqual(normalizedValue, NormNewlines(valueFromYaml)); // Validate it passes our subset. var sr = new StringReader(text); var y = new YamlLexer(sr); var p = y.ReadNext(); Assert.AreEqual(YamlTokenKind.Property, p.Kind); Assert.AreEqual("Foo", p.Property); Assert.AreEqual(normalizedValue, NormNewlines(p.Value)); }
public void NewLinesRoundtrip(string value) { var sw = new StringWriter(); var yw = new YamlWriter(sw); yw.WriteProperty("Foo", value); var text = sw.ToString(); // Validate it passes YamlDotNet var valueFromYaml = ParseSinglePropertyViaYamlDotNot(text); var valueWithoutR = value.Replace("\r", ""); // yamlDotNet doesn't do \R Assert.AreEqual(valueWithoutR, valueFromYaml); // Validate it passes our subset. var sr = new StringReader(text); var y = new YamlLexer(sr); var p = y.ReadNext(); Assert.AreEqual(YamlTokenKind.Property, p.Kind); Assert.AreEqual("Foo", p.Property); Assert.AreEqual(value, p.Value); }
public static void Write(TextWriter writer, IDictionary <string, string> properties) { var yaml = new YamlWriter(writer); // Sort by keys to enforce canonical format. foreach (var kv in properties.OrderBy(x => x.Key)) { yaml.WriteProperty(kv.Key, kv.Value); } writer.Flush(); }
public void WriteEscapes(string value) { var sw = new StringWriter(); var yw = new YamlWriter(sw); yw.WriteProperty("Foo", value); var text = sw.ToString(); // We use a | escape. Assert.IsTrue(text.StartsWith("Foo: |")); }