添加项目文件。

This commit is contained in:
2025-12-02 13:10:10 +08:00
parent 93a2382a16
commit 289aa4cbe7
400 changed files with 91177 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
using System.Security.Claims;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Atomx.Utils.Json.Converts
{
public class ClaimConverter : JsonConverter<Claim>
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert == typeof(Claim);
}
public override Claim Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using (var jsonDocument = JsonDocument.ParseValue(ref reader))
{
var text = jsonDocument.RootElement.GetRawText();
var data = JsonSerializer.Deserialize<ClaimLite>(text, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
return new Claim(data?.Type ?? string.Empty, data?.Value ?? string.Empty, data?.ValueType ?? string.Empty);
}
}
public override void Write(Utf8JsonWriter writer, Claim value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value);
}
}
public class ClaimLite
{
public string Type { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
public string ValueType { get; set; } = string.Empty;
}
}

View File

@@ -0,0 +1,33 @@
using System.Text.Json;
namespace Atomx.Utils.Json.Converts
{
public class DateTimeJsonConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
readonly string _dateFormatString;
public DateTimeJsonConverter()
{
_dateFormatString = "yyyy-MM-dd HH:mm:ss";
}
public DateTimeJsonConverter(string dateFormatString)
{
_dateFormatString = dateFormatString;
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var text = reader.GetString();
if (string.IsNullOrEmpty(text))
{
return default;
}
return DateTime.Parse(text);
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("s"));
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Buffers;
using System.Buffers.Text;
using System.Text.Json;
namespace Atomx.Utils.Json.Converts
{
public class LongJsonConverter : System.Text.Json.Serialization.JsonConverter<long>
{
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
if (Utf8Parser.TryParse(span, out long number, out int bytesConsumed) && span.Length == bytesConsumed)
{
return number;
}
if (long.TryParse(reader.GetString(), out number))
{
return number;
}
}
return reader.GetInt64();
}
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}

View File

@@ -0,0 +1,26 @@
using Atomx.Utils.Json.Converts;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
namespace Atomx.Utils.Json
{
public class JsonOptions
{
public static JsonSerializerOptions DefaultOptions()
{
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
opts.Converters.Add(new LongJsonConverter());
opts.Converters.Add(new DateTimeJsonConverter());
opts.Converters.Add(new ClaimConverter());
return opts;
}
}
}

View File

@@ -0,0 +1,30 @@
using System.Text.Json;
namespace Atomx.Utils.Json
{
public static class JsonSerialize
{
public static string ToJson(this object obj, JsonSerializerOptions? options = null)
{
var opts = JsonOptions.DefaultOptions();
if (options != null)
{
opts = options;
}
var data = JsonSerializer.Serialize(obj, opts);
return data;
}
public static T? FromJson<T>(this string json, JsonSerializerOptions? options = null)
{
var opts = JsonOptions.DefaultOptions();
if (options != null)
{
opts = options;
}
var data = JsonSerializer.Deserialize<T>(json, opts);
return data;
}
}
}