public bool NameEquals(System.ReadOnlySpan<byte> utf8Text) { throw null; }
public bool NameEquals(System.ReadOnlySpan<char> text) { throw null; }
public bool NameEquals(string text) { throw null; }
- public void WriteTo(System.Text.Json.Utf8JsonWriter writer) { }
public override string ToString() { throw null; }
+ public void WriteTo(System.Text.Json.Utf8JsonWriter writer) { }
}
public partial struct JsonReaderOptions
{
{
public static object Deserialize(System.ReadOnlySpan<byte> utf8Json, System.Type returnType, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
public static object Deserialize(string json, System.Type returnType, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static TValue Deserialize<TValue>(System.ReadOnlySpan<byte> utf8Json, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static TValue Deserialize<TValue>(string json, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
+ public static object Deserialize(ref System.Text.Json.Utf8JsonReader reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
public static System.Threading.Tasks.ValueTask<object> DeserializeAsync(System.IO.Stream utf8Json, System.Type returnType, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.ValueTask<TValue> DeserializeAsync<TValue>(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
- public static object Deserialize(ref System.Text.Json.Utf8JsonReader reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
+ public static TValue Deserialize<TValue>(System.ReadOnlySpan<byte> utf8Json, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
+ public static TValue Deserialize<TValue>(string json, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
public static TValue Deserialize<TValue>(ref System.Text.Json.Utf8JsonReader reader, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static string Serialize(object value, System.Type inputType, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static byte[] SerializeToUtf8Bytes(object value, System.Type inputType, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static byte[] SerializeToUtf8Bytes<TValue>(TValue value, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
- public static System.Threading.Tasks.Task SerializeAsync(System.IO.Stream utf8Json, object value, System.Type inputType, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
+ public static string Serialize(object value, System.Type type, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
+ public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object value, System.Type type, System.Text.Json.JsonSerializerOptions options = null) { }
+ public static System.Threading.Tasks.Task SerializeAsync(System.IO.Stream utf8Json, object value, System.Type type, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task SerializeAsync<TValue>(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
- public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object value, System.Type inputType, System.Text.Json.JsonSerializerOptions options = null) { }
+ public static byte[] SerializeToUtf8Bytes(object value, System.Type type, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
+ public static byte[] SerializeToUtf8Bytes<TValue>(TValue value, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
public static void Serialize<TValue>(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions options = null) { }
+ public static string Serialize<TValue>(TValue value, System.Text.Json.JsonSerializerOptions options = null) { throw null; }
}
public sealed partial class JsonSerializerOptions
{
public System.Collections.Generic.IList<System.Text.Json.Serialization.JsonConverter> Converters { get { throw null; } }
public int DefaultBufferSize { get { throw null; } set { } }
public System.Text.Json.JsonNamingPolicy DictionaryKeyPolicy { get { throw null; } set { } }
+ public System.Text.Encodings.Web.JavaScriptEncoder Encoder { get { throw null; } set { } }
public bool IgnoreNullValues { get { throw null; } set { } }
public bool IgnoreReadOnlyProperties { get { throw null; } set { } }
public int MaxDepth { get { throw null; } set { } }
}
public partial struct JsonWriterOptions
{
+ private object _dummy;
private int _dummyPrimitive;
+ public System.Text.Encodings.Web.JavaScriptEncoder Encoder { readonly get { throw null; } set { } }
public bool Indented { get { throw null; } set { } }
public bool SkipValidation { get { throw null; } set { } }
}
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text.Json.Serialization;
+using System.Text.Encodings.Web;
namespace System.Text.Json
{
private JsonNamingPolicy _dictionayKeyPolicy;
private JsonNamingPolicy _jsonPropertyNamingPolicy;
private JsonCommentHandling _readCommentHandling;
+ private JavaScriptEncoder _encoder;
private int _defaultBufferSize = BufferSizeDefault;
private int _maxDepth;
private bool _allowTrailingCommas;
}
/// <summary>
+ /// The encoder to use when escaping strings, or <see langword="null" /> to use the default encoder.
+ /// </summary>
+ public JavaScriptEncoder Encoder
+ {
+ get
+ {
+ return _encoder;
+ }
+ set
+ {
+ VerifyMutable();
+
+ _encoder = value;
+ }
+ }
+
+ /// <summary>
/// Specifies the policy used to convert a <see cref="System.Collections.IDictionary"/> key's name to another format, such as camel-casing.
/// </summary>
/// <remarks>
{
return new JsonWriterOptions
{
+ Encoder = Encoder,
Indented = WriteIndented,
#if !DEBUG
SkipValidation = true
using System.Buffers.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
using System.Text.Encodings.Web;
+using System.Text.Unicode;
namespace System.Text.Json
{
return AllowList[value] == 0;
}
+ private static bool NeedsEscapingNoBoundsCheck(char value)
+ {
+ Debug.Assert(value <= LastAsciiCharacter);
+ return AllowList[value] == 0;
+ }
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool NeedsEscaping(byte value) => value > LastAsciiCharacter || AllowList[value] == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool NeedsEscaping(char value) => value > LastAsciiCharacter || AllowList[value] == 0;
- public static int NeedsEscaping(ReadOnlySpan<byte> value, JavaScriptEncoder encoder = null)
+ public static int NeedsEscaping(ReadOnlySpan<byte> value, JavaScriptEncoder encoder)
{
if (encoder != null)
{
return idx;
}
- public static int NeedsEscaping(ReadOnlySpan<char> value)
+ public static int NeedsEscaping(ReadOnlySpan<char> value, JavaScriptEncoder encoder)
{
+ if (encoder != null)
+ {
+ return encoder.FindFirstCharacterToEncodeUtf8(MemoryMarshal.Cast<char, byte>(value));
+ }
+
int idx;
for (idx = 0; idx < value.Length; idx++)
{
return firstIndexToEscape + JsonConstants.MaxExpansionFactorWhileEscaping * (textLength - firstIndexToEscape);
}
+ private static void EscapeString(ReadOnlySpan<byte> value, Span<byte> destination, JavaScriptEncoder encoder, ref int written)
+ {
+ Debug.Assert(encoder != null);
+
+ OperationStatus result = encoder.EncodeUtf8(value, destination, out int encoderBytesConsumed, out int encoderBytesWritten);
+
+ Debug.Assert(result != OperationStatus.DestinationTooSmall);
+ Debug.Assert(result != OperationStatus.NeedMoreData);
+ Debug.Assert(encoderBytesConsumed == value.Length);
+
+ if (result != OperationStatus.Done)
+ {
+ ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesWritten));
+ }
+
+ written += encoderBytesWritten;
+ }
+
public static void EscapeString(ReadOnlySpan<byte> value, Span<byte> destination, int indexOfFirstByteToEscape, JavaScriptEncoder encoder, out int written)
{
Debug.Assert(indexOfFirstByteToEscape >= 0 && indexOfFirstByteToEscape < value.Length);
if (encoder != null)
{
- OperationStatus result = encoder.EncodeUtf8(
- value.Slice(consumed), destination.Slice(written), out int encoderBytesConsumed, out int encoderBytesWritten);
-
- Debug.Assert(result != OperationStatus.DestinationTooSmall);
- Debug.Assert(result != OperationStatus.NeedMoreData);
- Debug.Assert(encoderBytesConsumed == value.Length - consumed);
-
- if (result != OperationStatus.Done)
- {
- ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesWritten));
- }
-
- written += encoderBytesWritten;
+ destination = destination.Slice(indexOfFirstByteToEscape);
+ value = value.Slice(indexOfFirstByteToEscape);
+ EscapeString(value, destination, encoder, ref written);
}
else
{
}
else
{
- // Fall back to default encoder
- OperationStatus result = JavaScriptEncoder.Default.EncodeUtf8(
- value.Slice(consumed), destination.Slice(written), out int encoderBytesConsumed, out int encoderBytesWritten);
-
- Debug.Assert(result != OperationStatus.DestinationTooSmall);
- Debug.Assert(result != OperationStatus.NeedMoreData);
- Debug.Assert(encoderBytesConsumed == value.Length - consumed);
-
- if (result != OperationStatus.Done)
- {
- ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesConsumed));
- }
-
- consumed += encoderBytesConsumed;
- written += encoderBytesWritten;
+ // Fall back to default encoder.
+ destination = destination.Slice(written);
+ value = value.Slice(consumed);
+ EscapeString(value, destination, JavaScriptEncoder.Default, ref written);
+ break;
}
}
}
private static bool IsAsciiValue(byte value) => value <= LastAsciiCharacter;
+ private static bool IsAsciiValue(char value) => value <= LastAsciiCharacter;
+
/// <summary>
/// Returns <see langword="true"/> if <paramref name="value"/> is a UTF-8 continuation byte.
/// A UTF-8 continuation byte is a byte whose value is in the range 0x80-0xBF, inclusive.
return SequenceValidity.Incomplete;
}
- public static void EscapeString(ReadOnlySpan<char> value, Span<char> destination, int indexOfFirstByteToEscape, out int written)
+ private static void EscapeString(ReadOnlySpan<char> value, Span<char> destination, JavaScriptEncoder encoder, ref int written)
{
- Debug.Assert(indexOfFirstByteToEscape >= 0 && indexOfFirstByteToEscape < value.Length);
+ // todo: issue #39523: add an Encode(ReadOnlySpan<char>) decode API to System.Text.Encodings.Web.TextEncoding to avoid utf16->utf8->utf16 conversion.
- value.Slice(0, indexOfFirstByteToEscape).CopyTo(destination);
- written = indexOfFirstByteToEscape;
- int consumed = indexOfFirstByteToEscape;
+ Debug.Assert(encoder != null);
- while (consumed < value.Length)
+ // Convert char to byte.
+ byte[] utf8DestinationArray = null;
+ Span<byte> utf8Destination;
+ int length = checked((value.Length) * JsonConstants.MaxExpansionFactorWhileTranscoding);
+ if (length > JsonConstants.StackallocThreshold)
{
- char val = value[consumed];
- if (NeedsEscaping(val))
+ utf8DestinationArray = ArrayPool<byte>.Shared.Rent(length);
+ utf8Destination = utf8DestinationArray;
+ }
+ else
+ {
+ unsafe
{
- EscapeNextChars(value, val, destination, ref consumed, ref written);
+ byte* ptr = stackalloc byte[JsonConstants.StackallocThreshold];
+ utf8Destination = new Span<byte>(ptr, JsonConstants.StackallocThreshold);
}
- else
+ }
+
+ ReadOnlySpan<byte> utf16Value = MemoryMarshal.AsBytes(value);
+ OperationStatus toUtf8Status = ToUtf8(utf16Value, utf8Destination, out int bytesConsumed, out int bytesWritten);
+
+ Debug.Assert(toUtf8Status != OperationStatus.DestinationTooSmall);
+ Debug.Assert(toUtf8Status != OperationStatus.NeedMoreData);
+
+ if (toUtf8Status != OperationStatus.Done)
+ {
+ if (utf8DestinationArray != null)
{
- destination[written++] = val;
+ utf8Destination.Slice(0, bytesWritten).Clear();
+ ArrayPool<byte>.Shared.Return(utf8DestinationArray);
}
- consumed++;
+
+ ThrowHelper.ThrowArgumentException_InvalidUTF8(utf16Value.Slice(bytesWritten));
}
- }
- private static void EscapeNextChars(ReadOnlySpan<char> value, int firstChar, Span<char> destination, ref int consumed, ref int written)
- {
- int nextChar = -1;
- if (JsonHelpers.IsInRangeInclusive(firstChar, JsonConstants.HighSurrogateStartValue, JsonConstants.LowSurrogateEndValue))
+ Debug.Assert(toUtf8Status == OperationStatus.Done);
+ Debug.Assert(bytesConsumed == utf16Value.Length);
+
+ // Escape the bytes.
+ byte[] utf8ConvertedDestinationArray = null;
+ Span<byte> utf8ConvertedDestination;
+ length = checked(bytesWritten * JsonConstants.MaxExpansionFactorWhileEscaping);
+ if (length > JsonConstants.StackallocThreshold)
+ {
+ utf8ConvertedDestinationArray = ArrayPool<byte>.Shared.Rent(length);
+ utf8ConvertedDestination = utf8ConvertedDestinationArray;
+ }
+ else
{
- consumed++;
- if (value.Length <= consumed || firstChar >= JsonConstants.LowSurrogateStartValue)
+ unsafe
{
- ThrowHelper.ThrowArgumentException_InvalidUTF16(firstChar);
+ byte* ptr = stackalloc byte[JsonConstants.StackallocThreshold];
+ utf8ConvertedDestination = new Span<byte>(ptr, JsonConstants.StackallocThreshold);
}
+ }
+
+ EscapeString(utf8Destination.Slice(0, bytesWritten), utf8ConvertedDestination, indexOfFirstByteToEscape: 0, encoder, out int convertedBytesWritten);
- nextChar = value[consumed];
- if (!JsonHelpers.IsInRangeInclusive(nextChar, JsonConstants.LowSurrogateStartValue, JsonConstants.LowSurrogateEndValue))
+ if (utf8DestinationArray != null)
+ {
+ utf8Destination.Slice(0, bytesWritten).Clear();
+ ArrayPool<byte>.Shared.Return(utf8DestinationArray);
+ }
+
+ // Convert byte to char.
+#if BUILDING_INBOX_LIBRARY
+ OperationStatus toUtf16Status = Utf8.ToUtf16(utf8ConvertedDestination.Slice(0, convertedBytesWritten), destination, out int bytesRead, out int charsWritten);
+ Debug.Assert(toUtf16Status == OperationStatus.Done);
+ Debug.Assert(bytesRead == convertedBytesWritten);
+#else
+ string utf16 = JsonReaderHelper.GetTextFromUtf8(utf8ConvertedDestination.Slice(0, convertedBytesWritten));
+ utf16.AsSpan().CopyTo(destination);
+ int charsWritten = utf16.Length;
+#endif
+ written += charsWritten;
+
+ if (utf8ConvertedDestinationArray != null)
+ {
+ utf8ConvertedDestination.Slice(0, written).Clear();
+ ArrayPool<byte>.Shared.Return(utf8ConvertedDestinationArray);
+ }
+ }
+
+ public static void EscapeString(ReadOnlySpan<char> value, Span<char> destination, int indexOfFirstByteToEscape, JavaScriptEncoder encoder, out int written)
+ {
+ Debug.Assert(indexOfFirstByteToEscape >= 0 && indexOfFirstByteToEscape < value.Length);
+
+ value.Slice(0, indexOfFirstByteToEscape).CopyTo(destination);
+ written = indexOfFirstByteToEscape;
+ int consumed = indexOfFirstByteToEscape;
+
+ if (encoder != null)
+ {
+ destination = destination.Slice(indexOfFirstByteToEscape);
+ value = value.Slice(indexOfFirstByteToEscape);
+ EscapeString(value, destination, encoder, ref written);
+ }
+ else
+ {
+ // For performance when no encoder is specified, perform escaping here for Ascii and on the
+ // first occurrence of a non-Ascii character, then call into the default encoder.
+ while (consumed < value.Length)
{
- ThrowHelper.ThrowArgumentException_InvalidUTF16(nextChar);
+ char val = value[consumed];
+ if (IsAsciiValue(val))
+ {
+ if (NeedsEscapingNoBoundsCheck(val))
+ {
+ EscapeNextChars(val, destination, ref written);
+ consumed++;
+ }
+ else
+ {
+ destination[written] = val;
+ written++;
+ consumed++;
+ }
+ }
+ else
+ {
+ // Fall back to default encoder.
+ destination = destination.Slice(written);
+ value = value.Slice(consumed);
+ EscapeString(value, destination, JavaScriptEncoder.Default, ref written);
+ break;
+ }
}
}
+ }
+
+ private static void EscapeNextChars(char value, Span<char> destination, ref int written)
+ {
+ Debug.Assert(IsAsciiValue(value));
destination[written++] = '\\';
- switch (firstChar)
+ switch ((byte)value)
{
case JsonConstants.Quote:
// Optimize for the common quote case.
default:
destination[written++] = 'u';
#if BUILDING_INBOX_LIBRARY
- firstChar.TryFormat(destination.Slice(written), out int charsWritten, HexFormatString);
+ int intChar = value;
+ intChar.TryFormat(destination.Slice(written), out int charsWritten, HexFormatString);
Debug.Assert(charsWritten == 4);
written += charsWritten;
#else
- written = WriteHex(firstChar, destination, written);
-#endif
- if (nextChar != -1)
- {
- destination[written++] = '\\';
- destination[written++] = 'u';
-#if BUILDING_INBOX_LIBRARY
- nextChar.TryFormat(destination.Slice(written), out charsWritten, HexFormatString);
- Debug.Assert(charsWritten == 4);
- written += charsWritten;
-#else
- written = WriteHex(nextChar, destination, written);
+ written = WriteHex(value, destination, written);
#endif
- }
break;
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Text.Encodings.Web;
+
namespace System.Text.Json
{
/// <summary>
private int _optionsMask;
/// <summary>
+ /// The encoder to use when escaping strings, or <see langword="null" /> to use the default encoder.
+ /// </summary>
+ public JavaScriptEncoder Encoder { get; set; }
+
+ /// <summary>
/// Defines whether the <see cref="Utf8JsonWriter"/> should pretty print the JSON which includes:
/// indenting nested JSON tokens, adding new lines, and adding white space between property names and values.
/// By default, the JSON is written without any extra white space.
private void WriteBase64Escape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteBase64Escape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteBase64ByOptions(escapedPropertyName.Slice(0, written), bytes);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteBase64ByOptions(escapedPropertyName.Slice(0, written), bytes);
private void WriteBase64ByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteBase64Indented(propertyName, bytes);
}
private void WriteBase64ByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteBase64Indented(utf8PropertyName, bytes);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteStringEscape(ReadOnlySpan<char> propertyName, DateTime value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, DateTime value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteStringByOptions(ReadOnlySpan<char> propertyName, DateTime value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(propertyName, value);
}
private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, DateTime value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteStringEscape(ReadOnlySpan<char> propertyName, DateTimeOffset value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteStringByOptions(ReadOnlySpan<char> propertyName, DateTimeOffset value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(propertyName, value);
}
private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteNumberEscape(ReadOnlySpan<char> propertyName, decimal value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, decimal value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, decimal value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(propertyName, value);
}
private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, decimal value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteNumberEscape(ReadOnlySpan<char> propertyName, double value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, double value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, double value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(propertyName, value);
}
private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, double value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteNumberEscape(ReadOnlySpan<char> propertyName, float value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, float value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, float value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(propertyName, value);
}
private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, float value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteNumberEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteLiteralIndented(propertyName, value);
}
private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteLiteralIndented(utf8PropertyName, value);
}
private void WriteStringEscape(ReadOnlySpan<char> propertyName, Guid value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, Guid value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteStringByOptions(ReadOnlySpan<char> propertyName, Guid value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(propertyName, value);
}
private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, Guid value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ValidateWritingProperty()
{
- if (!Options.SkipValidation)
+ if (!_options.SkipValidation)
{
if (!_inObject || _tokenType == JsonTokenType.PropertyName)
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ValidateWritingProperty(byte token)
{
- if (!Options.SkipValidation)
+ if (!_options.SkipValidation)
{
if (!_inObject || _tokenType == JsonTokenType.PropertyName)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteLiteralEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteLiteralEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteLiteralByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteLiteralByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteLiteralByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteLiteralIndented(propertyName, value);
}
private void WriteLiteralByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteLiteralIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteNumberEscape(ReadOnlySpan<char> propertyName, long value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, long value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, long value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(propertyName, value);
}
private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, long value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
{
JsonWriterHelper.ValidateProperty(propertyName);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
}
}
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
propertyName = escapedPropertyName.Slice(0, written);
}
private void WriteStringByOptionsPropertyName(ReadOnlySpan<char> propertyName)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndentedPropertyName(propertyName);
}
{
JsonWriterHelper.ValidateProperty(utf8PropertyName);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
}
}
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
utf8PropertyName = escapedPropertyName.Slice(0, written);
}
private void WriteStringByOptionsPropertyName(ReadOnlySpan<byte> utf8PropertyName)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndentedPropertyName(utf8PropertyName);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
JsonWriterHelper.ValidateValue(value);
- int valueIdx = JsonWriterHelper.NeedsEscaping(value);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2);
JsonWriterHelper.ValidateValue(utf8Value);
- int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2);
JsonWriterHelper.ValidateProperty(propertyName);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
JsonWriterHelper.ValidateProperty(utf8PropertyName);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
stackalloc byte[length] :
(valueArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndex, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndex, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written));
stackalloc char[length] :
(valueArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndex, out int written);
+ JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndex, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written));
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndex, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), escapedValue);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndex, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out int written);
WriteStringByOptions(escapedPropertyName.Slice(0, written), escapedValue);
private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value)
{
- int valueIdx = JsonWriterHelper.NeedsEscaping(value);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
{
- int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value)
{
- int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value)
{
- int valueIdx = JsonWriterHelper.NeedsEscaping(value);
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
}
}
- JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, out int written);
+ JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
value = escapedValue.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
propertyName = escapedPropertyName.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
utf8Value = escapedValue.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
utf8PropertyName = escapedPropertyName.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
utf8Value = escapedValue.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
propertyName = escapedPropertyName.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, out int written);
+ JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
value = escapedValue.Slice(0, written);
}
}
}
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
utf8PropertyName = escapedPropertyName.Slice(0, written);
}
private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(propertyName, value);
}
private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(utf8PropertyName, utf8Value);
}
private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(propertyName, utf8Value);
}
private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
private void WriteNumberEscape(ReadOnlySpan<char> propertyName, ulong value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, ulong value)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, ulong value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(propertyName, value);
}
private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, ulong value)
{
ValidateWritingProperty();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberIndented(utf8PropertyName, value);
}
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteBase64Indented(bytes);
}
private void WriteCommentByOptions(ReadOnlySpan<char> value)
{
- if (Options.Indented)
+ if (_options.Indented)
{
WriteCommentIndented(value);
}
private void WriteCommentByOptions(ReadOnlySpan<byte> utf8Value)
{
- if (Options.Indented)
+ if (_options.Indented)
{
WriteCommentIndented(utf8Value);
}
public void WriteStringValue(DateTime value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringValueIndented(value);
}
public void WriteStringValue(DateTimeOffset value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringValueIndented(value);
}
public void WriteNumberValue(decimal value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberValueIndented(value);
}
JsonWriterHelper.ValidateDouble(value);
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberValueIndented(value);
}
JsonWriterHelper.ValidateSingle(value);
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberValueIndented(value);
}
JsonWriterHelper.ValidateNumber(utf8FormattedNumber);
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberValueIndented(utf8FormattedNumber);
}
public void WriteStringValue(Guid value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringValueIndented(value);
}
{
private void ValidateWritingValue()
{
- if (!Options.SkipValidation)
+ if (!_options.SkipValidation)
{
if (_inObject)
{
else
{
Debug.Assert(destination.Length >= written * JsonConstants.MaxExpansionFactorWhileEscaping);
- JsonWriterHelper.EscapeString(encodedBytes, destination, firstEscapeIndexVal, encoder: null, out written);
+ JsonWriterHelper.EscapeString(encodedBytes, destination, firstEscapeIndexVal, _options.Encoder, out written);
BytesPending += written;
}
private void WriteLiteralByOptions(ReadOnlySpan<byte> utf8Value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteLiteralIndented(utf8Value);
}
public void WriteNumberValue(long value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberValueIndented(value);
}
private void WriteStringEscape(ReadOnlySpan<char> value)
{
- int valueIdx = JsonWriterHelper.NeedsEscaping(value);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < value.Length);
private void WriteStringByOptions(ReadOnlySpan<char> value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(value);
}
stackalloc char[length] :
(valueArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, out int written);
+ JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
WriteStringByOptions(escapedValue.Slice(0, written));
private void WriteStringEscape(ReadOnlySpan<byte> utf8Value)
{
- int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value);
+ int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length);
private void WriteStringByOptions(ReadOnlySpan<byte> utf8Value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteStringIndented(utf8Value);
}
stackalloc byte[length] :
(valueArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
WriteStringByOptions(escapedValue.Slice(0, written));
public void WriteNumberValue(ulong value)
{
ValidateWritingValue();
- if (Options.Indented)
+ if (_options.Indented)
{
WriteNumberValueIndented(value);
}
// else, no list separator is needed since we are writing the first item.
private int _currentDepth;
+ private JsonWriterOptions _options; // Since JsonWriterOptions is a struct, use a field to avoid a copy for internal code.
+
/// <summary>
/// Returns the amount of bytes written by the <see cref="Utf8JsonWriter"/> so far
/// that have not yet been flushed to the output and committed.
/// the <see cref="Utf8JsonWriter"/> which indicates whether to format the output
/// while writing and whether to skip structural JSON validation or not.
/// </summary>
- public JsonWriterOptions Options { get; }
+ public JsonWriterOptions Options
+ {
+ get
+ {
+ return _options;
+ }
+ }
private int Indentation => CurrentDepth * JsonConstants.SpacesPerIndent;
_isNotPrimitive = default;
_tokenType = default;
_currentDepth = default;
- Options = options;
+ _options = options;
// Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle.
// This way we avoid allocations in the common, default cases, and allocate lazily.
_isNotPrimitive = default;
_tokenType = default;
_currentDepth = default;
- Options = options;
+ _options = options;
// Only allocate if the user writes a JSON payload beyond the depth that the _allocationFreeContainer can handle.
// This way we avoid allocations in the common, default cases, and allocate lazily.
if (CurrentDepth >= JsonConstants.MaxWriterDepth)
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.DepthTooLarge, _currentDepth, token: default, tokenType: default);
- if (Options.IndentedOrNotSkipValidation)
+ if (_options.IndentedOrNotSkipValidation)
{
WriteStartSlow(token);
}
private void WriteStartSlow(byte token)
{
- Debug.Assert(Options.Indented || !Options.SkipValidation);
+ Debug.Assert(_options.Indented || !_options.SkipValidation);
- if (Options.Indented)
+ if (_options.Indented)
{
- if (!Options.SkipValidation)
+ if (!_options.SkipValidation)
{
ValidateStart();
UpdateBitStackOnStart(token);
}
else
{
- Debug.Assert(!Options.SkipValidation);
+ Debug.Assert(!_options.SkipValidation);
ValidateStart();
UpdateBitStackOnStart(token);
WriteStartMinimized(token);
private void WriteStartEscape(ReadOnlySpan<byte> utf8PropertyName, byte token)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
{
ValidateWritingProperty(token);
- if (Options.Indented)
+ if (_options.Indented)
{
WritePropertyNameIndented(utf8PropertyName, token);
}
stackalloc byte[length] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, encoder: null, out int written);
+ JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStartByOptions(escapedPropertyName.Slice(0, written), token);
private void WriteStartEscape(ReadOnlySpan<char> propertyName, byte token)
{
- int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName);
+ int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
{
ValidateWritingProperty(token);
- if (Options.Indented)
+ if (_options.Indented)
{
WritePropertyNameIndented(propertyName, token);
}
stackalloc char[length] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
- JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written);
+ JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteStartByOptions(escapedPropertyName.Slice(0, written), token);
private void WriteEnd(byte token)
{
- if (Options.IndentedOrNotSkipValidation)
+ if (_options.IndentedOrNotSkipValidation)
{
WriteEndSlow(token);
}
private void WriteEndSlow(byte token)
{
- Debug.Assert(Options.Indented || !Options.SkipValidation);
+ Debug.Assert(_options.Indented || !_options.SkipValidation);
- if (Options.Indented)
+ if (_options.Indented)
{
- if (!Options.SkipValidation)
+ if (!_options.SkipValidation)
{
ValidateEnd(token);
}
}
else
{
- Debug.Assert(!Options.SkipValidation);
+ Debug.Assert(!_options.SkipValidation);
ValidateEnd(token);
WriteEndMinimized(token);
}
}
Debug.Assert(indent <= 2 * JsonConstants.MaxWriterDepth);
- Debug.Assert(Options.SkipValidation || _tokenType != JsonTokenType.None);
+ Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.None);
int maxRequired = indent + 3; // 1 end token, 1-2 bytes for new line
#endif
);
- // Temporary hack until we can use the same escape algorithm throughout.
+ // Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Collections.Generic;
+using System.Text.Encodings.Web;
+using System.Text.Unicode;
using Xunit;
namespace System.Text.Json.Serialization.Tests
Assert.False(options.AllowTrailingCommas);
Assert.Equal(16 * 1024, options.DefaultBufferSize);
Assert.Equal(null, options.DictionaryKeyPolicy);
+ Assert.Null(options.Encoder);
Assert.False(options.IgnoreNullValues);
Assert.Equal(0, options.MaxDepth);
Assert.Equal(false, options.PropertyNameCaseInsensitive);
Assert.Throws<InvalidOperationException>(() => options.AllowTrailingCommas = options.AllowTrailingCommas);
Assert.Throws<InvalidOperationException>(() => options.DefaultBufferSize = options.DefaultBufferSize);
Assert.Throws<InvalidOperationException>(() => options.DictionaryKeyPolicy = options.DictionaryKeyPolicy);
+ Assert.Throws<InvalidOperationException>(() => options.Encoder = JavaScriptEncoder.Default);
Assert.Throws<InvalidOperationException>(() => options.IgnoreNullValues = options.IgnoreNullValues);
Assert.Throws<InvalidOperationException>(() => options.MaxDepth = options.MaxDepth);
Assert.Throws<InvalidOperationException>(() => options.PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<BasicCompany>(BasicCompany.s_data, options));
}
+
+ private class TestClassForEncoding
+ {
+ public string MyString { get; set; }
+ }
+
+ // This is a copy of the test data in System.Text.Json.Tests.JsonEncodedTextTests.JsonEncodedTextStringsCustom
+ public static IEnumerable<object[]> JsonEncodedTextStringsCustom
+ {
+ get
+ {
+ return new List<object[]>
+ {
+ new object[] { "age", "\\u0061\\u0067\\u0065" },
+ new object[] { "éééééêêêêê", "éééééêêêêê" },
+ new object[] { "ééééé\"êêêêê", "ééééé\\u0022êêêêê" },
+ new object[] { "ééééé\\u0022êêêêê", "ééééé\\\\\\u0075\\u0030\\u0030\\u0032\\u0032êêêêê" },
+ new object[] { "ééééé>>>>>êêêêê", "ééééé\\u003E\\u003E\\u003E\\u003E\\u003Eêêêêê" },
+ new object[] { "ééééé\\u003e\\u003eêêêêê", "ééééé\\\\\\u0075\\u0030\\u0030\\u0033\\u0065\\\\\\u0075\\u0030\\u0030\\u0033\\u0065êêêêê" },
+ new object[] { "ééééé\\u003E\\u003Eêêêêê", "ééééé\\\\\\u0075\\u0030\\u0030\\u0033\\u0045\\\\\\u0075\\u0030\\u0030\\u0033\\u0045êêêêê" },
+ };
+ }
+ }
+
+ [Theory]
+ [MemberData(nameof(JsonEncodedTextStringsCustom))]
+ public static void CustomEncoderAllowLatin1Supplement(string message, string expectedMessage)
+ {
+ // Latin-1 Supplement block starts from U+0080 and ends at U+00FF
+ JavaScriptEncoder encoder = JavaScriptEncoder.Create(UnicodeRanges.Latin1Supplement);
+
+ var options = new JsonSerializerOptions();
+ options.Encoder = encoder;
+
+ var obj = new TestClassForEncoding();
+ obj.MyString = message;
+
+ string baselineJson = JsonSerializer.Serialize(obj);
+ Assert.DoesNotContain(expectedMessage, baselineJson);
+
+ string json = JsonSerializer.Serialize(obj, options);
+ Assert.Contains(expectedMessage, json);
+
+ obj = JsonSerializer.Deserialize<TestClassForEncoding>(json);
+ Assert.Equal(obj.MyString, message);
+ }
+
+ public static IEnumerable<object[]> JsonEncodedTextStringsCustomAll
+ {
+ get
+ {
+ return new List<object[]>
+ {
+ new object[] { "éééééêêêêê", "éééééêêêêê" },
+ new object[] { "aѧѦa", "aѧѦa" }, // U0467, U0466
+ };
+ }
+ }
+
+ [Theory]
+ [MemberData(nameof(JsonEncodedTextStringsCustomAll))]
+ public static void JsonEncodedTextStringsCustomAllowAll(string message, string expectedMessage)
+ {
+ // Allow all unicode values (except forbidden characters which we don't have in test data here)
+ JavaScriptEncoder encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
+
+ var options = new JsonSerializerOptions();
+ options.Encoder = encoder;
+
+ var obj = new TestClassForEncoding();
+ obj.MyString = message;
+
+ string baselineJson = JsonSerializer.Serialize(obj);
+ Assert.DoesNotContain(expectedMessage, baselineJson);
+
+ string json = JsonSerializer.Serialize(obj, options);
+ Assert.Contains(expectedMessage, json);
+
+ obj = JsonSerializer.Deserialize<TestClassForEncoding>(json);
+ Assert.Equal(obj.MyString, message);
+ }
}
}
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Threading.Tasks;
using System.IO.Pipelines;
using System.Collections.Generic;
+using System.Text.Encodings.Web;
+using System.Text.Unicode;
namespace System.Text.Json.Tests
{
Assert.Equal(0, writer.BytesCommitted);
Assert.Equal(0, writer.BytesPending);
Assert.Equal(0, writer.CurrentDepth);
+ Assert.Equal(null, writer.Options.Encoder);
Assert.Equal(formatted, writer.Options.Indented);
Assert.Equal(skipValidation, writer.Options.SkipValidation);
Assert.Equal(0, stream.Position);
Assert.Equal(0, writer.BytesCommitted);
Assert.Equal(0, writer.BytesPending);
Assert.Equal(0, writer.CurrentDepth);
+ Assert.Equal(null, writer.Options.Encoder);
Assert.Equal(formatted, writer.Options.Indented);
Assert.Equal(skipValidation, writer.Options.SkipValidation);
Assert.Equal(0, output.FormattedCount);
Assert.Equal(0, writeToStream.BytesCommitted);
Assert.Equal(0, writeToStream.BytesPending);
Assert.Equal(0, writeToStream.CurrentDepth);
+ Assert.Equal(null, writeToStream.Options.Encoder);
Assert.Equal(formatted, writeToStream.Options.Indented);
Assert.Equal(skipValidation, writeToStream.Options.SkipValidation);
Assert.True(stream.Position != 0);
Assert.Equal(0, writeToIBW.BytesCommitted);
Assert.Equal(0, writeToIBW.BytesPending);
Assert.Equal(0, writeToIBW.CurrentDepth);
+ Assert.Equal(null, writeToIBW.Options.Encoder);
Assert.Equal(formatted, writeToIBW.Options.Indented);
Assert.Equal(skipValidation, writeToIBW.Options.SkipValidation);
Assert.True(output.FormattedCount != 0);
Assert.Equal(0, writeToStream.BytesCommitted);
Assert.Equal(0, writeToStream.BytesPending);
Assert.Equal(0, writeToStream.CurrentDepth);
+ Assert.Equal(null, writeToStream.Options.Encoder);
Assert.Equal(formatted, writeToStream.Options.Indented);
Assert.Equal(skipValidation, writeToStream.Options.SkipValidation);
Assert.True(stream.Position != 0);
Assert.Equal(0, writeToIBW.BytesCommitted);
Assert.Equal(0, writeToIBW.BytesPending);
Assert.Equal(0, writeToIBW.CurrentDepth);
+ Assert.Equal(null, writeToIBW.Options.Encoder);
Assert.Equal(formatted, writeToIBW.Options.Indented);
Assert.Equal(skipValidation, writeToIBW.Options.SkipValidation);
Assert.True(output.FormattedCount != 0);
Assert.Equal(0, writeToStream.BytesCommitted);
Assert.Equal(0, writeToStream.BytesPending);
Assert.Equal(0, writeToStream.CurrentDepth);
+ Assert.Equal(null, writeToStream.Options.Encoder);
Assert.Equal(formatted, writeToStream.Options.Indented);
Assert.Equal(skipValidation, writeToStream.Options.SkipValidation);
Assert.True(stream.Position != 0);
Assert.Equal(0, writeToIBW.BytesCommitted);
Assert.Equal(0, writeToIBW.BytesPending);
Assert.Equal(0, writeToIBW.CurrentDepth);
+ Assert.Equal(null, writeToIBW.Options.Encoder);
Assert.Equal(formatted, writeToIBW.Options.Indented);
Assert.Equal(skipValidation, writeToIBW.Options.SkipValidation);
Assert.True(output.FormattedCount != 0);
}
}
- [Theory(Skip = "Update test to match JavaScriptEncoder semantics.")]
- [InlineData(true, true)]
- [InlineData(true, false)]
- [InlineData(false, true)]
- [InlineData(false, false)]
- public void InvalidUTF8(bool formatted, bool skipValidation)
- {
- var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation };
+ private const string InvalidUtf8 = "\"\\uFFFD(\"";
+ private const string ValidUtf8 = "\"\\u00F1\"";
- var output = new ArrayBufferWriter<byte>(1024);
- var jsonUtf8 = new Utf8JsonWriter(output, options);
+ // todo: verify that we should be using replacement char and not throwing here if skipValidation=true
+ [Theory]
+ [InlineData(true,
+ "{" + InvalidUtf8 + ":" + InvalidUtf8 + "}",
+ "{" + InvalidUtf8 + ":" + ValidUtf8 + "}",
+ "{" + ValidUtf8 + ":" + InvalidUtf8 + "}",
+ "{" + ValidUtf8 + ":" + ValidUtf8 + "}")]
+ [InlineData(false,
+ "{" + InvalidUtf8 + ":" + InvalidUtf8 + "}",
+ "{" + InvalidUtf8 + ":" + ValidUtf8 + "}",
+ "{" + ValidUtf8 + ":" + InvalidUtf8 + "}",
+ "{" + ValidUtf8 + ":" + ValidUtf8 + "}")]
+ public void UTF8ReplacementCharacters(bool skipValidation, string expected0, string expected1, string expected2, string expected3)
+ {
+ var options = new JsonWriterOptions { SkipValidation = skipValidation };
var validUtf8 = new byte[2] { 0xc3, 0xb1 }; // 0xF1
var invalidUtf8 = new byte[2] { 0xc3, 0x28 };
- jsonUtf8.WriteStartObject();
- for (int i = 0; i < 6; i++)
+ string WriteProperty(byte[] propertyName, byte[] value)
{
- switch (i)
- {
- case 0:
- Assert.Throws<ArgumentException>(() => jsonUtf8.WriteString(invalidUtf8, invalidUtf8));
- break;
- case 1:
- Assert.Throws<ArgumentException>(() => jsonUtf8.WriteString(invalidUtf8, validUtf8));
- break;
- case 2:
- Assert.Throws<ArgumentException>(() => jsonUtf8.WriteString(validUtf8, invalidUtf8));
- break;
- case 3:
- jsonUtf8.WriteString(validUtf8, validUtf8);
- break;
- case 4:
- Assert.Throws<ArgumentException>(() => jsonUtf8.WritePropertyName(invalidUtf8));
- break;
- case 5:
- jsonUtf8.WritePropertyName(validUtf8);
- Assert.Throws<ArgumentException>(() => jsonUtf8.WriteStringValue(invalidUtf8));
- jsonUtf8.WriteStringValue(validUtf8);
- break;
- case 6:
- jsonUtf8.WritePropertyName(validUtf8);
- jsonUtf8.WriteStringValue(validUtf8);
- break;
- }
+ var output = new ArrayBufferWriter<byte>(1024);
+ var jsonUtf8 = new Utf8JsonWriter(output, options);
+ jsonUtf8.WriteStartObject();
+ jsonUtf8.WriteString(propertyName, value);
+ jsonUtf8.WriteEndObject();
+ jsonUtf8.Flush();
+ string result = Encoding.UTF8.GetString(
+ output.WrittenSpan
+#if netfx
+ .ToArray()
+#endif
+ );
+ output.Clear();
+ return result;
}
- jsonUtf8.WriteEndObject();
- jsonUtf8.Flush();
+
+ string result;
+ result = WriteProperty(invalidUtf8, invalidUtf8);
+ Assert.Equal(expected0, result);
+
+ result = WriteProperty(invalidUtf8, validUtf8);
+ Assert.Equal(expected1, result);
+
+ result = WriteProperty(validUtf8, invalidUtf8);
+ Assert.Equal(expected2, result);
+
+ result = WriteProperty(validUtf8, validUtf8);
+ Assert.Equal(expected3, result);
}
[Theory]
jsonUtf8.Flush();
}
+ public static IEnumerable<object[]> JsonEncodedTextStringsCustomAll
+ {
+ get
+ {
+ return new List<object[]>
+ {
+ new object[] { "éééééêêêêê", "{\"Prop\":\"éééééêêêêê\"}" },
+ new object[] { "aѧѦa", "{\"Prop\":\"aѧѦa\"}" }, // U0467, U0466
+ };
+ }
+ }
+
+ [Theory]
+ [MemberData(nameof(JsonEncodedTextStringsCustomAll))]
+ public void CustomEscaper(string value, string expectedStr)
+ {
+ const string PropertyName = "Prop";
+
+ // Allow all unicode values (except forbidden characters which we don't have in test data here)
+ JavaScriptEncoder encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
+
+ var options = new JsonWriterOptions();
+ options.Encoder = encoder;
+
+ for (int i = 0; i < 6; i++)
+ {
+ var output = new ArrayBufferWriter<byte>(1024);
+ var jsonUtf8 = new Utf8JsonWriter(output, options);
+
+ jsonUtf8.WriteStartObject();
+
+ switch (i)
+ {
+ case 0:
+ jsonUtf8.WriteString(PropertyName, value);
+ break;
+ case 1:
+ jsonUtf8.WriteString(Encoding.UTF8.GetBytes(PropertyName), Encoding.UTF8.GetBytes(value));
+ break;
+ case 2:
+ jsonUtf8.WriteString(JsonEncodedText.Encode(PropertyName), JsonEncodedText.Encode(value, encoder));
+ break;
+ case 3:
+ jsonUtf8.WritePropertyName(PropertyName);
+ jsonUtf8.WriteStringValue(value);
+ break;
+ case 4:
+ jsonUtf8.WritePropertyName(Encoding.UTF8.GetBytes(PropertyName));
+ jsonUtf8.WriteStringValue(Encoding.UTF8.GetBytes(value));
+ break;
+ case 5:
+ jsonUtf8.WritePropertyName(JsonEncodedText.Encode(PropertyName, encoder));
+ jsonUtf8.WriteStringValue(JsonEncodedText.Encode(value, encoder));
+ break;
+ }
+
+ jsonUtf8.WriteEndObject();
+ jsonUtf8.Flush();
+
+ string result = Encoding.UTF8.GetString(
+ output.WrittenSpan
+#if netfx
+ .ToArray()
+#endif
+ );
+
+ Assert.Equal(expectedStr, result);
+ }
+ }
+
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
#endif
);
- // Temporary hack until we can use the same escape algorithm throughout.
+ // Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
}
{
var sb = new StringBuilder(json.Length);
int i = 0;
- while (i < json.Length - 1)
+ while (i < json.Length)
{
if (json[i] == '\\')
{