src/Library/NetPro.StackExchange.Redis/serializers/StackExchange.Redis.Extensions.Newtonsoft/NewtonsoftSerializer.cs
(开头部分) 2KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using Newtonsoft.Json;
using StackExchange.Redis.Extensions.Core;
using System;
using System.Text;
namespace StackExchange.Redis.Extensions.Newtonsoft
{
/// <summary>
/// JSon.Net implementation of <see cref="ISerializer"/>
/// </summary>
public class NewtonsoftSerializer : ISerializer
{
/// <summary>
/// Encoding to use to convert string to byte[] and the other way around.
/// </summary>
/// <remarks>
/// StackExchange.Redis uses Encoding.UTF8 to convert strings to bytes,
/// hence we do same here.
/// </remarks>
private static readonly Encoding encoding = Encoding.UTF8;
private readonly JsonSerializerSettings settings;
/// <summary>
/// Initializes a new instance of the <see cref="NewtonsoftSerializer"/> class.
/// </summary>
public NewtonsoftSerializer()
: this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NewtonsoftSerializer"/> class.
/// </summary>
/// <param name="settings">The settings.</param>
public NewtonsoftSerializer(JsonSerializerSettings settings)
{
this.settings = settings ?? new JsonSerializerSettings();
}
/// <inheritdoc/>
public byte[] Serialize(object item)
{
var type = item?.GetType();
var jsonString = JsonConvert.SerializeObject(item, type, settings);
jsonString = jsonString.TrimStart('\"').TrimEnd('\"');
return encoding.GetBytes(jsonString);
}
/// <inheritdoc/>
public T Deserialize<T>(byte[] serializedObject)
{
var jsonString = encoding.GetString(serializedObject);
jsonString = jsonString.TrimStart('\"').TrimEnd('\"');
if (typeof(T) == typeof(string) || typeof(T).IsValueType)
{
return (T)Convert.ChangeType(jsonString, typeof(T));
}
return JsonConvert.DeserializeObject<T>(jsonString, settings);
}
后面还有 13 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。