private static void WriterSystemJsonBasic(bool formatted, bool skipValidation, ArrayFormatterWrapper output, ReadOnlySpan <int> data) { var state = new JsonWriterState(options: new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation }); var json = new Utf8JsonWriter2(output, state); json.WriteStartObject(); json.WriteNumber("age", 42); json.WriteString("first", "John"); json.WriteString("last", "Smith"); json.WriteStartArray("phoneNumbers"); json.WriteStringValue("425-000-1212"); json.WriteStringValue("425-000-1213"); json.WriteEndArray(); json.WriteStartObject("address"); json.WriteString("street", "1 Microsoft Way"); json.WriteString("city", "Redmond"); json.WriteNumber("zip", 98052); json.WriteEndObject(); json.WriteStartArray("ExtraArray"); for (var i = 0; i < data.Length; i++) { json.WriteNumberValue(data[i]); } json.WriteEndArray(); json.WriteEndObject(); json.Flush(); }
//[Benchmark] public void WriteArrayLoop() { _arrayFormatterWrapper.Clear(); var state = new JsonWriterState(options: new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation }); var json = new Utf8JsonWriter2(_arrayFormatterWrapper, state); json.WriteStartObject(); json.WriteStartArray(Message, suppressEscaping: true); for (int i = 0; i < _longs.Length; i++) { json.WriteNumberValue(_longs[i]); } json.WriteEndArray(); json.WriteEndObject(); json.Flush(); }
//[Benchmark] public void WriterSystemTextJsonValues() { _arrayFormatterWrapper.Clear(); var state = new JsonWriterState(options: new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation }); var json = new Utf8JsonWriter2(_arrayFormatterWrapper, state); json.WriteStartObject(); json.WriteStartArray(Encoding.UTF8.GetBytes("numbers"), suppressEscaping: true); for (int i = 0; i < 100; i++) { json.WriteNumberValue(12345); } json.WriteEndArray(); json.WriteEndObject(); json.Flush(); }