| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a single property for a JSON object. |
| | | 11 | | /// </summary> |
| | | 12 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 13 | | public readonly struct JsonProperty |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// The value of this property. |
| | | 17 | | /// </summary> |
| | 0 | 18 | | public JsonElement Value { get; } |
| | | 19 | | |
| | | 20 | | internal JsonProperty(JsonElement value) |
| | 0 | 21 | | { |
| | 0 | 22 | | Value = value; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The name of this property. |
| | | 27 | | /// This allocates a new string instance for each call. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public string Name => Value.GetPropertyName(); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Compares <paramref name="text" /> to the name of this property. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="text">The text to compare against.</param> |
| | | 35 | | /// <returns> |
| | | 36 | | /// <see langword="true" /> if the name of this property matches <paramref name="text"/>, |
| | | 37 | | /// <see langword="false" /> otherwise. |
| | | 38 | | /// </returns> |
| | | 39 | | /// <exception cref="InvalidOperationException"> |
| | | 40 | | /// This value's <see cref="Type"/> is not <see cref="JsonTokenType.PropertyName"/>. |
| | | 41 | | /// </exception> |
| | | 42 | | /// <remarks> |
| | | 43 | | /// This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and |
| | | 44 | | /// <see cref="Name" />, but can avoid creating the string instance. |
| | | 45 | | /// </remarks> |
| | | 46 | | public bool NameEquals(string? text) |
| | 0 | 47 | | { |
| | 0 | 48 | | return NameEquals(text.AsSpan()); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Compares the text represented by <paramref name="utf8Text" /> to the name of this property. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="utf8Text">The UTF-8 encoded text to compare against.</param> |
| | | 55 | | /// <returns> |
| | | 56 | | /// <see langword="true" /> if the name of this property has the same UTF-8 encoding as |
| | | 57 | | /// <paramref name="utf8Text" />, <see langword="false" /> otherwise. |
| | | 58 | | /// </returns> |
| | | 59 | | /// <exception cref="InvalidOperationException"> |
| | | 60 | | /// This value's <see cref="Type"/> is not <see cref="JsonTokenType.PropertyName"/>. |
| | | 61 | | /// </exception> |
| | | 62 | | /// <remarks> |
| | | 63 | | /// This method is functionally equal to doing an ordinal comparison of <paramref name="utf8Text" /> and |
| | | 64 | | /// <see cref="Name" />, but can avoid creating the string instance. |
| | | 65 | | /// </remarks> |
| | | 66 | | public bool NameEquals(ReadOnlySpan<byte> utf8Text) |
| | 0 | 67 | | { |
| | 0 | 68 | | return Value.TextEqualsHelper(utf8Text, isPropertyName: true, shouldUnescape: true); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Compares <paramref name="text" /> to the name of this property. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="text">The text to compare against.</param> |
| | | 75 | | /// <returns> |
| | | 76 | | /// <see langword="true" /> if the name of this property matches <paramref name="text"/>, |
| | | 77 | | /// <see langword="false" /> otherwise. |
| | | 78 | | /// </returns> |
| | | 79 | | /// <exception cref="InvalidOperationException"> |
| | | 80 | | /// This value's <see cref="Type"/> is not <see cref="JsonTokenType.PropertyName"/>. |
| | | 81 | | /// </exception> |
| | | 82 | | /// <remarks> |
| | | 83 | | /// This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and |
| | | 84 | | /// <see cref="Name" />, but can avoid creating the string instance. |
| | | 85 | | /// </remarks> |
| | | 86 | | public bool NameEquals(ReadOnlySpan<char> text) |
| | 0 | 87 | | { |
| | 0 | 88 | | return Value.TextEqualsHelper(text, isPropertyName: true); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | internal bool EscapedNameEquals(ReadOnlySpan<byte> utf8Text) |
| | 0 | 92 | | { |
| | 0 | 93 | | return Value.TextEqualsHelper(utf8Text, isPropertyName: true, shouldUnescape: false); |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | internal bool NameIsEscaped => Value.ValueIsEscapedHelper(isPropertyName: true); |
| | 0 | 97 | | internal ReadOnlySpan<byte> NameSpan => Value.GetPropertyNameRaw(); |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Write the property into the provided writer as a named JSON object property. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="writer">The writer.</param> |
| | | 103 | | /// <exception cref="ArgumentNullException"> |
| | | 104 | | /// The <paramref name="writer"/> parameter is <see langword="null"/>. |
| | | 105 | | /// </exception> |
| | | 106 | | /// <exception cref="ArgumentException"> |
| | | 107 | | /// This <see cref="Name"/>'s length is too large to be a JSON object property. |
| | | 108 | | /// </exception> |
| | | 109 | | /// <exception cref="InvalidOperationException"> |
| | | 110 | | /// This <see cref="Value"/>'s <see cref="JsonElement.ValueKind"/> would result in an invalid JSON. |
| | | 111 | | /// </exception> |
| | | 112 | | /// <exception cref="ObjectDisposedException"> |
| | | 113 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 114 | | /// </exception>> |
| | | 115 | | public void WriteTo(Utf8JsonWriter writer) |
| | 0 | 116 | | { |
| | 0 | 117 | | ArgumentNullException.ThrowIfNull(writer); |
| | | 118 | | |
| | 0 | 119 | | Value.WritePropertyNameTo(writer); |
| | 0 | 120 | | Value.WriteTo(writer); |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Provides a <see cref="string"/> representation of the property for |
| | | 125 | | /// debugging purposes. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <returns> |
| | | 128 | | /// A string containing the un-interpreted value of the property, beginning |
| | | 129 | | /// at the declaring open-quote and ending at the last character that is part of |
| | | 130 | | /// the value. |
| | | 131 | | /// </returns> |
| | | 132 | | public override string ToString() |
| | 0 | 133 | | { |
| | 0 | 134 | | return Value.GetPropertyRawText(); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | | 138 | | private string DebuggerDisplay |
| | 0 | 139 | | => Value.ValueKind == JsonValueKind.Undefined ? "<Undefined>" : $"\"{ToString()}\""; |
| | | 140 | | } |
| | | 141 | | } |