private void ScalarToYaml(YamlScalar node, string pres, Context c) { var s = node.Value; // If tag can be resolved from the content, or tag is !!str, // no need to explicitly specify it. var auto_tag = YamlNode.ShorthandTag(AutoTagResolver.Resolve(s)); var tag = TagToYaml(node, auto_tag); if (tag != "" && tag != "!!str") { Write(tag + " "); } if (IsValidPlainText(s, c) && !(node.ShorthandTag() == "!!str" && auto_tag != null && !node.Properties.ContainsKey("plainText"))) { // one line plain style Write(s); if (c != Context.NoBreak) { WriteLine(); } } else { if (ForbiddenChars.IsMatch(s) || OneLine.IsMatch(s) || (config.ExplicitlyPreserveLineBreaks && GetPropertyOrNull(node, "Don'tCareLineBreaks") == null)) { // double quoted Write(DoubleQuotedString.Quote(s, pres, c)); if (c != Context.NoBreak) { WriteLine(); } } else { // Literal style if (s[s.Length - 1] == '\n' || s[s.Length - 1] == '\r') { WriteLine("|+2"); } else { WriteLine("|-2"); s += "\r\n"; // guard } var press = pres + indent; for (int p = 0; p < s.Length;) { var m = UntilBreak.Match(s, p); // does not fail because of the guard Write(pres + s.Substring(p, m.Length)); p += m.Length; } } } }
public bool Encode(object obj, out YamlScalar node) { node = null; YamlTagResolutionRule rule; if (!TypeToRule.TryGetValue(obj.GetType(), out rule)) { return(false); } node = new YamlScalar(rule.Tag, rule.Encode(obj)); return(true); }
/// <summary> /// Decode <paramref name="text"/> and returns actual value in C# object. /// </summary> /// <param name="node">Node to be decoded.</param> /// <param name="obj">Decoded value.</param> /// <returns>True if decoded successfully.</returns> public bool Decode(YamlScalar node, out object obj) { obj = null; if (node.Tag == null || node.Value == null) { return(false); } var tag = YamlNode.ExpandTag(node.Tag); if (!types.ContainsKey(tag)) { return(false); } foreach (var rule in types[tag]) { var m = rule.Pattern.Match(node.Value); if (m.Success) { obj = rule.Decode(m); return(true); } } return(false); }
public void TestVerbatimTags() { var node = new YamlScalar("!Test", "value"); var yaml = node.ToYaml(); Assert.AreEqual( BuildResult("!Test value"), yaml); node = new YamlScalar("!!Test", "value"); yaml = node.ToYaml(); Assert.AreEqual( BuildResult("!!Test value"), yaml); node = new YamlScalar("!System.Int32", "value"); yaml = node.ToYaml(); Assert.AreEqual( BuildResult("!System.Int32 value"), yaml); node = new YamlScalar("!Test+Test", "value"); yaml = node.ToYaml(); Assert.AreEqual( BuildResult("!Test%2BTest value"), yaml); }
public void MergeKey() { var map = new YamlMapping("existing", "value"); var mergeKey = new YamlScalar("!!merge", "<<"); map.Add(mergeKey, new YamlMapping("existing", "new value")); map.OnLoaded(); Assert.AreEqual(1, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); map.Add(mergeKey, new YamlMapping("not existing", "new value")); map.OnLoaded(); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode) "new value", map["not existing"]); map.Add(mergeKey, new YamlMapping("key1", "value1", 2, 2, 3.0, 3.0)); map.OnLoaded(); Assert.AreEqual(5, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode) "new value", map["not existing"]); Assert.IsTrue(map.ContainsKey(2)); Assert.AreEqual((YamlNode) 2, map[2]); Assert.IsTrue(map.ContainsKey(3.0)); Assert.AreEqual((YamlNode) 3.0, map[3.0]); map = new YamlMapping( "existing", "value", mergeKey, new YamlMapping("not existing", "new value")); map.OnLoaded(); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey("existing")); Assert.AreEqual((YamlNode) "value", map["existing"]); Assert.IsTrue(map.ContainsKey("not existing")); Assert.AreEqual((YamlNode) "new value", map["not existing"]); map = (YamlMapping) YamlNode.FromYaml( "key1: value1\r\n" + "key2: value2\r\n" + "<<: \r\n" + " key2: value2 modified\r\n" + " key3: value3\r\n" + " <<: \r\n" + " key4: value4\r\n" + " <<: value5\r\n" + "key6: <<\r\n")[0]; var mapBack = map.ToYaml(); Assert.AreEqual(@"%YAML 1.2 --- <<: value5 key1: value1 key2: value2 key3: value3 key4: value4 key6: << ... ", mapBack); Assert.IsTrue(map.ContainsKey(mergeKey)); Assert.AreEqual(mergeKey, map["key6"]); map.Remove(mergeKey); map.Add(mergeKey, map); // recursive map.OnLoaded(); // nothing has been changed mapBack = map.ToYaml(); Assert.AreEqual(@"%YAML 1.2 --- key1: value1 key2: value2 key3: value3 key4: value4 key6: << ... ", mapBack); }