private static string GetExpectedString(bool prettyPrint, bool isUtf8, int[] data) { MemoryStream ms = new MemoryStream(); StreamWriter streamWriter = new StreamWriter(ms, new UTF8Encoding(false), 1024, true); StringBuilder sb = new StringBuilder(); StringWriter stringWriter = new StringWriter(sb); TextWriter writer = isUtf8 ? streamWriter : (TextWriter)stringWriter; var json = new Newtonsoft.Json.JsonTextWriter(writer) { Formatting = prettyPrint ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None }; json.WriteStartObject(); json.WritePropertyName("age"); json.WriteValue(42); json.WritePropertyName("first"); json.WriteValue("John"); json.WritePropertyName("last"); json.WriteValue("Smith"); json.WritePropertyName("phoneNumbers"); json.WriteStartArray(); json.WriteValue("425-000-1212"); json.WriteValue("425-000-1213"); json.WriteEnd(); json.WritePropertyName("address"); json.WriteStartObject(); json.WritePropertyName("street"); json.WriteValue("1 Microsoft Way"); json.WritePropertyName("city"); json.WriteValue("Redmond"); json.WritePropertyName("zip"); json.WriteValue(98052); json.WriteEnd(); // Add a large array of values json.WritePropertyName("ExtraArray"); json.WriteStartArray(); for (var i = 0; i < ExtraArraySize; i++) { json.WriteValue(data[i]); } json.WriteEnd(); json.WriteEnd(); json.Flush(); return(isUtf8 ? Encoding.UTF8.GetString(ms.ToArray()) : sb.ToString()); }
//[Benchmark] public void WriteArrayNewtonsoft() { using (var json = new Newtonsoft.Json.JsonTextWriter(GetWriter())) { json.Formatting = Formatted ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; json.WriteStartObject(); json.WritePropertyName("message"); json.WriteStartArray(); for (var i = 0; i < _longs.Length; i++) { json.WriteValue(_longs[i]); } json.WriteEnd(); json.WriteEnd(); } }
private static void WriterNewtonsoftHelloWorld(bool formatted, TextWriter writer) { using (var json = new Newtonsoft.Json.JsonTextWriter(writer)) { json.Formatting = formatted ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; json.WriteStartObject(); json.WritePropertyName("message"); json.WriteValue("Hello, World!"); json.WriteEnd(); } }
private static void WriterNewtonsoftBasic(bool formatted, StreamWriter writer) { using (var json = new Newtonsoft.Json.JsonTextWriter(writer)) { json.Formatting = formatted ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; json.WriteStartObject(); json.WritePropertyName("age"); json.WriteValue(42); json.WritePropertyName("first"); json.WriteValue("John"); json.WritePropertyName("last"); json.WriteValue("Smith"); json.WritePropertyName("phoneNumbers"); json.WriteStartArray(); json.WriteValue("425-000-1212"); json.WriteValue("425-000-1213"); json.WriteEnd(); json.WritePropertyName("address"); json.WriteStartObject(); json.WritePropertyName("street"); json.WriteValue("1 Microsoft Way"); json.WritePropertyName("city"); json.WriteValue("Redmond"); json.WritePropertyName("zip"); json.WriteValue(98052); json.WriteEnd(); // Add a large array of values json.WritePropertyName("ExtraArray"); json.WriteStartArray(); for (var i = 0; i < ExtraArraySize; i++) { json.WriteValue(i); } json.WriteEnd(); json.WriteEnd(); } }
private static void WriterNewtonsoftBasic(bool formatted, TextWriter writer, ReadOnlySpan <int> data) { using (var json = new Newtonsoft.Json.JsonTextWriter(writer)) { json.Formatting = formatted ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None; json.WriteStartObject(); json.WritePropertyName("age"); json.WriteValue(42); json.WritePropertyName("first"); json.WriteValue("John"); json.WritePropertyName("last"); json.WriteValue("Smith"); json.WritePropertyName("phoneNumbers"); json.WriteStartArray(); json.WriteValue("425-000-1212"); json.WriteValue("425-000-1213"); json.WriteEnd(); json.WritePropertyName("address"); json.WriteStartObject(); json.WritePropertyName("street"); json.WriteValue("1 Microsoft Way"); json.WritePropertyName("city"); json.WriteValue("Redmond"); json.WritePropertyName("zip"); json.WriteValue(98052); json.WriteEnd(); json.WritePropertyName("ExtraArray"); json.WriteStartArray(); for (var i = 0; i < data.Length; i++) { json.WriteValue(data[i]); } json.WriteEnd(); json.WriteEnd(); } }
private static string GetHelloWorldExpectedString(bool prettyPrint, bool isUtf8) { MemoryStream ms = new MemoryStream(); StreamWriter streamWriter = new StreamWriter(ms, new UTF8Encoding(false), 1024, true); StringBuilder sb = new StringBuilder(); StringWriter stringWriter = new StringWriter(sb); TextWriter writer = isUtf8 ? streamWriter : (TextWriter)stringWriter; var json = new Newtonsoft.Json.JsonTextWriter(writer) { Formatting = prettyPrint ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None }; json.WriteStartObject(); json.WritePropertyName("message"); json.WriteValue("Hello, World!"); json.WriteEnd(); json.Flush(); return(isUtf8 ? Encoding.UTF8.GetString(ms.ToArray()) : sb.ToString()); }