public WriteComment ( string text ) : void | ||
text | string | Text to place inside the comment. |
return | void |
static void Main(string[] args) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter writer = new JsonTextWriter(sw)) { writer.Formatting = Newtonsoft.Json.Formatting.Indented; writer.WriteStartObject(); writer.WritePropertyName("CPU"); writer.WriteValue("Intel"); writer.WritePropertyName("PSU"); writer.WriteValue("500W"); writer.WritePropertyName("Drives"); writer.WriteStartArray(); writer.WriteValue("DVD read/writer"); writer.WriteComment("(broken)"); writer.WriteValue("500 gigabyte hard drive"); writer.WriteValue("200 gigabype hard drive"); writer.WriteEnd(); writer.WriteEndObject(); } File.WriteAllText(Environment.CurrentDirectory + "/rhcdata.json", sw.ToString()); Console.WriteLine(sw.ToString()); Console.ReadLine(); }
public void Indenting() { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("CPU"); jsonWriter.WriteValue("Intel"); jsonWriter.WritePropertyName("PSU"); jsonWriter.WriteValue("500W"); jsonWriter.WritePropertyName("Drives"); jsonWriter.WriteStartArray(); jsonWriter.WriteValue("DVD read/writer"); jsonWriter.WriteComment("(broken)"); jsonWriter.WriteValue("500 gigabyte hard drive"); jsonWriter.WriteValue("200 gigabype hard drive"); jsonWriter.WriteEnd(); jsonWriter.WriteEndObject(); Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); } // { // "CPU": "Intel", // "PSU": "500W", // "Drives": [ // "DVD read/writer" // /*(broken)*/, // "500 gigabyte hard drive", // "200 gigabype hard drive" // ] // } string expected = @"{ ""CPU"": ""Intel"", ""PSU"": ""500W"", ""Drives"": [ ""DVD read/writer"" /*(broken)*/, ""500 gigabyte hard drive"", ""200 gigabype hard drive"" ] }"; string result = sb.ToString(); Assert.AreEqual(expected, result); }
public void CloseWithRemainingContent() { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("CPU"); jsonWriter.WriteValue("Intel"); jsonWriter.WritePropertyName("PSU"); jsonWriter.WriteValue("500W"); jsonWriter.WritePropertyName("Drives"); jsonWriter.WriteStartArray(); jsonWriter.WriteValue("DVD read/writer"); jsonWriter.WriteComment("(broken)"); jsonWriter.WriteValue("500 gigabyte hard drive"); jsonWriter.WriteValue("200 gigabype hard drive"); jsonWriter.Close(); } string expected = @"{ ""CPU"": ""Intel"", ""PSU"": ""500W"", ""Drives"": [ ""DVD read/writer"" /*(broken)*/, ""500 gigabyte hard drive"", ""200 gigabype hard drive"" ] }"; string result = sb.ToString(); Assert.AreEqual(expected, result); }
void TrStmtList(List<Statement/*!*/>/*!*/ stmts) { Contract.Requires(cce.NonNullElements(stmts)); List<LocalVariable> AllDecls = new List<LocalVariable>(); using (WriteArray()) { j.WriteValue(KremlinAst.ESequence); using (WriteArray()) { WriteEUnit(); // in case the statement list is empty foreach (Statement ss in stmts) { // JsonTextWriter is forward-only, but after calling TrStmt() we may need // to go back and inject new ELet statements to introduce temp variables. // So call TrStmt() once to generate code to a throw-away MemoryStream, // but remember what temps need to be introduced. Then introduce them // and call TrStmt() once more, to generate the actual Json. JsonTextWriter oldj = j; VariableTracker oldtracker = VarTracker.Clone(); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonTextWriter newj = new JsonTextWriter(sw); newj.Formatting = oldj.Formatting; newj.Indentation = oldj.Indentation; j = newj; var oldVarDeclsList = varDeclsList; varDeclsList = new List<LocalVariable>(); TrStmt(ss); j = oldj; VarTracker = oldtracker; var decls = varDeclsList; // Grab the set of just-declared variables generated by the first TrStmt() pass varDeclsList = null; // Switch modes for next TrStmt() pass AllDecls.AddRange(decls); // Accumulate the set of all variables this stmt list has generated foreach (var l in decls) { // ELet v in { Stmt j.WriteStartArray(); j.WriteValue(KremlinAst.ELet); j.WriteStartArray(); WriteBinder(l, l.Name, true); // lident WriteDefaultValue(l.Type); // = default VarTracker.Push(l); // "in" is the contents that follow j.WriteStartArray(); j.WriteValue(KremlinAst.ESequence); j.WriteStartArray(); WriteEUnit(); } newj.Close(); string RawJson = sb.ToString(); if (RawJson != "") { j.WriteRaw(","); j.WriteWhitespace("\n"); j.WriteRaw(RawJson); // Now paste the JSON } if (ss.Labels != null) { // bugbug: these appear to be used to support "Break" statements. j.WriteComment("Labels are unsupported: " + ss.Labels.Data.AssignUniqueId("after_", idGenerator)); } varDeclsList = oldVarDeclsList; } // Now that all statements in the list have been generated, close out the nested ELets generated above AllDecls.Reverse(); foreach (var l in AllDecls) { VarTracker.Pop(l); j.WriteEndArray(); // Closing out the expr list in the ESequence j.WriteEndArray(); // Closing out the array aboce ESequence j.WriteEndArray(); // Closing out the list of binder * expr * expr j.WriteEndArray(); // Closing out the array above ELet } } } }
private string ModsToJson(List<Mod> mods) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter writer = new JsonTextWriter(sw)) { writer.Formatting = Formatting.Indented; writer.WriteComment("DO NOT EDIT THIS BY HAND WITHOUT KNOWING EXACTLY WHAT YOU ARE DOING" + Environment.NewLine + "THIS FILE IS CREATED BY MCM AND IS USED FOR KNOWING WHICH MODS ARE INSTALLED!"); writer.WriteStartObject(); writer.WritePropertyName("mods"); writer.WriteStartArray(); foreach (Mod mod in mods) { writer.WriteStartObject(); writer.WritePropertyName("name"); writer.WriteValue(mod.name); writer.WritePropertyName("path"); writer.WriteValue(mod.path); writer.WritePropertyName("type"); writer.WriteValue(mod.type); writer.WritePropertyName("level"); writer.WriteValue(mod.level); writer.WritePropertyName("version"); writer.WriteValue(mod.version.Key); writer.WriteEndObject(); } writer.WriteEnd(); } return sb.ToString(); }
private static void WriteJsonToTextWriter(TextWriter textWriter, bool skipComment = false) { _serializer.Formatting = Formatting.Indented; if (!skipComment) WriteComment(textWriter, ConfigFormat.Json); using (JsonWriter writer = new JsonTextWriter(textWriter)) { writer.Formatting = Formatting.Indented; writer.WriteStartObject(); lock (_current) { foreach (var kvp in _current) { var objType = kvp.Value.GetType(); var levelAttr = objType.GetCustomAttribute<ConfigKeySerializeLevelAttribute>(); string key; if (levelAttr != null) { var keys = kvp.Key.Split('.'); key = string.Join(".", keys.Skip(keys.Length - levelAttr.Level)); if (string.IsNullOrEmpty(key) || levelAttr.Level == 0) continue; } else { key = kvp.Key; } var configAttr = objType.GetCustomAttribute<ConfigCommentAttribute>(); if (!string.IsNullOrEmpty(configAttr?.Comment)) writer.WriteComment(configAttr.Comment); writer.WritePropertyName(key, false); writer.WriteStartObject(); foreach (var prop in objType.Properties()) { WriteJsonValue(writer, prop, prop.GetValue(kvp.Value, null), prop.PropertyType); } foreach (var field in objType.Fields()) { WriteJsonValue(writer, field, field.GetValue(kvp.Value), field.FieldType); } writer.WriteEndObject(); } writer.WriteEndObject(); } } }
public override bool Process(HttpServer server, HttpRequest request, HttpResponse response) { if (request.Page.StartsWith("/status")) { response.ContentType = (string)SubstitutingFileReader.MimeTypes[".html"]; response.ReturnCode = 200; StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("twain_source"); jsonWriter.WriteValue(Properties.Settings.Default.twain_source); jsonWriter.WriteComment("ID of selected TWAIN source"); jsonWriter.WritePropertyName("x"); jsonWriter.WriteValue(Properties.Settings.Default.x); jsonWriter.WriteComment("The x-coordinate of the upper-left corner of the crop rectangle"); jsonWriter.WritePropertyName("y"); jsonWriter.WriteValue(Properties.Settings.Default.y); jsonWriter.WriteComment("The y-coordinate of the upper-left corner of the crop rectangle"); jsonWriter.WritePropertyName("w"); jsonWriter.WriteValue(Properties.Settings.Default.w); jsonWriter.WriteComment("The width of the crop rectangle"); jsonWriter.WritePropertyName("h"); jsonWriter.WriteValue(Properties.Settings.Default.h); jsonWriter.WriteComment("The height of the crop rectangle"); jsonWriter.WritePropertyName("encode"); jsonWriter.WriteValue(Properties.Settings.Default.encode); jsonWriter.WriteComment("Encoding setup"); jsonWriter.WritePropertyName("server_autostart"); jsonWriter.WriteValue(Properties.Settings.Default.server_autostart); jsonWriter.WriteComment("Shall web server be started automatically"); jsonWriter.WritePropertyName("start_minimized"); jsonWriter.WriteValue(Properties.Settings.Default.start_minimized); jsonWriter.WriteComment("Shall application start minimized"); jsonWriter.WritePropertyName("port"); jsonWriter.WriteValue(Properties.Settings.Default.port); jsonWriter.WriteComment("Web server listening port"); jsonWriter.WritePropertyName("dpi"); jsonWriter.WriteValue(Properties.Settings.Default.dpi); jsonWriter.WriteComment("DPI"); jsonWriter.WritePropertyName("colour"); jsonWriter.WriteValue(Properties.Settings.Default.colour == ColourSetting.Colour ? "full" : Properties.Settings.Default.colour == ColourSetting.GreyScale ? "greyscale" : "blackandwhite"); jsonWriter.WriteComment("Colouring setup"); jsonWriter.WritePropertyName("adf"); jsonWriter.WriteValue(Properties.Settings.Default.useADF); jsonWriter.WriteComment("Shall ADF be used?"); jsonWriter.WriteEndObject(); } response.Content = sb.ToString(); return true; } return false; }