Closed mikhail-barg closed 2 weeks ago
Okay, I managed to find a way to do this, by using not a type converter, but implementing a custom NodeSerializer and replacing default ScalarNodeDeserializer (where: syntax => syntax.InsteadOf<ScalarNodeDeserializer>()
).
Will close the issue then
And another, even simpler solution would be just to implement a public static ClassName Parse(string v)
method in each class requiring such behavior
I am working on a flexible yaml deserialization where I'd like to have "shortcuts" for some classes.
For example for a class like this
I'd like to be able to not only write a regular detailed yaml:
but also a simplified one (when only a
url
matters):This looks like a nice feature in my case, because most of these yamls are going to be written manually by people and I'd like to save them from whiting boilerplate.
So I've come to the following solution:
IMaybeSimpleValue
interface and aSimpleValueAttribute
attribute (just a marker intreface and attributes):[AttributeUsage(AttributeTargets.Property] public sealed class SimpleValueAttribute: Attribute {}
I implement a custom type converter:
So the general idea is that all the classes implementing IMaybeSimpleValue intreface are handled by the type converter. The converter checks if yaml contains a scalar for the object, and if it does then it creates an instance of the object and initializes it's sole required property. (And this does work).
But if the yaml contains a proper object description, it should fall back to the default deserialization routine. And this is where it does not work because I seem to have no way to call a "default" deserialization for a class, without my custom converter. (If I call
rootDeserializer.Invoke(type)
it just goes stack overflow because it goes to my type converter again).So, is there a way to have a "default" deserialization working for my type, without repeating code from ObjectNodeDeserializer
Or is there a simpler way to achieve what I'm aiming for?