似乎该RegionInfo
对象在序列化方面已经被遗忘了。CultureInfo
效果很好,并且在字符串之间进行了序列化。当试图抛出一个RegionInfo
对象时,我得到了所有RegionInfo
无法反序列化的属性的混乱,因为没有构造函数可以反转所有这些属性。我很想将RegionInfo
s 序列化和反序列化为字符串,例如CultureInfo
,但不能完全弄清楚。
我的尝试:
我创建了一个 regioninfo 转换器
public class RegionInfoConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, ((RegionInfo)value).Name);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
return new RegionInfo(token.ToObject<string>());
}
public override bool CanConvert(Type objectType)
{
return typeof(RegionInfo) == objectType;
}
}
我把它塞进ConnectionSettings:
var connectionSettings = new ConnectionSettings(pool,
(builtin, settings) => new JsonNetSerializer(
builtin,
settings,
contractJsonConverters: new JsonConverter[] { new RegionInfoConverter() })
);
但我得到了错误:object mapping for [region] tried to parse field [region] as object, but found a concrete value
这听起来像是我的序列化程序之一是错误的,但我觉得我不太了解它是哪一部分。谢谢。