Hi,
The question: is there any mechanism existing or planned in ServiceStack.Text which gives similar capabilities to those found in System.Text.Json.Serialization.JsonConverterFactory?
For various business reasons which aren’t really relevant to the question (mostly in that I can’t change them), I find myself in a situation where:
- I have numerous different POCOs which represent specific types of entities. These POCOs can be complex.
- Some of the POCOs in #1 contain collections of other of these POCOs (i.e., an arbitrary tree of these types)
- I need a variety of collections of these POCOs, but unfortunately I can’t easily wrap them in collection types ServiceStack seems to natively understand. Instead, they need to be in a collection of our internal specification with specific business-relevant properties
- I need to (de)serialize these types and their collections to/from JSON.
In other parts of the system that must use System.Text.Json.Serialization.JsonConverterFactory, I’ve been able to easily use a single JsonConverterFactory which accomplishes this.
Right now, for the ServiceStack part which uses these types, the only solution I’ve found is to have many dozens entries for JSConfig which all look similar to:
JsConfig<MyCollectionType<MyPocoTypeX>>.RawDeserializeFn = s => JsonSerializer.DeserializeFromString<List<MyPocoTypeX>>(s).ToMyCollectionType();
JsConfig<MyCollectionType<MyPocoTypeY>>.RawDeserializeFn = s => JsonSerializer.DeserializeFromString<List<MyPocoTypeY>>(s).ToMyCollectionType();
I do understand I could build an entirely separate parallel hierarchy of all these types which contain the MyPoco types, NOT use the MyCollectionType, and instead use List so ServiceStack is comfortable deserializing them, and then automap, but that seems even more painful than just using dozens of RawDeserializerFns
I suppose I could also do some reflection trickery to automatically find all the uses of MyCollection and then dynamically build appropriate RawDeserializeFn methods…but really, it seems what I need a JsonConverterFactory like is provided in System.Text.Json.Serialization.JsonConverterFactory
Any thoughts?
Thanks!
EDIT:
Here’s a link to the Microsoft documentation for the JsonConverterFactory, for those who haven’t run across this before