似乎RegionInfo
对象在序列化方面有点被遗忘了。 CultureInfo
工作得很好,并且被序列化为字符串。当尝试抛入一个RegionInfo
对象时,我得到了无法反序列化的所有属性RegionInfo
的混乱,因为没有构造函数可以反向获取所有这些属性。我很想将RegionInfo
序列化和反序列化为字符串,就像 CultureInfo
一样,但不能完全弄清楚。
我的尝试:
我创建了一个区域信息转换器
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;
}
}
我把它塞进了连接设置:
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
这听起来像是我的一个序列化器部分是错误的,但我觉得我不太了解它是什么部分。谢谢。
我认为这里的问题可能是 Elasticsearch 最初从要索引的文档推断出RegionInfo
的object
数据类型映射,现在正在传递RegionInfo
的string
值。您可能需要删除索引并重新创建,将 RegionInfo
属性映射为 keyword
数据类型。
这是一个工作示例
private static void Main()
{
var defaultIndex = "my_index";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool, (b, s) =>
new JsonNetSerializer(b, s, contractJsonConverters: new JsonConverter[] { new RegionInfoConverter() })
)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);
var createIndexResponse = client.CreateIndex(defaultIndex, c => c
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(0)
)
.Mappings(m => m
.Map<MyEntity>(mm => mm
.AutoMap()
.Properties(p => p
.Keyword(k => k
.Name(n => n.RegionInfo)
)
)
)
)
);
var indexResponse = client.Index(new MyEntity
{
RegionInfo = RegionInfo.CurrentRegion
}, i => i.Refresh(Refresh.WaitFor));
}
public class MyEntity
{
public RegionInfo RegionInfo { get; set; }
}
public class RegionInfoConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
writer.WriteValue(((RegionInfo)value).Name);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
if (reader.TokenType != JsonToken.String)
throw new JsonSerializationException($"Cannot deserialize {nameof(RegionInfo)} from {reader.TokenType}");
return new RegionInfo((string)reader.Value);
}
public override bool CanConvert(Type objectType)
{
return typeof(RegionInfo) == objectType;
}
}
索引请求发送以下 JSON
{
"regionInfo": "AU"
}