Map of Map of Map of Java Bean could not be parsed as Java Bean

606 views
Skip to first unread message

Demeter Sztanko

unread,
Jun 30, 2014, 5:52:15 PM6/30/14
to snakeya...@googlegroups.com
Hello all,

First of all, thank you for this amazing library. It is being extremely useful for me!

I have recently stuck with a problem and I have a suspicion it could be a bug.

All the sources can be accessed from here: https://gist.github.com/sztanko/229f788b7bb7b70543ca in case you find it difficult to read it from here.

I have the following yaml file:
mapping:
    nature:
        wetland:
            buffer: [5.0]
            mappings:
                natural: [wetland, mud,]
        meadow:
            buffer: [5.0]
            mappings:
                landuse: [grass, meadow]
                natural: [fell, grassland]
    landmark:
        castle:
            buffer: [-5.0, 30.0, 50.0]
            mappings:
                bridge: [drawbridge]
                historic: [castle]

and the following mapping for that:

public static class TagMapping {
public static class Mapping {
public List<Double> buffer;
public Map<String, List<String>> mappings;
}
public Map<String, Map<String, Mapping>> mapping;
}
 


When I try to process the file by running the following code:

package snaketest;
import java.util.List;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
public class SnakeTest {
 
public static class TagMapping {
public static class Mapping {
public List<Double> buffer;
public Map<String, List<String>> mappings;
}
public Map<String, Map<String, Mapping>> mapping;
}
 
public static TagMapping parse(String str) {
Yaml yaml = new Yaml(new Constructor(TagMapping.class));
TagMapping mapping = (TagMapping) yaml.load(str);
return mapping;
}
public  static void main(String... args)
{
String testString
"mapping:\n"+
"    nature:\n"+
"        wetland:\n"+
"            buffer: [5.0]\n"+
"            mappings:\n"+
"                natural: [wetland, mud]\n"+
"        meadow:\n"+
"            buffer: [5.0]\n"+
"            mappings:\n"+
"                landuse: [grass, meadow]\n"+
"                natural: [fell, grassland]\n"+
"    landmark:\n"+
"        castle:\n"+
"            buffer: [-5.0, 30.0, 50.0]\n"+
"            mappings:\n"+
"                bridge: [drawbridge]\n"+
"                historic: [castle]";
TagMapping m = parse(testString);
System.out.println(m.mapping.get("nature").get("wetland").mappings.get("natural").get(0));
}
}


It fails with the following error:
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to snakefail.SnakeTest$TagMapping$Mapping
at snakefail.SnakeTest.main(SnakeTest.java:51)


It seems like instead of having Map<String,Map<String,Mapping>> it is creating a Map<String,Map<String,Map>>. Do you know what could be the easiest workaround?


Thank you very much,

Dimi.

Andrey

unread,
Jul 1, 2014, 10:19:41 AM7/1/14
to snakeya...@googlegroups.com
Hi Dimi,
due to erasure we do not get the types of the data inside collections.
Please check the usage of TypeDescription in the tests to find your way.
Basically, you have to explicitly define what are the keys and values of your Mapping.

Cheers,
Andrey

Demeter Sztanko

unread,
Jul 2, 2014, 2:12:46 PM7/2/14
to snakeya...@googlegroups.com
Dear Andrey,

thank you very much for your answer. The problem is that I simply don't see any way of applying TypeDescription in this case. I imagine it could be easy to putMapPropertyType for the specific property but I don't know how it is possible to tell snakeyaml that a given property is a map of maps and that every element of that nested map should be a given object.

If you have an example of a nested map, please let me know. I did not find anything that could help me in solving.


--
You received this message because you are subscribed to a topic in the Google Groups "SnakeYAML" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/snakeyaml-core/H3CLnxl3DQI/unsubscribe.
To unsubscribe from this group and all its topics, 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.
For more options, visit https://groups.google.com/d/optout.

Andrey

unread,
Jul 3, 2014, 3:33:05 AM7/3/14
to snakeya...@googlegroups.com
Indeed.
This feature may be is not present.
Feel free to propose the API and implement it.

Cheers,
Andrey


Paul Hollingsworth

unread,
Jul 3, 2014, 4:08:07 AM7/3/14
to snakeya...@googlegroups.com
Somehow the Google Json parser *is* able to infer the complete types of parameterized collections: https://code.google.com/p/google-gson/

I recommend experimenting with it to see how they did it - maybe whatever technique they are using could also be applied to Snake yaml.

Paul

Sent from my iPhone
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.

Andrey

unread,
Jul 3, 2014, 4:16:16 AM7/3/14
to snakeya...@googlegroups.com
Thank you Paul.
It may be indeed very useful.

Andrey

Reply all
Reply to author
Forward
0 new messages