You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace OMS.NET.Common
|
|
{
|
|
public class Point
|
|
{
|
|
public double X { get; set; }
|
|
public double Y { get; set; }
|
|
}
|
|
|
|
public class Util
|
|
{
|
|
public static readonly JsonSerializerOptions options = new()
|
|
{
|
|
ReadCommentHandling = JsonCommentHandling.Skip, //允许注释
|
|
AllowTrailingCommas = true,//允许尾随逗号
|
|
//PropertyNamingPolicy = new InstructNamingPolicy(), // 属性名为定制转换
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值
|
|
WriteIndented = true, // 美化输出
|
|
PropertyNameCaseInsensitive = true,//属性名忽略大小写
|
|
};
|
|
|
|
public static string ObjToBase64(object obj)
|
|
{
|
|
string utf8 = JsonSerializer.Serialize(obj, options);
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(utf8));
|
|
}
|
|
|
|
public static string JsonToBase64(string json)
|
|
{
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
|
|
}
|
|
|
|
public static string Base64ToJson(string base64)
|
|
{
|
|
return Encoding.UTF8.GetString(Convert.FromBase64String(base64));
|
|
}
|
|
|
|
// public static JsonNode? JsonNodeFromJson(string json)
|
|
// {
|
|
// return JsonNode.Parse(json);
|
|
// }
|
|
|
|
//===========================================================
|
|
|
|
public static List<Point> PointsFromBase64(string base64)
|
|
{
|
|
string utf8 = Base64ToJson(base64);
|
|
return PointsFromJson(utf8);
|
|
}
|
|
|
|
public static List<Point> PointsFromJson(string json)
|
|
{
|
|
return JsonSerializer.Deserialize<List<Point>>(json, options) ?? new List<Point>();
|
|
}
|
|
|
|
public static Point PointFromBase64(string base64)
|
|
{
|
|
string utf8 = Base64ToJson(base64);
|
|
return PointFromJson(utf8);
|
|
}
|
|
|
|
public static Point PointFromJson(string json)
|
|
{
|
|
return JsonSerializer.Deserialize<Point>(json, options) ?? throw new Exception("转化point类型失败:" + json);
|
|
}
|
|
}
|
|
} |