-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add a JsonEncodedText.Value property #69716
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationThe public class MyConverter : JsonConverter<Foo>
{
private readonly JsonEncodedText _preEncodedPropertyName = JsonEncodedText.Encode("propertyName");
public override void Write(Utf8JsonWriter writer, Foo value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(_propertyName.Utf8EncodedBytes, value.Value);
writer.WriteEndObject();
}
public override Foo Read(ref Utf8JsonReader reader, Type ttc, JsonSerializerOptions options)
{
reader.Read();
reader.Read();
Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
string propertyName = reader.ReadPropertyName();
if (_preEncodedPropertyName.ToString() == propertyName) // there's no proper way to fetch the UTF-16 variant when serializing
{
/* read the property value */
}
}
} API Proposalnamespace System.Text.Json;
public struct JsonEncodedValue
{
public ReadOnlySpan<byte> EncodedUtf8Bytes => _utf8Value;
+ public string Value => _value ?? string.Empty;
} API UsageFrom the previous example: public override Foo Read(ref Utf8JsonReader reader, Type ttc, JsonSerializerOptions options)
{
reader.Read();
reader.Read();
Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
string propertyName = reader.ReadPropertyName();
if (_preEncodedPropertyName.Value == propertyName) // there's no proper way to fetch the UTF-16 variant when serializing
{
/* read the property value */
}
}
### Alternative Designs
_No response_
### Risks
_No response_
<table>
<tr>
<th align="left">Author:</th>
<td>eiriktsarpalis</td>
</tr>
<tr>
<th align="left">Assignees:</th>
<td>-</td>
</tr>
<tr>
<th align="left">Labels:</th>
<td>
`api-suggestion`, `area-System.Text.Json`
</td>
</tr>
<tr>
<th align="left">Milestone:</th>
<td>-</td>
</tr>
</table>
</details> |
namespace System.Text.Json;
public partial struct JsonEncodedText
{
public string Value { get; }
} |
Is this one up for grabs @eiriktsarpalis? If so I'd like to take a stab at it :) |
Yep, go for it :-) |
Background and motivation
The
JsonEncodedText
struct encapsulates both the UTF8 and string representations of the encoded value, however only theUTF8
value is accessible via theEncodedUtf8Bytes
property. This makes the type more difficult to use in deserialization scenaria, for instance:API Proposal
namespace System.Text.Json; public struct JsonEncodedValue { public ReadOnlySpan<byte> EncodedUtf8Bytes => _utf8Value; + public string Value => _value ?? string.Empty; }
API Usage
From the previous example:
Alternative Designs
We might expose the property as
ReadOnlySpan<char>
, but there's currently no reason why we would want to do this; the current implementation will always create a string for the UTF-16 representation.Risks
No response
The text was updated successfully, but these errors were encountered: