Skip to content

Commit 435b51d

Browse files
committed
修复 错误的情景文件反序列化
1 parent e9e9473 commit 435b51d

File tree

4 files changed

+67
-27
lines changed

4 files changed

+67
-27
lines changed

Core/SDKs/CustomScenario/CustomScenarioManger.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,31 @@ public static void Load(FileInfo fileInfo)
8484
try
8585
{
8686
var deserializeObject = JsonSerializer.Deserialize<CustomScenario>(json, ConfigManger.DefaultOptions);
87-
for (var index = 0; index < deserializeObject.InputValue.Count; index++)
88-
{
89-
var (key, value) = deserializeObject.InputValue[index];
90-
if (value is JsonElement jsonElement)
91-
{
92-
if (jsonElement.ValueKind == JsonValueKind.Object)
93-
{
94-
deserializeObject.InputValue.SetValueWithoutNotify(deserializeObject.InputValue[index].Key,
95-
new object());
96-
}
97-
}
98-
}
99-
100-
for (var index = 0; index < deserializeObject.Values.Count; index++)
101-
{
102-
var (key, value) = deserializeObject.Values[index];
103-
if (value is JsonElement jsonElement)
104-
{
105-
if (jsonElement.ValueKind == JsonValueKind.Object)
106-
{
107-
deserializeObject.Values.SetValueWithoutNotify(deserializeObject.Values[index].Key,
108-
new object());
109-
}
110-
}
111-
}
87+
// for (var index = 0; index < deserializeObject.InputValue.Count; index++)
88+
// {
89+
// var (key, value) = deserializeObject.InputValue[index];
90+
// if (value is JsonElement jsonElement)
91+
// {
92+
// if (jsonElement.ValueKind == JsonValueKind.Object)
93+
// {
94+
// deserializeObject.InputValue[deserializeObject.InputValue[index].Key]= new object();
95+
//
96+
// }
97+
// }
98+
// }
99+
//
100+
// for (var index = 0; index < deserializeObject.Values.Count; index++)
101+
// {
102+
// var (key, value) = deserializeObject.Values[index];
103+
// if (value is JsonElement jsonElement)
104+
// {
105+
// if (jsonElement.ValueKind == JsonValueKind.Object)
106+
// {
107+
// deserializeObject.Values[deserializeObject.Values[index].Key]= new object();
108+
//
109+
// }
110+
// }
111+
// }
112112

113113
deserializeObject.OnDeserialized();
114114

Core/SDKs/Services/Config/ConfigManger.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static class ConfigManger
2828
IncludeFields = true,
2929
WriteIndented = true,
3030
ReferenceHandler = ReferenceHandler.Preserve,
31-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
31+
Converters = { new ObjectJsonConverter() }
32+
// DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
3233
};
3334

3435
public static void Init()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Dynamic;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Core.SDKs.Services.Config;
6+
7+
public class ObjectJsonConverter : JsonConverter<object>
8+
{
9+
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
// 根据ValueKind判断数据类型
12+
if (reader.TokenType ==
13+
// 若为JsonElement类型,再根据ValueKind属性判断
14+
JsonTokenType.StartObject)
15+
{
16+
JsonSerializer.Deserialize<ExpandoObject>(ref reader, options);
17+
return new object();
18+
}
19+
20+
if (reader.TokenType == JsonTokenType.Number)
21+
return reader.TryGetInt64(out long l) ? l : reader.GetDouble();
22+
if (reader.TokenType == JsonTokenType.String)
23+
return reader.TryGetDateTime(out DateTime datetime) ? datetime : reader.GetString();
24+
if (reader.TokenType == JsonTokenType.True)
25+
return true;
26+
if (reader.TokenType == JsonTokenType.False)
27+
return false;
28+
if (reader.TokenType == JsonTokenType.Null)
29+
return null;
30+
// 这里可以根据需要添加更多的数据类型判断
31+
return JsonSerializer.Deserialize<JsonElement>(ref reader);
32+
}
33+
34+
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
35+
{
36+
JsonSerializer.Serialize(writer, value, value.GetType(), options);
37+
}
38+
}

Core/SDKs/Services/Plugin/AssemblyLoadContextH.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public AssemblyLoadContextH(string pluginPath, string name) : base(isCollectible
3636
IncludeFields = true,
3737
WriteIndented = true,
3838
ReferenceHandler = ReferenceHandler.Preserve,
39-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
39+
Converters = { new ObjectJsonConverter() }
40+
//DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
4041
};
4142
_assembly = null;
4243
AvaloniaPropertyRegistry.Instance.UnregisterByModule(sender.Assemblies.First()

0 commit comments

Comments
 (0)