/// <summary> /// Writes a SKONObject. /// </summary> /// <param name="obj">The object to write.</param> /// <returns>A string to write into a file.</returns> public static string Write(SKONObject obj, SKONMetadata metadata = default(SKONMetadata)) { if (obj.Type != SKONValueType.MAP) { throw new ArgumentException("SKONObject to write must be of type map!"); } if (ContainsLoops(obj)) { throw new ArgumentException("Could not write SKONObject due to recursive references!"); } //There might be something we would want to do if the versions don't match metadata.LanguageVersion = LanguageVersion; StringBuilder sb = new StringBuilder(); sb.Append($"{Metadelimit}Version: {metadata.LanguageVersion}{Metadelimit}\n"); sb.Append($"{Metadelimit}DocumentVersion: \"{metadata.DocuemntVersion}\"{Metadelimit}\n"); if (metadata.SKEMA != null && metadata.SKEMA.Length > 0) { sb.Append($"{Metadelimit}SKEMA: {metadata.SKEMA}{Metadelimit}\n"); } foreach (string key in obj.Keys) { sb.Append(key + ": " + WriteObject(obj[key], 0) + ",\n"); } return(sb.ToString()); }
/// <summary> /// Parses a SKON string to a SKONObject. /// </summary> /// <param name="skon">The SKON data string.</param> /// <param name="errorStream">The TextWriter to write error messages to.</param> /// <returns>The newly created SKONObject.</returns> public static SKONObject Parse(string skon, out SKONMetadata metadata, TextWriter errorStream = null) { using (MemoryStream stream = ParserUtils.GenerateStreamFromString(skon)) { Scanner sc = new Scanner(stream); return(Parse(sc, out metadata, errorStream)); } }
/// <summary> /// Loads a text file as a SKON Map. /// </summary> /// <param name="path">Full FilePath to the SKON text file.</param> /// <param name="errorStream">The TextWriter to write error messages to.</param> /// <returns>The root map containing all SKONObjects.</returns> public static SKONObject LoadFile(string path, out SKONMetadata metadata, TextWriter errorStream = null) { if (File.Exists(path)) { Scanner sc = new Scanner(path); return(Parse(sc, out metadata, errorStream)); } else { throw new FileNotFoundException("File not found!", path); } }
/// <summary> /// /// </summary> /// <param name="sc"></param> /// <param name="errorStream"></param> /// <returns></returns> private static SKONObject Parse(Scanner sc, out SKONMetadata metadata, TextWriter errorStream) { Parser parser = new Parser(sc); if (errorStream != null) { parser.errors.errorStream = errorStream; } parser.Parse(); if (parser.errors.count > 0) { throw new FormatException(string.Format("Could not parse file! Got {0} errors!", parser.errors.count)); } metadata = parser.metadata; return(parser.data); }
/// <summary> /// Parses a SKON stream to a SKONObject. /// </summary> /// <param name="stream">The SKON data stream.</param> /// <param name="errorStream">The TextWriter to write error messages to.</param> /// <returns></returns> public static SKONObject Parse(Stream stream, out SKONMetadata metadata, TextWriter errorStream = null) { Scanner sc = new Scanner(stream); return(Parse(sc, out metadata, errorStream)); }