public void WriteBasicUtf8() { _arrayBufferWriter.Clear(); using (var json = new Utf8JsonWriter(_arrayBufferWriter, new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation })) { json.WriteStartObject(); json.WriteNumber(AgeUtf8, 42); json.WriteString(FirstUtf8, "John"); json.WriteString(LastUtf8, "Smith"); json.WriteStartArray(PhoneNumbersUtf8); json.WriteStringValue("425-000-1212"); json.WriteStringValue("425-000-1213"); json.WriteEndArray(); json.WriteStartObject(AddressUtf8); json.WriteString(StreetUtf8, "1 Microsoft Way"); json.WriteString(CityUtf8, "Redmond"); json.WriteNumber(ZipUtf8, 98052); json.WriteEndObject(); json.WriteStartArray(ExtraArrayUtf8); for (int i = 0; i < DataSize; i++) { json.WriteNumberValue(_numberArrayValues[i]); } json.WriteEndArray(); json.WriteEndObject(); json.Flush(); } }
public void WriteDeepUtf8() { _arrayBufferWriter.Clear(); using (var json = new Utf8JsonWriter(_arrayBufferWriter, new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation })) { json.WriteStartObject(); for (int i = 0; i < Depth; i++) { json.WriteStartObject(_propertyNamesUtf8[i]); } json.WriteStartArray(ExtraArrayUtf8); for (int i = 0; i < DataSize; i++) { json.WriteStringValue(_stringArrayValues[i]); json.WriteNumberValue(_numberArrayValues[i]); } json.WriteEndArray(); for (int i = 0; i < Depth; i++) { json.WriteEndObject(); } json.WriteEndObject(); json.Flush(); } }
public void WriteStringsUtf8() { _arrayBufferWriter.Clear(); using (var json = new Utf8JsonWriter(_arrayBufferWriter, new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation })) { json.WriteStartArray(); for (int i = 0; i < DataSize; i++) { json.WriteStringValue(_stringArrayValuesUtf8[i]); } json.WriteEndArray(); json.Flush(); } }
private void WriteByteArrayAsBase64Core(byte[] data) { _arrayBufferWriter.Clear(); using (var json = new Utf8JsonWriter(_arrayBufferWriter)) { json.WriteBase64StringValue(data); json.Flush(); } }
private static void WriteComplexValue( bool indented, string jsonIn, string expectedIndent, string expectedMinimal) { var buffer = new ArrayBufferWriter <byte>(1024); byte[] bufferOutput; var options = new JsonWriterOptions { Indented = indented, }; using (JsonDocument doc = JsonDocument.Parse($" [ {jsonIn} ]", s_options)) { JsonElement target = doc.RootElement[0]; using var writer = new Utf8JsonWriter(buffer, options); target.WriteTo(writer); writer.Flush(); if (indented && s_replaceNewlines) { JsonTestHelper.AssertContents( expectedIndent.Replace(CompiledNewline, Environment.NewLine), buffer); } JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer); bufferOutput = buffer.WrittenSpan.ToArray(); } // After reading the output and writing it again, it should be byte-for-byte identical. { string bufferString = Encoding.UTF8.GetString(bufferOutput); buffer.Clear(); using (JsonDocument doc2 = JsonDocument.Parse(bufferString, s_options)) { using (var writer = new Utf8JsonWriter(buffer, options)) { doc2.RootElement.WriteTo(writer); } } Assert.True(buffer.WrittenSpan.SequenceEqual(bufferOutput)); } }
public static void WriteWithRelaxedEscaper(bool indented, string jsonIn, string jsonOut, bool matchesRelaxedEscaping) { var buffer = new ArrayBufferWriter <byte>(1024); using (JsonDocument doc = JsonDocument.Parse($" [ {jsonIn} ]", s_options)) { JsonElement target = doc.RootElement[0]; { var options = new JsonWriterOptions { Indented = indented, }; using var writer = new Utf8JsonWriter(buffer, options); target.WriteTo(writer); writer.Flush(); if (matchesRelaxedEscaping) { JsonTestHelper.AssertContents(jsonOut, buffer); } else { JsonTestHelper.AssertContentsNotEqual(jsonOut, buffer); } } buffer.Clear(); { var options = new JsonWriterOptions { Indented = indented, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }; using var writer = new Utf8JsonWriter(buffer, options); target.WriteTo(writer); writer.Flush(); JsonTestHelper.AssertContents(jsonOut, buffer); } } }
public void WriteWithRelaxedEscaper(bool indented, string jsonIn, string jsonOut, bool matchesRelaxedEscaping) { var buffer = new ArrayBufferWriter <byte>(1024); using (JsonDocument doc = PrepareDocument(jsonIn)) { { var options = new JsonWriterOptions { Indented = indented, }; using (var writer = new Utf8JsonWriter(buffer, options)) { WriteSingleValue(doc, writer); } if (matchesRelaxedEscaping) { JsonTestHelper.AssertContents(jsonOut, buffer); } else { JsonTestHelper.AssertContentsNotEqual(jsonOut, buffer); } } buffer.Clear(); { var options = new JsonWriterOptions { Indented = indented, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }; using (var writer = new Utf8JsonWriter(buffer, options)) { WriteSingleValue(doc, writer); } JsonTestHelper.AssertContents(jsonOut, buffer); } } }
private void WriteComplexValue( bool indented, string jsonIn, string expectedIndent, string expectedMinimal) { var buffer = new ArrayBufferWriter <byte>(1024); byte[] bufferOutput; var options = new JsonWriterOptions { Indented = indented }; using (JsonDocument doc = PrepareDocument(jsonIn)) { using (var writer = new Utf8JsonWriter(buffer, options)) { WriteSingleValue(doc, writer); } JsonTestHelper.AssertContents(indented ? expectedIndent : expectedMinimal, buffer); bufferOutput = buffer.WrittenSpan.ToArray(); } // After reading the output and writing it again, it should be byte-for-byte identical. { string bufferString = Encoding.UTF8.GetString(bufferOutput); buffer.Clear(); using (JsonDocument doc2 = PrepareDocument(bufferString)) { using (var writer = new Utf8JsonWriter(buffer, options)) { WriteSingleValue(doc2, writer); } } Assert.True(buffer.WrittenSpan.SequenceEqual(bufferOutput)); } }