writer.WriteBooleanValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, bool value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, bool value, Utf8JsonWriter writer)
{
- writer.WriteBoolean(escapedPropertyName, value);
+ writer.WriteBoolean(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, byte value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, byte value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
);
}
- public override void Write(Span<byte> escapedPropertyName, char value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, char value, Utf8JsonWriter writer)
{
- writer.WriteString(escapedPropertyName,
+ writer.WriteString(propertyName,
#if BUILDING_INBOX_LIBRARY
MemoryMarshal.CreateSpan(ref value, 1)
#else
writer.WriteStringValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, DateTime value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, DateTime value, Utf8JsonWriter writer)
{
- writer.WriteString(escapedPropertyName, value);
+ writer.WriteString(propertyName, value);
}
}
}
writer.WriteStringValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, DateTimeOffset value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, DateTimeOffset value, Utf8JsonWriter writer)
{
- writer.WriteString(escapedPropertyName, value);
+ writer.WriteString(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, decimal value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, decimal value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, double value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, double value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
}
}
- public override void Write(Span<byte> escapedPropertyName, TValue value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, TValue value, Utf8JsonWriter writer)
{
if (TreatAsString)
{
- writer.WriteString(escapedPropertyName, value.ToString());
+ writer.WriteString(propertyName, value.ToString());
}
else if (s_isUint64)
{
// Use the ulong converter to prevent conversion into a signed\long value.
ulong ulongValue = Convert.ToUInt64(value);
- writer.WriteNumber(escapedPropertyName, ulongValue);
+ writer.WriteNumber(propertyName, ulongValue);
}
else
{
// long can hold the signed\unsigned values of other integer types.
long longValue = Convert.ToInt64(value);
- writer.WriteNumber(escapedPropertyName, longValue);
+ writer.WriteNumber(propertyName, longValue);
}
}
}
writer.WriteStringValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, Guid value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, Guid value, Utf8JsonWriter writer)
{
- writer.WriteString(escapedPropertyName, value);
+ writer.WriteString(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, short value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, short value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, int value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, int value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, long value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, long value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
value.WriteAsValue(writer);
}
- public override void Write(Span<byte> escapedPropertyName, JsonElement value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, JsonElement value, Utf8JsonWriter writer)
{
- value.WriteAsProperty(escapedPropertyName, writer);
+ value.WriteAsProperty(propertyName.ToString(), writer);
}
}
}
throw new InvalidOperationException();
}
- public override void Write(Span<byte> escapedPropertyName, object value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, object value, Utf8JsonWriter writer)
{
throw new InvalidOperationException();
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, sbyte value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, sbyte value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, float value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, float value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteStringValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, string value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, string value, Utf8JsonWriter writer)
{
- writer.WriteString(escapedPropertyName, value);
+ writer.WriteString(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, ushort value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, ushort value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, uint value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, uint value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
writer.WriteNumberValue(value);
}
- public override void Write(Span<byte> escapedPropertyName, ulong value, Utf8JsonWriter writer)
+ public override void Write(JsonEncodedText propertyName, ulong value, Utf8JsonWriter writer)
{
- writer.WriteNumber(escapedPropertyName, value);
+ writer.WriteNumber(propertyName, value);
}
}
}
public string NameUsedToCompareAsString { get; private set; }
// The escaped name passed to the writer.
- public byte[] EscapedName { get; private set; }
+ public JsonEncodedText? EscapedName { get; private set; }
public bool HasGetter { get; set; }
public bool HasSetter { get; set; }
}
// Cache the escaped name.
-#if true
- // temporary behavior until the writer can accept escaped string.
- EscapedName = Name;
-#else
- int valueIdx = JsonWriterHelper.NeedsEscaping(_name);
- if (valueIdx == -1)
- {
- _escapedName = _name;
- }
- else
- {
- byte[] pooledName = null;
- int length = JsonWriterHelper.GetMaxEscapedLength(_name.Length, valueIdx);
-
- Span<byte> escapedName = length <= JsonConstants.StackallocThreshold ?
- stackalloc byte[length] :
- (pooledName = ArrayPool<byte>.Shared.Rent(length));
-
- JsonWriterHelper.EscapeString(_name, escapedName, 0, out int written);
-
- _escapedName = escapedName.Slice(0, written).ToArray();
-
- if (pooledName != null)
- {
- // We clear the array because it is "user data" (although a property name).
- new Span<byte>(pooledName, 0, written).Clear();
- ArrayPool<byte>.Shared.Return(pooledName);
- }
- }
-#endif
+ EscapedName = JsonEncodedText.Encode(Name);
}
private void DetermineSerializationCapabilities()
if (value == null)
{
- Debug.Assert(EscapedName != null);
+ Debug.Assert(EscapedName.HasValue);
if (!IgnoreNullValues)
{
- writer.WriteNull(EscapedName);
+ writer.WriteNull(EscapedName.Value);
}
}
else if (ValueConverter != null)
{
- if (EscapedName != null)
+ if (EscapedName.HasValue)
{
- ValueConverter.Write(EscapedName, value, writer);
+ ValueConverter.Write(EscapedName.Value, value, writer);
}
else
{
if (value == null)
{
- Debug.Assert(EscapedName != null);
+ Debug.Assert(EscapedName.HasValue);
if (!IgnoreNullValues)
{
- writer.WriteNull(EscapedName);
+ writer.WriteNull(EscapedName.Value);
}
}
else if (ValueConverter != null)
{
- if (EscapedName != null)
+ if (EscapedName.HasValue)
{
- ValueConverter.Write(EscapedName, value.GetValueOrDefault(), writer);
+ ValueConverter.Write(EscapedName.Value, value.GetValueOrDefault(), writer);
}
else
{
// 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;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
}
else
{
- byte[] utf8Key = Encoding.UTF8.GetBytes(key);
-#if true
- // temporary behavior until the writer can accept escaped string.
- converter.Write(utf8Key, value, writer);
-#else
- int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Key);
- if (valueIdx == -1)
- {
- converter.Write(utf8Key, value, writer);
- }
- else
- {
- byte[] pooledKey = null;
- int length = JsonWriterHelper.GetMaxEscapedLength(utf8Key.Length, valueIdx);
-
- Span<byte> escapedKey = length <= JsonConstants.StackallocThreshold ?
- stackalloc byte[length] :
- (pooledKey = ArrayPool<byte>.Shared.Rent(length));
-
- JsonWriterHelper.EscapeString(utf8Key, escapedKey, valueIdx, out int written);
-
- converter.Write(escapedKey.Slice(0, written), value, writer);
-
- if (pooledKey != null)
- {
- // We clear the array because it is "user data" (although a property name).
- new Span<byte>(pooledKey, 0, written).Clear();
- ArrayPool<byte>.Shared.Return(pooledKey);
- }
- }
-#endif
+ JsonEncodedText escapedKey = JsonEncodedText.Encode(key);
+ converter.Write(escapedKey, value, writer);
}
}
{
if (!jsonPropertyInfo.IgnoreNullValues)
{
- writer.WriteNull(jsonPropertyInfo.EscapedName);
+ writer.WriteNull(jsonPropertyInfo.EscapedName.Value);
}
state.Current.NextProperty();
{
public abstract bool TryRead(Type valueType, ref Utf8JsonReader reader, out TValue value);
public abstract void Write(TValue value, Utf8JsonWriter writer);
- public abstract void Write(Span<byte> escapedPropertyName, TValue value, Utf8JsonWriter writer);
+ public abstract void Write(JsonEncodedText propertyName, TValue value, Utf8JsonWriter writer);
}
}
public void WriteObjectOrArrayStart(ClassType classType, Utf8JsonWriter writer, bool writeNull = false)
{
- if (JsonPropertyInfo?.EscapedName != null)
+ if (JsonPropertyInfo?.EscapedName.HasValue == true)
{
- WriteObjectOrArrayStart(classType, JsonPropertyInfo?.EscapedName, writer, writeNull);
+ WriteObjectOrArrayStart(classType, JsonPropertyInfo.EscapedName.Value, writer, writeNull);
}
else if (KeyName != null)
{
- byte[] pooledKey = null;
- byte[] utf8Key = Encoding.UTF8.GetBytes(KeyName);
- int length = JsonWriterHelper.GetMaxEscapedLength(utf8Key.Length, 0);
-
- Span<byte> escapedKey = length <= JsonConstants.StackallocThreshold ?
- stackalloc byte[length] :
- (pooledKey = ArrayPool<byte>.Shared.Rent(length));
-
- JsonWriterHelper.EscapeString(utf8Key, escapedKey, 0, out int written);
- Span<byte> propertyName = escapedKey.Slice(0, written);
-
+ JsonEncodedText propertyName = JsonEncodedText.Encode(KeyName);
WriteObjectOrArrayStart(classType, propertyName, writer, writeNull);
-
- if (pooledKey != null)
- {
- ArrayPool<byte>.Shared.Return(pooledKey);
- }
}
else
{
}
}
- private void WriteObjectOrArrayStart(ClassType classType, ReadOnlySpan<byte> propertyName, Utf8JsonWriter writer, bool writeNull)
+ private void WriteObjectOrArrayStart(ClassType classType, JsonEncodedText propertyName, Utf8JsonWriter writer, bool writeNull)
{
if (writeNull)
{