Skip to content

Commit

Permalink
提供使用自定义缓存的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
withsalt committed Mar 4, 2022
1 parent bc703c9 commit 85c58ae
Show file tree
Hide file tree
Showing 22 changed files with 631 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OnceMi.Framework.Util.Json
{
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OnceMi.Framework.Util.Json
{
public class DateTimeNullableConverter : JsonConverter<DateTime?>
{
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return string.IsNullOrEmpty(reader.GetString()) ? default(DateTime?) : DateTime.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value?.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace OnceMi.Framework.Util.Json
{
public class ExceptionConverter : JsonConverter<Exception>
{
public override Exception Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException();
}

public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(nameof(value.Message), value.Message);
writer.WriteString(nameof(value.StackTrace), value.StackTrace);
writer.WriteEndObject();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OnceMi.Framework.Util.Json
{
/// <summary>
/// 数字序列化为字符串
/// 使用方式:
/// 1、熟悉上面添加:[JsonConverter(typeof(NumberToStringConverter))]
/// 2、var deserializeOptions = new JsonSerializerOptions(); deserializeOptions.Converters.Add(new NumberToStringConverter());
/// 解决问题:
/// 解决雪花算法生成的id过长导致js丢失精度的问题。
/// 适用范围:
/// System.Text.Json
/// </summary>
public class NumberToStringConverter : JsonConverter<object>
{
public override bool CanConvert(Type typeToConvert)
{
var result = typeof(long) == typeToConvert
|| typeof(int) == typeToConvert
|| typeof(long?) == typeToConvert
|| typeof(int?) == typeToConvert;
return result;
}

public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.TryGetInt64(out long n) ?
n.ToString() :
reader.GetDouble().ToString();
}
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
{
return document.RootElement.Clone().ToString();
}
}

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
if (value == null)
writer.WriteNullValue();
else
writer.WriteStringValue(value.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OnceMi.Framework.Util.Json
{
public class TypeConverter : JsonConverter<Type>
{
public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException();
}

public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.AssemblyQualifiedName);
}
}
}
Loading

0 comments on commit 85c58ae

Please sign in to comment.