youxinren / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

How to convert to a JavaBean while yaml's key is NOT same with javaBean's field? #152

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There is a JavaBean like this:
public class Param {
  private String name;
  private String inputPart;
  //...
}
The yaml string is like this:
name: "Test", input_part: "abc"

The parsing code like this:
Yaml yaml = new Yaml();
Param p = yaml.loadAs(yamlStr, Param.class);

but get exception like this:
Caused by: org.yaml.snakeyaml.error.YAMLException: Cannot create 
property=input_part for JavaBean=ngoss.im.wizard.model.Param@6c596c2a; Unable 
to find property 'input_part' on class: ngoss.im.wizard.model.Param
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:299)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:331)
    ... 32 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 
'input_part' on class: ngoss.im.wizard.model.Param
    at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:128)
    at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:120)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:308)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:240)
    ... 34 more

any way to solve this situation?

Original issue reported on code.google.com by way...@gmail.com on 2 Jul 2012 at 2:06

GoogleCodeExporter commented 9 years ago
1)
public class Param {
  private String name;
  private String inputPart;
  public setInput_part(String part) { inputPart = part} ;
  //...
}
2) 
Create standard Java structure (no loadAs()) and transform the map of values to 
your business JavaBean

3) 
Write your own Contructor

Original comment by py4fun@gmail.com on 2 Jul 2012 at 4:50

GoogleCodeExporter commented 9 years ago
thank you, py4fun.
I know the former 2 ways, but i don't think they are good ways to solve it. Can 
you give me some sample for the third way? because what i want acutally is a 
mapping method between the key'name in yaml str and the field's name in 
javaBean, like the mapping in Hibernate,JPA or myBatis:
class Param {
@Column(name="column_one")
private String columnOne;
...
}.
or 
<property="columnOne" column="column_one">

because i have another problem, if there is a key word named 'default' in a 
yaml string, how do i parese it without using the former 2 ways?
yaml string like this:
name: "Test", input_part: "abc", default: "111"

any help will be appreciated!

Original comment by way...@gmail.com on 3 Jul 2012 at 9:42

GoogleCodeExporter commented 9 years ago
By using this clone http://code.google.com/r/alexandermaslov-sny-beancontrol/ 
you can achieve what you expect like this:

**********************
TypeDescription paramDesc = new TypeDescription(Param.class);
paramDesc.substituteProperty("input_part", String.class, "getInputPart", 
"setInputPart");
paramDesc.substituteProperty("default", String.class, "getMore", "setMore");

Yaml yaml = new Yaml();
yaml.addTypeDescription(paramDesc);

Param p = yaml.loadAs("name: \"Test\"\ninput_part: \"abc\"\ndefault: \"some 
value\"", Param.class);
**********************

I was planning to add property name mapping, but it's not there yet.

Original comment by alexande...@gmail.com on 4 Jul 2012 at 6:56

GoogleCodeExporter commented 9 years ago
Part of this task has been delivered with the fix for issue 154

Original comment by aso...@gmail.com on 27 Jul 2012 at 3:08

GoogleCodeExporter commented 9 years ago
why isn't Alexander's clone (with property name mappings) merged into the main 
snakeyaml project?

Original comment by gdo...@gmail.com on 24 Mar 2015 at 12:54

GoogleCodeExporter commented 9 years ago
First of all - it is work-in-progress. But mostly - luck of time at the moment 
to (re)write tests to keep coverage good.
I hope to do it before this August (since we need to migrate the whole thing 
somewhere).

Original comment by alexande...@gmail.com on 25 Mar 2015 at 6:23