I've got an interesting situation whereby elastic v7 default utf8json has managed to serialize my object, but is unable to de-serialize it correctly.
public class MyClass : FlagsSet
{
[JsonIgnore]
public bool IsActive
{
get
{
return this.IsSet("active");
}
}
}
public class FlagsSet : ICollection<string>, IEnumerable<string>, IEnumerable
{
private readonly HashSet<string> _list = new HashSet<string>((IEqualityComparer<string>) StringComparer.InvariantCultureIgnoreCase);
...
public void Add(string item)
{
if (string.IsNullOrEmpty(item))
return;
this._list.Add(item);
}
}
If I was using json.net I'd handle this by writing a converter, but I'm not able to see an equivalent using utf8json as it appears the formatters used by the default serializer (DefaultHighLevelSerializer) are all registered internally. I've read a few pages on customer serializers (notably this one.. https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/custom-serialization.html)
So in short..
Answered in the elastic discussion (as linked by Russ above).. https://discuss.elastic.co/t/does-utf8json-used-in-elastic-v7-x-support-user-defined-custom-formatters/237283/4?u=forloop
A quick summary of the answer here..
No it does not. Utf8Json used internally is a fork of Utf8Json and all the types are internal. It's used for serializing requests and responses, and also use to serialize documents too, by default. There is no way to register custom formatters for documents however. Some simple customisation is exposed through attributes e.g. StringEnumAttribute to serialize enums as strings, but nothing more complex. If custom serialization is required, I would recommend looking at JsonNetSerializer and custom converters for NEST 7.x and lower.
So in short, if you want the benefits of the faster utf8json serialization AND custom formatters.. then you'll need implement a custom serializer using a non-elastic specific import of the utf8json library. Alternatively if that sounds like a lot of work(!) AND/OR you're happy with the slower performing json.net (as used in NEST v6's default serializer), then you can revert to this and use the custom converters.
Forward looking though, things appear brighter as v8 NEST will likely get a 'new new' serializer which will (hopefully) support custom converter/formatters..
For NEST 8.x, we are looking at System.Text.Json for serialization and would likely expose custom formatters/converters for this.