Loading an ArrayList<SomeType> with yaml.load()

1,327 views
Skip to first unread message

Jay Jay Billings

unread,
Feb 17, 2013, 5:15:35 PM2/17/13
to snakeya...@googlegroups.com
I have a YAML file that has blocks similar to the sequence below:

- name: myName
  description: my Description
- name: otherName
  description : other Description
- name: thirdName
  description : third Description

I have created a Java Bean class that represents something like this, let's call it the Name class which has name and description as attributes. What is the best way to load this sequence into an ArrayList<Name> instead of an ArrayList<Map>? Calling yaml.load(input) loads the former and yaml.loadAs(input, Name.class) fails because it is a sequence.

Thanks,
Jay Jay Billings

Jordan Angold

unread,
Feb 18, 2013, 2:31:50 AM2/18/13
to snakeya...@googlegroups.com
If you use tags to tell SnakeYAML what type those maps are, then they will be loaded as that type. For example:

  name: myName
  description: my description
  name: otherName
  description: other description
...

That's the easiest way. You could probably achieve the same result in other ways, but it's not nearly as easy.

You may find it best to define tags for package names, especially if they are very long:

%TAG !pkg! tag:yaml.org,2002:some.package.
- !pkg!Name
  name: my name
  description: my description
...

Good luck,
/Jordan

Jay Jay Billings

unread,
Feb 18, 2013, 7:12:45 AM2/18/13
to snakeya...@googlegroups.com

Thanks, Jordan. Unfortunately the file I am parsing is generated by a third party and does not have tags. It is a 1MB file, so putting them in by hand is out too.

Jay

-----
Jay Jay Billings
Computational Scientist
Oak Ridge National Laboratory
Twitter Handle: @jayjaybillings

--
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 http://groups.google.com/group/snakeyaml-core?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jordan Angold

unread,
Feb 19, 2013, 3:12:26 AM2/19/13
to snakeya...@googlegroups.com
That's unfortunate.

In that case, you have two main options:
1) Write a loop to build instances of Name from the Maps that SnakeYAML gives you.
2) Extend org.yaml.snakeyaml.constructor.Constructor to follow the particular rules of your document.

I feel like option (1) is much easier to execute. It's unfortunate, but SnakeYAML can't do the whole job automatically.

/Jordan

Andrey Somov

unread,
Feb 19, 2013, 3:28:19 AM2/19/13
to snakeya...@googlegroups.com
Just for clarification.
The YAML dicument can be very flexible:



- name: myName
  description: my Description
- 3
- name: thirdName
  description : third Description
- 5: myName
  description: my Description
- name: otherName
- name: thirdName
  description : third Description

As you can see the parser must be prepared to receive any structure. The second option recommended by Jordan basically allows to choose at runtime (dynamically) the tag for data structure.

But the usecase is valid: it would be nice to extend the API to add an option to parse a sequence of JavaBeans

Cheers,
Andrey

Jay Jay Billings

unread,
Feb 19, 2013, 7:13:34 AM2/19/13
to snakeya...@googlegroups.com

Thanks Jordan. I implemented #1 on Sunday, but was just hoping for there to be an automated way similar to the way JAXB handles XML. Thanks again for your help!

-----
Jay Jay Billings
Computational Scientist
Oak Ridge National Laboratory
Twitter Handle: @jayjaybillings

Jay Jay Billings

unread,
Feb 19, 2013, 1:50:01 PM2/19/13
to snakeya...@googlegroups.com
Andrey,

Yes, I think it would be nice. I think it might be as simple as adding an operation like yaml.loadSequenceAs(InputStream,Class) where the second argument is used to state the type of each element in the sequence generically. This would return ArrayList<T> instead of ArrayList<Map>.

Jay


--
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 http://groups.google.com/group/snakeyaml-core?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jay Jay Billings

Andrey Somov

unread,
Feb 20, 2013, 2:07:21 AM2/20/13
to snakeya...@googlegroups.com
Developing general API is more complex. How to treat the corner cases ?

What shall happen if one document is different ? Exception ? Ignore ? Call special (user provided) code ?

Andrey

Jay Jay Billings

unread,
Feb 20, 2013, 7:09:33 AM2/20/13
to snakeya...@googlegroups.com

I would go with exception...

-----
Jay Jay Billings
Computational Scientist

Oak Ridge National Laboratory
Twitter Handle: @jayjaybillings

Reply all
Reply to author
Forward
0 new messages