private void DeterminePropertyName(JsonSerializerOptions options) { if (PropertyInfo != null) { JsonPropertyNameAttribute nameAttribute = GetAttribute <JsonPropertyNameAttribute>(); if (nameAttribute != null) { NameAsString = nameAttribute.Name; // This is detected and thrown by caller. if (NameAsString == null) { return; } } else if (options.PropertyNamingPolicy != null) { NameAsString = options.PropertyNamingPolicy.ConvertName(PropertyInfo.Name); // This is detected and thrown by caller. if (NameAsString == null) { return; } } else { NameAsString = PropertyInfo.Name; } // At this point propertyName is valid UTF16, so just call the simple UTF16->UTF8 encoder. _name = Encoding.UTF8.GetBytes(NameAsString); // Set the compare name. if (options.PropertyNameCaseInsensitive) { CompareNameAsString = NameAsString.ToUpperInvariant(); _compareName = Encoding.UTF8.GetBytes(CompareNameAsString); } else { CompareNameAsString = NameAsString; _compareName = _name; } // Cache the escaped name. int valueIdx = JsonWriterHelper.NeedsEscaping(_name); if (valueIdx == -1) { _escapedName = _name; } else { int length = JsonWriterHelper.GetMaxEscapedLength(_name.Length, valueIdx); byte[] tempArray = ArrayPool <byte> .Shared.Rent(length); JsonWriterHelper.EscapeString(_name, tempArray, valueIdx, out int written); _escapedName = new byte[written]; tempArray.CopyTo(_escapedName, 0); // We clear the array because it is "user data" (although a property name). new Span <byte>(tempArray, 0, written).Clear(); ArrayPool <byte> .Shared.Return(tempArray); } } }
private void DeterminePropertyName(JsonSerializerOptions options) { if (PropertyInfo != null) { JsonPropertyNameAttribute nameAttribute = GetAttribute <JsonPropertyNameAttribute>(); if (nameAttribute != null) { NameAsString = nameAttribute.Name; // null is not valid; JsonClassInfo throws an InvalidOperationException after this return. if (NameAsString == null) { return; } } else if (options.PropertyNamingPolicy != null) { NameAsString = options.PropertyNamingPolicy.ConvertName(PropertyInfo.Name); // null is not valid; JsonClassInfo throws an InvalidOperationException after this return. if (NameAsString == null) { return; } } else { NameAsString = PropertyInfo.Name; } // At this point propertyName is valid UTF16, so just call the simple UTF16->UTF8 encoder. _name = Encoding.UTF8.GetBytes(NameAsString); // Set the compare name. if (options.PropertyNameCaseInsensitive) { NameUsedToCompareAsString = NameAsString.ToUpperInvariant(); _nameUsedToCompare = Encoding.UTF8.GetBytes(NameUsedToCompareAsString); } else { NameUsedToCompareAsString = NameAsString; _nameUsedToCompare = _name; } // 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 } }