-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
JsonSerializer writing into Utf8JsonWriter #29205
Comments
Example of it being used to serialize values into a JSON array: var writer = new Utf8JsonWriter();
writer.WriteStartArray();
foreach (var item in Items)
{
JsonSerializer.Write(writer, item);
}
writer.WriteEndArray(); |
Related, but Utf8JsonReader dotnet/corefx#36717 |
We came across another need for this when writing a converter. You often want to re-enter the serializer to handle some value inside a converter. writer.WritePropertyName("Value");
JsonSerializer.Write(writer, value, option); public static partial class JsonSerializer
{
public static void Write(object value, Type type, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
public static void Write<TValue>(TValue value, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
} |
It might be easier to review both the read and write side together (https://github.com/dotnet/corefx/issues/36714 / https://github.com/dotnet/corefx/issues/36717), and also in the context of existing APIs: API Proposal: public static partial class JsonSerializer
{
// Where we have a disagreement between the options used to create the reader/writer and the
// JsonSerializerOptions that are passed in, we will honor the options on the reader/writer.
// ADD:
public static object ReadValue(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions options = null) { throw null; }
public static TValue ReadValue<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions options = null) { throw null; }
public static void WriteAsValue(object value, Type type, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
public static void WriteAsValue<TValue>(TValue value, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
public static void WriteAsProperty(ReadOnlySpan<byte> utf8PropertyName, object value, Type type, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
public static void WriteAsProperty<TValue>(ReadOnlySpan<char> propertyName, TValue value, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
public static void WriteAsProperty<TValue>(string propertyName, TValue value, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
public static void WriteAsProperty<TValue>(JsonEncodedText propertyName, TValue value, Utf8JsonWriter writer, JsonSerializerOptions options = null) { throw null; }
// EXISTING:
public static object Parse(ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerOptions options = null) { throw null; }
public static object Parse(string json, Type returnType, JsonSerializerOptions options = null) { throw null; }
public static TValue Parse<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerializerOptions options = null) { throw null; }
public static TValue Parse<TValue>(string json,JsonSerializerOptions options = null) { throw null; }
public static ValueTask<object> ReadAsync(Stream utf8Json, Type returnType, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw null; }
public static ValueTask<TValue> ReadAsync<TValue>(Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw null; }
public static byte[] ToUtf8Bytes(object value, Type type, JsonSerializerOptions options = null) { throw null; }
public static byte[] ToUtf8Bytes<TValue>(TValue value, JsonSerializerOptions options = null) { throw null; }
public static string ToString(object value, Type type, JsonSerializerOptions options = null) { throw null; }
public static string ToString<TValue>(TValue value, JsonSerializerOptions options = null) { throw null; }
public static Task WriteAsync(object value, Type type, Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw null; }
public static Task WriteAsync<TValue>(TValue value, Stream utf8Json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { throw null; }
} Questions:
Existing APIs that take public sealed partial class JsonDocument : IDisposable
{
public static JsonDocument ParseValue(ref Utf8JsonReader reader) { throw null; }
public static bool TryParseValue(ref Utf8JsonReader reader, out JsonDocument document) { throw null; }
}
public readonly partial struct JsonElement
{
public void WriteAsProperty(ReadOnlySpan<byte> utf8PropertyName, Utf8JsonWriter writer) { }
public void WriteAsProperty(ReadOnlySpan<char> propertyName, Utf8JsonWriter writer) { }
public void WriteAsProperty(string propertyName, Utf8JsonWriter writer) { }
public void WriteAsValue(Utf8JsonWriter writer) { }
// ADD:
public void WriteAsProperty(JsonEncodedText propertyName, Utf8JsonWriter writer) { }
} Existing APIs taken from the ref, for reference: |
Originally I thought this was fine - the object being written is "more pertinent" than the stream so it should be first. However I see value in having the stream first considering if we ever expect this to be an extension method on
I prefer Just
Assuming the writer will have a pulib API added to write just the property name, at this point I would say we just have |
I am working under the assumption we don't have the
|
|
Here is the perf difference from before and after, looks good!
|
Currently if you want to deserialize an object into a
Utf8JsonWriter
you need to first parse the object with the serializer, then create a temporaryJsonDocument
, then write theJsonElement
into theUtf8JsonWriter
.There should be a way to do this directly from the serializer and avoid the temporary
JsonDocument
.The text was updated successfully, but these errors were encountered: