RussellSpitzer / snakeyaml

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

org.yaml.snakeyaml.Yaml#loadFromReader do not work in Grails application #196

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This issue is related to jvm's class loader system.
I wanted to deserialize a yaml document, so I coded like this: 
yaml.loadAs(string, User.class)
then this code throwed an ClassNotFoundException:  class not found :User
I traced the code and finnally found the exception was rased here:
org.yaml.snakeyaml.constructor.Constructor#getClassForName (line 646)

That's the problem, because User.class was load from Grails's URL class loader, 
which is not the same as Yaml's. so Yaml 's class loader cannot find or load 
User.class with just a single class name.

Mybe the org.yaml.snakeyaml.constructor.Constructor#getClassForName is not 
needed, because the class was already specificed at the second parameter of 
Yaml.loadAs.
My Yaml version is 1.13 , and jdk 1.7

Original issue reported on code.google.com by lshd...@gmail.com on 12 Jul 2014 at 4:59

GoogleCodeExporter commented 9 years ago
1) please study the way how the class loading works
2) take a look at CustomClassLoaderConstructor
Basically, you need to instruct your JVM how to load the class definition. It 
is not related to SnakeYAML. The class loader which is used with SnakeYAML must 
be able to find your class (exactly as the Grails's URL class loader does)  

Original comment by py4fun@gmail.com on 14 Jul 2014 at 11:14