/// <summary> /// Creates a VdfSerializer object /// </summary> /// <param name="value">the VdfValue to serialize</param> public VdfSerializer(VdfValue value) { root = value; }
private void RunSerialization(Action <string> onStep, VdfValue current) { if (current.Comments.Count > 0) { foreach (var s in current.Comments) { onStep(IndentString); onStep(CommentDelimiter); onStep(EscapeString(s)); onStep(Newline); } } onStep(IndentString); WriteString(onStep, current.Name, true); switch (current.Type) { case VdfValueType.String: { onStep(KVSeperator); WriteString(onStep, (current as VdfString).Content, true); onStep(Newline); break; } case VdfValueType.Long: { onStep(KVSeperator); WriteString(onStep, (current as VdfLong).Content.ToString(CultureInfo.InvariantCulture), false); onStep(Newline); break; } case VdfValueType.Decimal: { onStep(KVSeperator); WriteString(onStep, (current as VdfDecimal).Content.ToString(CultureInfo.InvariantCulture), false); onStep(Newline); break; } case VdfValueType.Table: { onStep(Newline); onStep(IndentString); onStep(TableOpen); onStep(Newline); indentLevel++; foreach (var v in current as VdfTable) { RunSerialization(onStep, v); } indentLevel--; onStep(IndentString); onStep(TableClose); onStep(Newline); break; } } }
/// <summary> /// Deserializes the Vdf string into a VdfValue /// </summary> /// <returns></returns> public VdfValue Deserialize() { if (VdfText == null) { throw new ArgumentNullException("s"); } if (VdfText.Length < 1) { throw new ArgumentException("s cannot be empty ", "s"); } var tokens = Tokenize(VdfText); if (tokens.Count < 1) { throw new ArgumentException("no tokens found in string", "s"); } VdfValue root = null; VdfTable current = null; var comments = new List <string>(); string name = null; foreach (var token in tokens) { if (token.type == TokenType.Comment) { comments.Add(token.content.Substring(2)); continue; } if (root == null) { if (token.type == TokenType.String) { if (name != null) { return(new VdfString(name, token.content)); } name = token.content; } else if (token.type == TokenType.TableStart) { root = new VdfTable(name); if (comments.Count > 0) { foreach (var comment in comments) { root.Comments.Add(comment); } comments.Clear(); } current = root as VdfTable; name = null; } else { throw new VdfDeserializationException("Invalid format: First token was not a string"); } continue; } if (name != null) { VdfValue v; if (token.type == TokenType.String) { long i; decimal d; if (long.TryParse(token.content, NumberStyles.Integer, CultureInfo.InvariantCulture, out i)) { v = new VdfLong(name, i); } else if (decimal.TryParse(token.content, NumberStyles.Number, CultureInfo.InvariantCulture, out d)) { v = new VdfDecimal(name, d); } else { v = new VdfString(name, token.content); } if (comments.Count > 0) { foreach (var comment in comments) { v.Comments.Add(comment); } comments.Clear(); } name = null; current.Add(v); } else if (token.type == TokenType.TableStart) { v = new VdfTable(name); if (comments.Count > 0) { foreach (var comment in comments) { v.Comments.Add(comment); } comments.Clear(); } current.Add(v); name = null; current = v as VdfTable; } } else { if (token.type == TokenType.String) { name = token.content; } else if (token.type == TokenType.TableEnd) { current = current.Parent as VdfTable; } else { throw new VdfDeserializationException("Invalid Format: a name was needed but not found"); } } } if (current != null) { throw new VdfDeserializationException("Invalid format: unclosed table"); } return(root); }