Hi all,
I'm doing a little YAML-parsing where I have CustomObjects having Lists
of other CustomObjects as properties, but the default map constructor
does not seem to work for these lists.
example:
class Sample {
Integer age
String name
}
class SampleCollection {
String name
List<Sample> listOfSamples
}
def m = [name:'Collection1',listOfSamples:[
[age:10,name:'Anna'],[age:12,name:'Berta'] ]]
def c = m as SampleCollection // or c = new SampleCollection(m)
assert c.class == SampleCollection // passes
assert c.listOfSamples[0].class == Sample // fails
First solution I've come up with is adding a custom constructor to
SampleCollection like this:
SampleCollection(Map map) {
name = map.name
// listOfSamples = map.listOfSamples // cast not working
listOfSamples = []
map.listOfSamples.each{ sample ->
listOfSamples << (sample as Sample)
}
}
This works, but seems rather inefficient, as
a) I have to loop through each list with potentially many items and
b) SampleCollection has some more simple properties like 'name', which I
don't want to add all manually. Is there a way to use the default for
these simple properties?
regards
Paul
--
typed with Neo 2 -- an ergonomically optimized keyboard layout
for German, English, programming, and science
http://neo-layout.org/