Assign default Constructor (Bean) value to param if value in yaml is a constant (key) value

378 views
Skip to first unread message

Niranjan Karunanandham

unread,
Apr 16, 2016, 1:43:36 PM4/16/16
to SnakeYAML
Hi all,

In my code, am using I read the contents from a yaml file into a string and load it to the class (X) using the method loadAs(String, Class). I have a requirement where when loading the values from the yaml file to the Object X, if a value in the file is a constant, i.e., say it is $default, then it should be replaced using the default value defined for the Object X.

For example:
Object X:
String id = "A0001"
String name = "AAAAA";

Yaml file:
id: $default
name: sample 

Here after loading the yaml file to Object X, id should be contain the value A0001 instead of $default and name should be sample

Is there a way to do this using yaml methods or should I check each value after loading it to the object and replace it?

Regards,
Niranjan

Andrey Somov

unread,
Apr 16, 2016, 2:33:09 PM4/16/16
to SnakeYAML
Hi Niranjan,
you can override Constructor.constructJavaBean2ndStep to change how the JavaBean properties are created.

Cheers,
Andrey

Niranjan Karunanandham

unread,
Apr 17, 2016, 10:11:44 AM4/17/16
to snakeya...@googlegroups.com, public...@gmail.com
Hi Andrey,

Thanks. I was able to set the value by overriding Constructor.constructJavaBean2ndStep. But this requires me to override the entire method, where I need to handle all cases for each new object (which is already handled by the yaml default method). 

What I noticed was that the object passed to this method contains the default value and this is replaced with the value in node. Is there any rule or such that can be based to yaml to use the default value (defined in the Class) instead of the value from the node? In this way, I do not have to override the entire constructJavaBean2ndStep.

Regards,
Niranjan

--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snakeyaml-cor...@googlegroups.com.
To post to this group, send email to snakeya...@googlegroups.com.
Visit this group at https://groups.google.com/group/snakeyaml-core.
For more options, visit https://groups.google.com/d/optout.



--
Niranjan Karunanandham
Reply all
Reply to author
Forward
0 new messages