Skip to content

Commit

Permalink
共享Utf8JsonWriterCache
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Jun 20, 2024
1 parent fbe7cb7 commit b9a0fc9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 46 deletions.
39 changes: 2 additions & 37 deletions WebApiClientCore/Serialization/JsonBufferSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

Expand All @@ -10,9 +9,6 @@ namespace WebApiClientCore.Serialization
/// </summary>
public static class JsonBufferSerializer
{
[ThreadStatic]
private static Utf8JsonWriter? bufferWriterUtf8JsonWriter;

/// <summary>
/// 默认选项
/// </summary>
Expand All @@ -34,39 +30,8 @@ public static void Serialize(IBufferWriter<byte> bufferWriter, object? obj, Json
}

options ??= defaultOptions;
var utf8JsonWriter = bufferWriterUtf8JsonWriter;
if (utf8JsonWriter == null)
{
utf8JsonWriter = new Utf8JsonWriter(bufferWriter, GetJsonWriterOptions(options));
bufferWriterUtf8JsonWriter = utf8JsonWriter;
}
else if (OptionsEquals(utf8JsonWriter.Options, options))
{
utf8JsonWriter.Reset(bufferWriter);
}
else
{
utf8JsonWriter.Dispose();
utf8JsonWriter = new Utf8JsonWriter(bufferWriter, GetJsonWriterOptions(options));
bufferWriterUtf8JsonWriter = utf8JsonWriter;
}

var utf8JsonWriter = Utf8JsonWriterCache.Get(bufferWriter, options);
JsonSerializer.Serialize(utf8JsonWriter, obj, obj.GetType(), options);
}

private static bool OptionsEquals(JsonWriterOptions options1, JsonSerializerOptions options2)
{
return options1.Encoder == options2.Encoder && options1.Indented == options2.WriteIndented;
}

private static JsonWriterOptions GetJsonWriterOptions(JsonSerializerOptions options)
{
return new JsonWriterOptions
{
Encoder = options.Encoder,
Indented = options.WriteIndented,
SkipValidation = true,
};
}
}
}
12 changes: 3 additions & 9 deletions WebApiClientCore/Serialization/KeyValueSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,10 @@ public static IList<KeyValue> Serialize(string key, object? obj, KeyValueSeriali
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
private static List<KeyValue> GetKeyValueList(string key, object obj, Type objType, KeyValueSerializerOptions options)
{
var jsonOptions = options.GetJsonSerializerOptions();
using var bufferWriter = new RecyclableBufferWriter<byte>();
using var utf8JsonWriter = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions
{
Indented = false,
SkipValidation = true,
Encoder = jsonOptions.Encoder
});

System.Text.Json.JsonSerializer.Serialize(utf8JsonWriter, obj, objType, jsonOptions);
var jsonOptions = options.GetJsonSerializerOptions();
var utf8JsonWriter = Utf8JsonWriterCache.Get(bufferWriter, jsonOptions);
JsonSerializer.Serialize(utf8JsonWriter, obj, objType, jsonOptions);
var utf8JsonReader = new Utf8JsonReader(bufferWriter.WrittenSpan, new JsonReaderOptions
{
MaxDepth = jsonOptions.MaxDepth,
Expand Down
58 changes: 58 additions & 0 deletions WebApiClientCore/Serialization/Utf8JsonWriterCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Buffers;
using System.Text.Json;

namespace WebApiClientCore.Serialization
{
/// <summary>
/// Utf8JsonWriter缓存
/// </summary>
static class Utf8JsonWriterCache
{
[ThreadStatic]
private static Utf8JsonWriter? bufferWriterUtf8JsonWriter;

/// <summary>
/// 获取与 bufferWriter 关联的 Utf8JsonWriter
/// </summary>
/// <param name="bufferWriter"></param>
/// <param name="options"></param>
/// <returns></returns>
public static Utf8JsonWriter Get(IBufferWriter<byte> bufferWriter, JsonSerializerOptions options)
{
var utf8JsonWriter = bufferWriterUtf8JsonWriter;
if (utf8JsonWriter == null)
{
utf8JsonWriter = new Utf8JsonWriter(bufferWriter, GetJsonWriterOptions(options));
bufferWriterUtf8JsonWriter = utf8JsonWriter;
}
else if (OptionsEquals(utf8JsonWriter.Options, options))
{
utf8JsonWriter.Reset(bufferWriter);
}
else
{
utf8JsonWriter.Dispose();
utf8JsonWriter = new Utf8JsonWriter(bufferWriter, GetJsonWriterOptions(options));
bufferWriterUtf8JsonWriter = utf8JsonWriter;
}
return utf8JsonWriter;
}


private static bool OptionsEquals(JsonWriterOptions options1, JsonSerializerOptions options2)
{
return options1.Encoder == options2.Encoder && options1.Indented == options2.WriteIndented;
}

private static JsonWriterOptions GetJsonWriterOptions(JsonSerializerOptions options)
{
return new JsonWriterOptions
{
Encoder = options.Encoder,
Indented = options.WriteIndented,
SkipValidation = true,
};
}
}
}

0 comments on commit b9a0fc9

Please sign in to comment.