-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
631 additions
and
50 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/samples/Sample.AspNetCore.Mvc/CacheProviders/Json/Converters/DateTimeConverter.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,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")); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...samples/Sample.AspNetCore.Mvc/CacheProviders/Json/Converters/DateTimeNullableConverter.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,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")); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/samples/Sample.AspNetCore.Mvc/CacheProviders/Json/Converters/ExceptionConverter.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,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(); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/samples/Sample.AspNetCore.Mvc/CacheProviders/Json/Converters/NumberToStringConverter.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,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()); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/samples/Sample.AspNetCore.Mvc/CacheProviders/Json/Converters/TypeConverter.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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.