UcasRichard / snakeyaml

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

dumping Apache Tika Metadata ? #95

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, 

First, THANKS a lot for your project. Being able to manipulate yaml from Java 
in so valuable.

I would like to discuss about something which is not an issue I think, but 
still cannot solve it... I have to use with org.apache.tika.metadta.Metadata 
(http://tika.apache.org/0.7/api/org/apache/tika/metadata/Metadata.html).
I think these metadata could be nicely dumped to or loaded from yaml, for tests 
or maybe end user editions in our case.

But this class is not a JavaBean, so trying to dump it produces a YAMLException 
org.yaml.snakeyaml.error.YAMLException: No JavaBean properties found in 
org.apache.tika.metadata.Metadata. Is there a way to "Represent" it a nice way?

Thanks for any tip, and sorry as it's not directly linked to yaml.
jg

What version of the product are you using?
yaml-1.7

Original issue reported on code.google.com by jgrand...@gmail.com on 16 Nov 2010 at 5:34

GoogleCodeExporter commented 9 years ago
From the Metadata.java it looks like BeanAccess.FIELD might work for you. 
For dumping and for loading since there is nothing special done in setters.

But it was brief look ;) So you better try yourself.

Yaml.setBeanAccess(BeanAccess.FIELD)

-alex 

Original comment by alexande...@gmail.com on 16 Nov 2010 at 7:18

GoogleCodeExporter commented 9 years ago
Thank you very much Alex, 
I tried your tip, and it works well for a simple example. Here's the test:

Yaml y = new org.yaml.snakeyaml.Yaml()
y.setBeanAccess(org.yaml.snakeyaml.introspector.BeanAccess.FIELD)

Metadata met = new Metadata();
met.set("met1", "whatever")
met.set("met2", "something")
met.add("met2", "something else");

y.dump(m)
!!org.apache.tika.metadata.Metadata
metadata:
  met1: [whatever]
  met2: [something, something else]

But I still have to learn how to properly load it!!
y.load(FileInputStream(File("metadata.yml")))
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList 
cannot be cast to [Ljava.lang.String;

Thanks again, 
Regards
jg

Original comment by jgrand...@gmail.com on 16 Nov 2010 at 8:37

GoogleCodeExporter commented 9 years ago
This issue was updated by revision 191ab72b39.

GenericProperty.class fails to identify type when Collection's parameter is an 
Array.
Workaround: use TypeDespriptor to specify correct PropertyType

Original comment by alexande...@gmail.com on 16 Nov 2010 at 12:28

GoogleCodeExporter commented 9 years ago
This issue was closed by revision fa3b0e3d2a.

Original comment by alexande...@gmail.com on 16 Nov 2010 at 12:28

GoogleCodeExporter commented 9 years ago
It should work now. You can use either latest sources or something like this 
with 1.7:

TypeDescription aTypeDescr = new TypeDescription(Metadata.class);
aTypeDescr.putMapPropertyType("meta", String.class, String[].class);

Constructor c = new Constructor();
c.addTypeDescription(aTypeDescr);
Yaml yaml2load = new Yaml(c);
yaml2load.setBeanAccess(BeanAccess.FIELD);

yaml2load.load(....)

See http://code.google.com/p/snakeyaml/wiki/Documentation#Type_safe_collections

Original comment by alexande...@gmail.com on 16 Nov 2010 at 12:34

GoogleCodeExporter commented 9 years ago
You can also try the latest snapshot:
https://oss.sonatype.org/content/groups/public/org/yaml/snakeyaml/

Original comment by py4fun@gmail.com on 16 Nov 2010 at 1:49

GoogleCodeExporter commented 9 years ago
Please be aware that you do not have to use global tags (with the class name). 
You can find an example in the tests (testNoTags):
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/issues/issue95/ArrayInGenericCollectionTest.java

Original comment by py4fun@gmail.com on 16 Nov 2010 at 3:10