static YamlNode SaveNode(string name, dynamic node) { if (node == null) { return(new YamlScalarNode("null")); } else if (IsReferenceNode(node)) { if (NodePaths[node].Tag == null) { NodePaths[node].Tag = $"!ref{refNodeId++}"; } return(new YamlScalarNode($"!refTag={NodePaths[node].Tag}")); } else if ((node is IList <dynamic>)) { var yamlNode = new YamlSequenceNode(); // NodePaths.Add(node, yamlNode); if (!HasEnumerables((IList <dynamic>)node) && ((IList <dynamic>)node).Count < 6) { yamlNode.Style = SharpYaml.YamlStyle.Flow; } foreach (var item in (IList <dynamic>)node) { yamlNode.Add(SaveNode(null, item)); } return(yamlNode); } else if (node is IDictionary <string, dynamic> ) { var yamlNode = new YamlMappingNode(); // NodePaths.Add(node, yamlNode); if (!HasEnumerables((IDictionary <string, dynamic>)node) && ((IDictionary <string, dynamic>)node).Count < 6) { yamlNode.Style = SharpYaml.YamlStyle.Flow; } foreach (var item in (IDictionary <string, dynamic>)node) { string key = item.Key; YamlNode keyNode = new YamlScalarNode(key); if (BYAML.IsHash(key)) { uint hash = Convert.ToUInt32(key, 16); if (BYAML.Hashes.ContainsKey(hash)) { key = $"{BYAML.Hashes[hash]}"; } keyNode = new YamlScalarNode(key); keyNode.Tag = "!h"; } yamlNode.Add(keyNode, SaveNode(item.Key, item.Value)); } return(yamlNode); } else if (node is ByamlPathPoint) { return(ConvertPathPoint((ByamlPathPoint)node)); } else if (node is List <ByamlPathPoint> ) { var yamlNode = new YamlSequenceNode(); foreach (var pt in (List <ByamlPathPoint>)node) { yamlNode.Add(ConvertPathPoint(pt)); } return(yamlNode); } else { string tag = null; if (node is int) { tag = "!l"; } else if (node is uint) { tag = "!u"; } else if (node is Int64) { tag = "!ul"; } else if (node is double) { tag = "!d"; } else if (node is ByamlPathIndex) { tag = "!p"; } var yamlNode = new YamlScalarNode(ConvertValue(node)); if (tag != null) { yamlNode.Tag = tag; } return(yamlNode); } }
static dynamic ConvertValue(string value, string tag) { if (tag == null) { tag = ""; } if (value == "null") { return(null); } else if (value == "true") { return(true); } else if (value == "false") { return(false); } else if (tag == "!u") { return(UInt32.Parse(value, CultureInfo.InvariantCulture)); } else if (tag == "!l") { return(Int32.Parse(value, CultureInfo.InvariantCulture)); } else if (tag == "!d") { return(Double.Parse(value, CultureInfo.InvariantCulture)); } else if (tag == "!ul") { return(UInt64.Parse(value, CultureInfo.InvariantCulture)); } else if (tag == "!ll") { return(Int64.Parse(value, CultureInfo.InvariantCulture)); } else if (tag == "!p") { return new ByamlPathIndex() { Index = Int32.Parse(value, CultureInfo.InvariantCulture) } } ; else { float floatValue = 0; bool isFloat = float.TryParse(value, out floatValue); if (isFloat) { return(floatValue); } else { if (BYAML.IsHash(value)) { uint hash = Convert.ToUInt32(value, 16); if (BYAML.Hashes.ContainsKey(hash)) { return($"!h {BYAML.Hashes[hash]}"); } } return(value); } } }