// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tools.DotNETCommon { /// /// Specifies how to format JSON output /// [Obsolete("Functionality in the Tools.DotNETCommon namespace is deprecated. Please reference the EpicGames.Core namespace and assembly instead.")] public enum JsonWriterStyle { /// /// Omit spaces between elements /// Compact, /// /// Put each value on a newline, and indent output /// Readable } /// /// Writer for JSON data, which indents the output text appropriately, and adds commas and newlines between fields /// [Obsolete("Functionality in the Tools.DotNETCommon namespace is deprecated. Please reference the EpicGames.Core namespace and assembly instead.")] public class JsonWriter : IDisposable { TextWriter Writer; bool bLeaveOpen; JsonWriterStyle Style; bool bRequiresComma; string Indent; /// /// Constructor /// /// File to write to /// Should use packed JSON or not public JsonWriter(string FileName, JsonWriterStyle Style = JsonWriterStyle.Readable) : this(new StreamWriter(FileName)) { this.Style = Style; } /// /// Constructor /// /// File to write to /// Should use packed JSON or not public JsonWriter(FileReference FileName, JsonWriterStyle Style = JsonWriterStyle.Readable) : this(new StreamWriter(FileName.FullName)) { this.Style = Style; } /// /// Constructor /// /// The text writer to output to /// Whether to leave the writer open when the object is disposed /// The output style public JsonWriter(TextWriter Writer, bool bLeaveOpen = false, JsonWriterStyle Style = JsonWriterStyle.Readable) { this.Writer = Writer; this.bLeaveOpen = bLeaveOpen; this.Style = Style; Indent = ""; } /// /// Dispose of any managed resources /// public void Dispose() { if(!bLeaveOpen && Writer != null) { Writer.Dispose(); Writer = null; } } private void IncreaseIndent() { if (Style == JsonWriterStyle.Readable) { Indent += "\t"; } } private void DecreaseIndent() { if (Style == JsonWriterStyle.Readable) { Indent = Indent.Substring(0, Indent.Length - 1); } } /// /// Write the opening brace for an object /// public void WriteObjectStart() { WriteCommaNewline(); Writer.Write(Indent); Writer.Write("{"); IncreaseIndent(); bRequiresComma = false; } /// /// Write the name and opening brace for an object /// /// Name of the field public void WriteObjectStart(string ObjectName) { WriteCommaNewline(); string Space = (Style == JsonWriterStyle.Readable) ? " " : ""; Writer.Write("{0}\"{1}\":{2}", Indent, ObjectName, Space); bRequiresComma = false; WriteObjectStart(); } /// /// Write the closing brace for an object /// public void WriteObjectEnd() { DecreaseIndent(); WriteLine(); Writer.Write(Indent); Writer.Write("}"); bRequiresComma = true; } /// /// Write the opening bracket for an unnamed array /// /// Name of the field public void WriteArrayStart() { WriteCommaNewline(); Writer.Write("{0}[", Indent); IncreaseIndent(); bRequiresComma = false; } /// /// Write the name and opening bracket for an array /// /// Name of the field public void WriteArrayStart(string ArrayName) { WriteCommaNewline(); string Space = (Style == JsonWriterStyle.Readable) ? " " : ""; Writer.Write("{0}\"{1}\":{2}[", Indent, ArrayName, Space); IncreaseIndent(); bRequiresComma = false; } /// /// Write the closing bracket for an array /// public void WriteArrayEnd() { DecreaseIndent(); WriteLine(); Writer.Write("{0}]", Indent); bRequiresComma = true; } private void WriteLine() { if (Style == JsonWriterStyle.Readable) { Writer.WriteLine(); } } private void WriteLine(string Line) { if (Style == JsonWriterStyle.Readable) { Writer.WriteLine(Line); } else { Writer.Write(Line); } } /// /// Write an array of strings /// /// Name of the field /// Values for the field public void WriteStringArrayField(string Name, IEnumerable Values) { WriteArrayStart(Name); foreach(string Value in Values) { WriteValue(Value); } WriteArrayEnd(); } /// /// Write an array of enum values /// /// Name of the field /// Values for the field public void WriteEnumArrayField(string Name, IEnumerable Values) where T : struct { WriteStringArrayField(Name, Values.Select(x => x.ToString())); } /// /// Write a value with no field name, for the contents of an array /// /// Value to write public void WriteValue(int Value) { WriteCommaNewline(); Writer.Write(Indent); Writer.Write(Value); bRequiresComma = true; } /// /// Write a value with no field name, for the contents of an array /// /// Value to write public void WriteValue(string Value) { WriteCommaNewline(); Writer.Write(Indent); WriteEscapedString(Value); bRequiresComma = true; } /// /// Write a field name and string value /// /// Name of the field /// Value for the field public void WriteValue(string Name, string Value) { WriteCommaNewline(); string Space = (Style == JsonWriterStyle.Readable) ? " " : ""; Writer.Write("{0}\"{1}\":{2}", Indent, Name, Space); WriteEscapedString(Value); bRequiresComma = true; } /// /// Write a field name and integer value /// /// Name of the field /// Value for the field public void WriteValue(string Name, int Value) { WriteValueInternal(Name, Value.ToString()); } /// /// Write a field name and double value /// /// Name of the field /// Value for the field public void WriteValue(string Name, double Value) { WriteValueInternal(Name, Value.ToString()); } /// /// Write a field name and bool value /// /// Name of the field /// Value for the field public void WriteValue(string Name, bool Value) { WriteValueInternal(Name, Value ? "true" : "false"); } /// /// Write a field name and enum value /// /// The enum type /// Name of the field /// Value for the field public void WriteEnumValue(string Name, T Value) where T : struct { WriteValue(Name, Value.ToString()); } void WriteCommaNewline() { if (bRequiresComma) { WriteLine(","); } else if (Indent.Length > 0) { WriteLine(); } } void WriteValueInternal(string Name, string Value) { WriteCommaNewline(); string Space = (Style == JsonWriterStyle.Readable) ? " " : ""; Writer.Write("{0}\"{1}\":{2}{3}", Indent, Name, Space, Value); bRequiresComma = true; } void WriteEscapedString(string Value) { // Escape any characters which may not appear in a JSON string (see http://www.json.org). Writer.Write("\""); if (Value != null) { Writer.Write(Json.EscapeString(Value)); } Writer.Write("\""); } } }