You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to be able to not only write a regular detailed yaml:
http_source:
url: "http://sample.com"auth: ..
but also a simplified one (when only a url matters):
http_source: "http://sample.com"
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:
I'm adding an IMaybeSimpleValue interface and a SimpleValueAttribute attribute (just a marker intreface and attributes):
publicsealedclassMaybeSimpleValueConverter:IYamlTypeConverter{privatereadonlyIObjectFactorym_objectFactory;internalMaybeSimpleValueConverter(IObjectFactoryobjectFactory){this.m_objectFactory =objectFactory;}bool IYamlTypeConverter.Accepts(Typetype){returntypeof(IMaybeSimpleValue).IsAssignableFrom(type);}object? IYamlTypeConverter.ReadYaml(IParserparser,Typetype,ObjectDeserializerrootDeserializer){if(parser.Current is YamlDotNet.Core.Events.Scalar){objectvalue=this.m_objectFactory.Create(type);List<PropertyInfo>valueProperties= type.GetProperties().Where(pi => pi.IsDefined(typeof(SimpleValueAttribute))).ToList();PropertyInfopi= valueProperties[0];//lots of validation skippedScalarscalar= parser.Consume<Scalar>();
pi.SetValue(value, scalar.Value);returnvalue;}else{????default deserialization goeshere!!//return rootDeserializer.Invoke(type);}}voidIYamlTypeConverter.WriteYaml(IEmitteremitter,object?value,Typetype,ObjectSerializerserializer){thrownew NotSupportedException();}}
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).
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>()).
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):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?
The text was updated successfully, but these errors were encountered: