|
|
using System.Text.Json;
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
namespace OMS.NET.Common
|
|
|
{
|
|
|
public class UserInfo
|
|
|
{
|
|
|
public string? Name { get; set; }
|
|
|
public string? Email { get; set; }
|
|
|
public string? Color { get; set; }
|
|
|
}
|
|
|
|
|
|
public class ActiveDataElement
|
|
|
{
|
|
|
public static readonly JsonSerializerOptions options = new()
|
|
|
{
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // 属性名为小写
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值,
|
|
|
WriteIndented = true, // 美化输出
|
|
|
Converters = { new ActiveDataElementConverter() }
|
|
|
};
|
|
|
public string EID { get; set; }
|
|
|
public UserInfo? Pick { get; set; }
|
|
|
public UserInfo? Select { get; set; }
|
|
|
|
|
|
public ActiveDataElement(long id)
|
|
|
{
|
|
|
EID = "E" + id;
|
|
|
}
|
|
|
|
|
|
public string ToJsonString()
|
|
|
{
|
|
|
return JsonSerializer.Serialize(this, options);
|
|
|
}
|
|
|
|
|
|
public static ActiveDataElement? JsonParse(string json)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return JsonSerializer.Deserialize<ActiveDataElement>(json, options);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
GlobalArea.Log.Error(ex.Message);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public class ActiveDataElementConverter : JsonConverter<ActiveDataElement>
|
|
|
{
|
|
|
public override ActiveDataElement Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
{
|
|
|
if (reader.TokenType != JsonTokenType.StartObject)
|
|
|
{
|
|
|
throw new JsonException();
|
|
|
}
|
|
|
reader.Read();
|
|
|
string? eid = (reader.GetString()?[1..]) ?? throw new JsonException(); // Removing 'E' prefix to get the ID
|
|
|
var element = new ActiveDataElement(Convert.ToInt64(eid));
|
|
|
while (reader.Read())
|
|
|
{
|
|
|
if (reader.TokenType == JsonTokenType.EndObject)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (reader.TokenType == JsonTokenType.StartObject)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if (reader.TokenType != JsonTokenType.PropertyName)
|
|
|
{
|
|
|
throw new JsonException();
|
|
|
}
|
|
|
|
|
|
string? propertyName = reader.GetString();
|
|
|
reader.Read();
|
|
|
|
|
|
switch (propertyName)
|
|
|
{
|
|
|
case "pick":
|
|
|
element.Pick = DeserializeUserInfo(ref reader);
|
|
|
break;
|
|
|
case "select":
|
|
|
element.Select = DeserializeUserInfo(ref reader);
|
|
|
break;
|
|
|
default:
|
|
|
reader.Skip(); // Skip unknown properties
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
//reader.CurrentDepth = 0;
|
|
|
reader.Read();
|
|
|
//还需要读取一次,因为之前有过一次读取,跳过了一个token,最后一个token也要读一次
|
|
|
return element;
|
|
|
}
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, ActiveDataElement value, JsonSerializerOptions options)
|
|
|
{
|
|
|
writer.WriteStartObject();
|
|
|
writer.WritePropertyName(value.EID);
|
|
|
writer.WriteStartObject();
|
|
|
if (value.Pick != null) { WriteUserInfo(writer, "pick", value.Pick); }
|
|
|
if (value.Select != null) { WriteUserInfo(writer, "select", value.Select); }
|
|
|
writer.WriteEndObject();
|
|
|
writer.WriteEndObject();
|
|
|
}
|
|
|
|
|
|
private static void WriteUserInfo(Utf8JsonWriter writer, string propertyName, UserInfo userInfo)
|
|
|
{
|
|
|
writer.WriteStartObject(propertyName);
|
|
|
if (userInfo != null)
|
|
|
{
|
|
|
writer.WriteString("name", userInfo.Name);
|
|
|
writer.WriteString("email", userInfo.Email);
|
|
|
writer.WriteString("color", userInfo.Color);
|
|
|
}
|
|
|
writer.WriteEndObject();
|
|
|
}
|
|
|
|
|
|
private static UserInfo DeserializeUserInfo(ref Utf8JsonReader reader)
|
|
|
{
|
|
|
if (reader.TokenType != JsonTokenType.StartObject)
|
|
|
{
|
|
|
throw new JsonException();
|
|
|
}
|
|
|
|
|
|
string? name = null;
|
|
|
string? email = null;
|
|
|
string? color = null;
|
|
|
|
|
|
while (reader.Read())
|
|
|
{
|
|
|
if (reader.TokenType == JsonTokenType.EndObject)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
if (reader.TokenType != JsonTokenType.PropertyName)
|
|
|
{
|
|
|
throw new JsonException();
|
|
|
}
|
|
|
string? propertyName = reader.GetString();
|
|
|
reader.Read();
|
|
|
switch (propertyName)
|
|
|
{
|
|
|
case "name":
|
|
|
name = reader.GetString();
|
|
|
break;
|
|
|
case "email":
|
|
|
email = reader.GetString();
|
|
|
break;
|
|
|
case "color":
|
|
|
color = reader.GetString();
|
|
|
break;
|
|
|
default:
|
|
|
reader.Skip(); // Skip unknown properties
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return new UserInfo { Name = name, Email = email, Color = color };
|
|
|
}
|
|
|
}
|
|
|
} |