jeudi 9 juillet 2015

Turn scalar from YAML to specific subclass instance in Java

I have a simple interface

public interface DoubleParameter{
double getParameter();
}

And one of the subclasses is the simple:

public class FixedDoubleParameter{
private double parameter;
void setParameter(double newParameter){parameter=newParameter;}
double getParameter(){return parameter;}
}

Now with yaml I can have:

!!parameters.FixedDoubleParameter
parameter: 123.0

And that will be read correctly by snakeyaml (and I can add representers to make it prettier).

What I really want is to instantiate an object like this:

class User{
       DoubleParameter variable;
}

where variable will be FixedDoubleParameter with value 123. I can write it in the YAML like this already:

!!User
variable: !!parameters.FixedDoubleParameter
    parameter: 123.0

But I wonder if it is possible to simply use this:

!!User
variable: 123.0

That is, simply to override the standard scalar constructor of snakeYAML when it knows the type it is trying to instantiate is a DoubleParameter to return a FixedDoubleParameter instead





Aucun commentaire:

Enregistrer un commentaire