-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from MartinYl/dev
扩展EasyCaching.Serialization.System.Text.Json,使用System.Text.Json进行序列化和反序列化json
- Loading branch information
Showing
9 changed files
with
429 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...syCaching.Serialization.SystemTextJson/Configurations/EasyCachingJsonSerializerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EasyCaching.Serialization.SystemTextJson.Configurations | ||
{ | ||
public class EasyCachingJsonSerializerOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the encoder to use when escaping strings, or null to use the default | ||
/// encoder. | ||
/// </summary> | ||
public JavaScriptEncoder Encoder => JavaScriptEncoder.UnsafeRelaxedJsonEscaping; | ||
/// <summary> | ||
/// Gets or sets a value that defines how comments are handled during deserialization. | ||
/// </summary> | ||
public JsonCommentHandling ReadCommentHandling => JsonCommentHandling.Disallow; | ||
/// <summary> | ||
/// Gets or sets a value that specifies the policy used to convert a property's name | ||
/// on an object to another format, such as camel-casing, or null to leave property | ||
/// names unchanged. | ||
/// </summary> | ||
public JsonNamingPolicy PropertyNamingPolicy => null; | ||
/// <summary> | ||
/// Gets or sets a value that determines whether a property's name uses a case-insensitive | ||
/// comparison during deserialization. The default value is false. | ||
/// </summary> | ||
public bool PropertyNameCaseInsensitive => false; | ||
/// <summary> | ||
/// Specifies how number types should be handled when serializing or deserializing. | ||
/// </summary> | ||
public JsonNumberHandling NumberHandling => JsonNumberHandling.Strict; | ||
/// <summary> | ||
/// Gets or sets the maximum depth allowed when serializing or deserializing JSON, | ||
/// with the default value of 0 indicating a maximum depth of 64. | ||
/// </summary> | ||
public int MaxDepth => 0; | ||
/// <summary> | ||
/// Determines whether fields are handled serialization and deserialization. The | ||
/// default value is false. | ||
/// </summary> | ||
public bool IncludeFields => false; | ||
/// <summary> | ||
/// Gets a value that determines whether read-only properties are ignored during | ||
/// serialization. The default value is false. | ||
/// </summary> | ||
public bool IgnoreReadOnlyProperties => false; | ||
/// <summary> | ||
/// Determines whether read-only fields are ignored during serialization. A property | ||
/// is read-only if it isn't marked with the readonly keyword. The default value is false. | ||
/// </summary> | ||
public bool IgnoreReadOnlyFields => false; | ||
/// <summary> | ||
/// Gets or sets a value that determines whether null values are ignored during serialization | ||
/// and deserialization. The default value is false. | ||
/// </summary> | ||
public bool IgnoreNullValues = false; | ||
/// <summary> | ||
/// Gets or sets the policy used to convert a System.Collections.IDictionary key's | ||
/// name to another format, such as camel-casing. | ||
/// </summary> | ||
public JsonNamingPolicy DictionaryKeyPolicy => null; | ||
/// <summary> | ||
/// Specifies a condition to determine when properties with default values are ignored | ||
/// during serialization or deserialization. The default value is System.Text.Json.Serialization.JsonIgnoreCondition.Never. | ||
/// </summary> | ||
public JsonIgnoreCondition DefaultIgnoreCondition => JsonIgnoreCondition.Never; | ||
/// <summary> | ||
/// Gets or sets the default buffer size, in bytes, to use when creating temporary buffers. | ||
/// </summary> | ||
public int DefaultBufferSize => 16 * 1024; | ||
/// <summary> | ||
/// Gets the list of user-defined converters that were registered. | ||
/// </summary> | ||
public IList<JsonConverter> Converters => new List<JsonConverter>(); | ||
/// <summary> | ||
/// Get or sets a value that indicates whether an extra comma at the end of a list | ||
/// of JSON values in an object or array is allowed (and ignored) within the JSON | ||
/// payload being deserialized. | ||
/// </summary> | ||
public bool AllowTrailingCommas => false; | ||
/// <summary> | ||
/// Configures how object references are handled when reading and writing JSON. | ||
/// </summary> | ||
public ReferenceHandler ReferenceHandler => null; | ||
/// <summary> | ||
/// Gets or sets a value that defines whether JSON should use pretty printing. By | ||
/// default, JSON is serialized without any extra white space. | ||
/// </summary> | ||
public bool WriteIndented => false; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...n/EasyCaching.Serialization.SystemTextJson/Configurations/EasyCachingOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using EasyCaching.Core.Configurations; | ||
using System; | ||
using System.Linq; | ||
using System.Text.Json; | ||
|
||
namespace EasyCaching.Serialization.SystemTextJson.Configurations | ||
{ | ||
|
||
/// <summary> | ||
/// EasyCaching options extensions. | ||
/// </summary> | ||
public static class EasyCachingOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Withs the json serializer. | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="name">The name of this serializer instance.</param> | ||
public static EasyCachingOptions WithSystemTextJson(this EasyCachingOptions options, string name = "json") => options.WithSystemTextJson(configure: x => { }, name); | ||
|
||
/// <summary> | ||
/// Withs the json serializer. | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="configure">Configure serializer settings.</param> | ||
/// <param name="name">The name of this serializer instance.</param> | ||
public static EasyCachingOptions WithSystemTextJson(this EasyCachingOptions options, Action<EasyCachingJsonSerializerOptions> configure, string name) | ||
{ | ||
var easyCachingJsonSerializerOptions = new EasyCachingJsonSerializerOptions(); | ||
|
||
configure(easyCachingJsonSerializerOptions); | ||
|
||
void jsonSerializerSettings(JsonSerializerOptions x) | ||
{ | ||
x.MaxDepth = easyCachingJsonSerializerOptions.MaxDepth; | ||
x.AllowTrailingCommas = easyCachingJsonSerializerOptions.AllowTrailingCommas; | ||
x.Converters.Union(easyCachingJsonSerializerOptions.Converters); | ||
x.DefaultBufferSize = easyCachingJsonSerializerOptions.DefaultBufferSize; | ||
x.DefaultIgnoreCondition = easyCachingJsonSerializerOptions.DefaultIgnoreCondition; | ||
x.DictionaryKeyPolicy = easyCachingJsonSerializerOptions.DictionaryKeyPolicy; | ||
x.Encoder = easyCachingJsonSerializerOptions.Encoder; | ||
x.IgnoreReadOnlyFields = easyCachingJsonSerializerOptions.IgnoreReadOnlyFields; | ||
x.IgnoreReadOnlyProperties = easyCachingJsonSerializerOptions.IgnoreReadOnlyProperties; | ||
x.IncludeFields = easyCachingJsonSerializerOptions.IncludeFields; | ||
x.NumberHandling = easyCachingJsonSerializerOptions.NumberHandling; | ||
x.PropertyNameCaseInsensitive = easyCachingJsonSerializerOptions.PropertyNameCaseInsensitive; | ||
x.PropertyNamingPolicy = easyCachingJsonSerializerOptions.PropertyNamingPolicy; | ||
x.ReadCommentHandling = easyCachingJsonSerializerOptions.ReadCommentHandling; | ||
x.ReferenceHandler = easyCachingJsonSerializerOptions.ReferenceHandler; | ||
x.WriteIndented = easyCachingJsonSerializerOptions.WriteIndented; | ||
} | ||
|
||
options.RegisterExtension(new JsonOptionsExtension(name, jsonSerializerSettings)); | ||
|
||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Withs the json serializer. | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="jsonSerializerSettingsConfigure">Configure serializer settings.</param> | ||
/// <param name="name">The name of this serializer instance.</param> | ||
public static EasyCachingOptions WithSystemTextJson(this EasyCachingOptions options, Action<JsonSerializerOptions> jsonSerializerSettingsConfigure, string name) | ||
{ | ||
options.RegisterExtension(new JsonOptionsExtension(name, jsonSerializerSettingsConfigure)); | ||
|
||
return options; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...alization/EasyCaching.Serialization.SystemTextJson/Configurations/JsonOptionsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using EasyCaching.Core.Configurations; | ||
using EasyCaching.Core.Serialization; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Text.Json; | ||
|
||
namespace EasyCaching.Serialization.SystemTextJson | ||
{ | ||
/// <summary> | ||
/// Json options extension. | ||
/// </summary> | ||
internal sealed class JsonOptionsExtension : IEasyCachingOptionsExtension | ||
{ | ||
/// <summary> | ||
/// The name. | ||
/// </summary> | ||
private readonly string _name; | ||
|
||
/// <summary> | ||
/// The configure. | ||
/// </summary> | ||
private readonly Action<JsonSerializerOptions> _configure; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:EasyCaching.Serialization.SystemTextJson.JsonOptionsExtension"/> class. | ||
/// </summary> | ||
/// <param name="name">Name.</param> | ||
/// <param name="configure">Configure.</param> | ||
public JsonOptionsExtension(string name, Action<JsonSerializerOptions> configure) | ||
{ | ||
_name = name; | ||
_configure = configure; | ||
} | ||
|
||
public void AddServices(IServiceCollection services) | ||
{ | ||
Action<JsonSerializerOptions> configure = x => { }; | ||
|
||
if (_configure != null) configure = _configure; | ||
|
||
services.AddOptions(); | ||
services.Configure(_name, configure); | ||
services.AddSingleton<IEasyCachingSerializer, DefaultJsonSerializer>(x => | ||
{ | ||
var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<JsonSerializerOptions>>(); | ||
var options = optionsMon.Get(_name); | ||
return new DefaultJsonSerializer(_name, options); | ||
}); | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
serialization/EasyCaching.Serialization.SystemTextJson/DefaultJsonSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using EasyCaching.Core.Internal; | ||
using EasyCaching.Core.Serialization; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace EasyCaching.Serialization.SystemTextJson | ||
{ | ||
/// <summary> | ||
/// Default json serializer. | ||
/// </summary> | ||
public class DefaultJsonSerializer : IEasyCachingSerializer | ||
{ | ||
/// <summary> | ||
/// The json serializer. | ||
/// </summary> | ||
private readonly JsonSerializerOptions jsonSerializerOption; | ||
/// <summary> | ||
/// The name. | ||
/// </summary> | ||
private readonly string _name; | ||
/// <summary> | ||
/// Gets the name. | ||
/// </summary> | ||
/// <value>The name.</value> | ||
public string Name => _name; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:EasyCaching.Serialization.SystemTextJson.DefaultJsonSerializer"/> class. | ||
/// </summary> | ||
/// <param name="name">Name.</param> | ||
/// <param name="serializerSettings">serializerSettings.</param> | ||
public DefaultJsonSerializer(string name, JsonSerializerOptions serializerSettings) | ||
{ | ||
_name = name; | ||
jsonSerializerOption = serializerSettings; | ||
} | ||
|
||
/// <summary> | ||
/// Deserialize the specified bytes. | ||
/// </summary> | ||
/// <returns>The deserialize.</returns> | ||
/// <param name="bytes">Bytes.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public T Deserialize<T>(byte[] bytes) | ||
{ | ||
return JsonSerializer.Deserialize<T>(bytes, jsonSerializerOption); | ||
} | ||
/// <summary> | ||
/// Deserialize the specified bytes. | ||
/// </summary> | ||
/// <returns>The deserialize.</returns> | ||
/// <param name="bytes">Bytes.</param> | ||
/// <param name="type">Type.</param> | ||
public object Deserialize(byte[] bytes, Type type) | ||
{ | ||
return JsonSerializer.Deserialize(bytes, type, jsonSerializerOption); | ||
} | ||
/// <summary> | ||
/// Deserializes the object. | ||
/// </summary> | ||
/// <returns>The object.</returns> | ||
/// <param name="value">Value.</param> | ||
public object DeserializeObject(ArraySegment<byte> value) | ||
{ | ||
var jr = new Utf8JsonReader(value); | ||
jr.Read(); | ||
if (jr.TokenType == JsonTokenType.StartArray) | ||
{ | ||
jr.Read(); | ||
var typeName = Encoding.UTF8.GetString(jr.ValueSpan.ToArray()); | ||
var type = Type.GetType(typeName, throwOnError: true); | ||
|
||
jr.Read(); | ||
return JsonSerializer.Deserialize(ref jr, type, jsonSerializerOption); | ||
} | ||
else | ||
{ | ||
throw new InvalidDataException("JsonTranscoder only supports [\"TypeName\", object]"); | ||
} | ||
} | ||
/// <summary> | ||
//; Serialize the specified value. | ||
/// </summary> | ||
/// <returns>The serialize.</returns> | ||
/// <param name="value">Value.</param> | ||
/// <typeparam name="T">The 1st type parameter.</typeparam> | ||
public byte[] Serialize<T>(T value) | ||
{ | ||
return JsonSerializer.SerializeToUtf8Bytes(value, jsonSerializerOption); | ||
} | ||
/// <summary> | ||
/// Serializes the object. | ||
/// </summary> | ||
/// <returns>The object.</returns> | ||
/// <param name="obj">Object.</param> | ||
public ArraySegment<byte> SerializeObject(object obj) | ||
{ | ||
var typeName = TypeHelper.BuildTypeName(obj.GetType()); | ||
|
||
using (var ms = new MemoryStream()) | ||
using (var jw = new Utf8JsonWriter(ms)) | ||
{ | ||
jw.WriteStartArray(); | ||
jw.WriteStringValue(typeName); | ||
|
||
JsonSerializer.Serialize(jw, obj, jsonSerializerOption); | ||
|
||
jw.WriteEndArray(); | ||
|
||
jw.Flush(); | ||
|
||
return new ArraySegment<byte>(ms.ToArray(), 0, (int)ms.Length); | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../EasyCaching.Serialization.SystemTextJson/EasyCaching.Serialization.SystemTextJson.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="System.Text.Json" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\EasyCaching.Core\EasyCaching.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,6 @@ public void TrySet_Parallel_Should_Succeed() | |
}); | ||
|
||
Assert.Equal(1, list.Count(x => x)); | ||
|
||
} | ||
|
||
[Fact] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.