1

似乎该RegionInfo对象在序列化方面已经被遗忘了。CultureInfo效果很好,并且在字符串之间进行了序列化。当试图抛出一个RegionInfo对象时,我得到了所有RegionInfo无法反序列化的属性的混乱,因为没有构造函数可以反转所有这些属性。我很想将RegionInfos 序列化和反序列化为字符串,例如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

这听起来像是我的序列化程序之一是错误的,但我觉得我不太了解它是哪一部分。谢谢。

4

1 回答 1

1

我认为这里的问题可能是 Elasticsearch 最初已经从要索引的文档中推断出一个object数据类型映射,现在RegionInfo正在传递一个string. RegionInfo您可能需要删除索引并重新创建,将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"
}
于 2019-01-09T04:42:12.130 回答