It seems that the RegionInfo
object has kinda been forgotten in terms of serialization. CultureInfo
works great and is serialized to and from a string. When attempting to throw in a RegionInfo
object, I get a mess of all the properties of RegionInfo
that can't be deserialized because there is no constructor that takes all those properties in reverse. I would love to just serialize and deserialize the RegionInfo
s as strings, like CultureInfo
, but can't quite figure that out.
My attempt:
I created a regioninfo converter
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;
}
}
I stuffed that into the ConnectionSettings:
var connectionSettings = new ConnectionSettings(pool,
(builtin, settings) => new JsonNetSerializer(
builtin,
settings,
contractJsonConverters: new JsonConverter[] { new RegionInfoConverter() })
);
but I get the error: object mapping for [region] tried to parse field [region] as object, but found a concrete value
That sounds like one of my serializer pieces is wrong, but I don't feel like I quite understand enough to figure out which part it is. Thanks.
I think the issue here may be that Elasticsearch has initially inferred an
object
datatype mapping forRegionInfo
from a document to be indexed, and is now being passed astring
value forRegionInfo
. You may need to delete the index and create again, mapping theRegionInfo
property as akeyword
datatype.Here's a working example
The index request sends the following JSON