private void WriteStringEscapeProperty(ReadOnlySpan <byte> utf8PropertyName, Guid value, int firstEscapeIndexProp) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length); byte[] propertyArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span <byte> escapedPropertyName; if (length > StackallocThreshold) { propertyArray = ArrayPool <byte> .Shared.Rent(length); escapedPropertyName = propertyArray; } else { // Cannot create a span directly since it gets passed to instance methods on a ref struct. unsafe { byte *ptr = stackalloc byte[length]; escapedPropertyName = new Span <byte>(ptr, length); } } JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, out int written); WriteStringByOptions(escapedPropertyName.Slice(0, written), value); if (propertyArray != null) { ArrayPool <byte> .Shared.Return(propertyArray); } }
private static byte[] GetEscapedPropertyNameSection( ReadOnlySpan <byte> utf8Value, int firstEscapeIndexVal, JavaScriptEncoder?encoder) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < utf8Value.Length); byte[]? valueArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span <byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ? stackalloc byte[JsonConstants.StackallocByteThreshold] : (valueArray = ArrayPool <byte> .Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written); byte[] propertySection = GetPropertyNameSection(escapedValue.Slice(0, written)); if (valueArray != null) { ArrayPool <byte> .Shared.Return(valueArray); } return(propertySection); }
private void WriteStringEscapeValue(ReadOnlySpan <char> value, int firstEscapeIndexVal) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length); Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < value.Length); char[] valueArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span <char> escapedValue; if (length > StackallocThreshold) { valueArray = ArrayPool <char> .Shared.Rent(length); escapedValue = valueArray; } else { // Cannot create a span directly since it gets passed to instance methods on a ref struct. unsafe { char *ptr = stackalloc char[length]; escapedValue = new Span <char>(ptr, length); } } JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, out int written); WriteStringByOptions(escapedValue.Slice(0, written)); if (valueArray != null) { ArrayPool <char> .Shared.Return(valueArray); } }
private void WriteStringEscapeValueOnly(ReadOnlySpan <byte> escapedPropertyName, ReadOnlySpan <char> value, int firstEscapeIndex) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length); Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < value.Length); char[] valueArray = ArrayPool <char> .Shared.Rent(JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndex)); Span <char> escapedValue = valueArray; JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndex, out int written); WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written)); ArrayPool <char> .Shared.Return(valueArray); }
private void WriteStringEscapeProperty(ReadOnlySpan <byte> utf8PropertyName, Guid value, int firstEscapeIndexProp) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length); byte[]? propertyArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span <byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool <byte> .Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(escapedPropertyName.Slice(0, written), value); if (propertyArray != null) { ArrayPool <byte> .Shared.Return(propertyArray); } }
private void WriteNumberEscapeProperty(ReadOnlySpan <char> propertyName, ulong value, int firstEscapeIndexProp) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length); Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length); char[] propertyArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span <char> escapedPropertyName = length <= JsonConstants.StackallocThreshold ? stackalloc char[length] : (propertyArray = ArrayPool <char> .Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, out int written); WriteNumberByOptions(escapedPropertyName.Slice(0, written), value); if (propertyArray != null) { ArrayPool <char> .Shared.Return(propertyArray); } }
private void WriteStringEscapeValue(ReadOnlySpan <byte> utf8Value, int firstEscapeIndexVal) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < utf8Value.Length); byte[]? valueArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span <byte> escapedValue = length <= JsonConstants.StackallocThreshold ? stackalloc byte[length] : (valueArray = ArrayPool <byte> .Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written); WriteStringByOptions(escapedValue.Slice(0, written)); if (valueArray != null) { ArrayPool <byte> .Shared.Return(valueArray); } }
private void WriteBase64EscapeProperty(ReadOnlySpan <char> propertyName, ReadOnlySpan <byte> bytes, int firstEscapeIndexProp) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length); Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length); char[]? propertyArray = null; int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span <char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool <char> .Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); WriteBase64ByOptions(escapedPropertyName.Slice(0, written), bytes); if (propertyArray != null) { ArrayPool <char> .Shared.Return(propertyArray); } }
private void WriteStringEscapePropertyOrValue(ReadOnlySpan <byte> utf8PropertyName, ReadOnlySpan <char> value, int firstEscapeIndexProp, int firstEscapeIndexVal) { Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length); Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); char[] valueArray = null; byte[] propertyArray = null; if (firstEscapeIndexVal != -1) { int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span <char> escapedValue; if (length > JsonConstants.StackallocThreshold) { valueArray = ArrayPool <char> .Shared.Rent(length); escapedValue = valueArray; } else { // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { char *ptr = stackalloc char[length]; escapedValue = new Span <char>(ptr, length); } } JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, out int written); value = escapedValue.Slice(0, written); } if (firstEscapeIndexProp != -1) { int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span <byte> escapedPropertyName; if (length > JsonConstants.StackallocThreshold) { propertyArray = ArrayPool <byte> .Shared.Rent(length); escapedPropertyName = propertyArray; } else { // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { byte *ptr = stackalloc byte[length]; escapedPropertyName = new Span <byte>(ptr, length); } } JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, out int written); utf8PropertyName = escapedPropertyName.Slice(0, written); } WriteStringByOptions(utf8PropertyName, value); if (valueArray != null) { ArrayPool <char> .Shared.Return(valueArray); } if (propertyArray != null) { ArrayPool <byte> .Shared.Return(propertyArray); } }