Groovy has a neat syntax for hydrating a POGO with a Map, like:
class Person {
Address address
}
class Address {
String city
}
Person p = new Person([address: [city: 'Toronto']])
assert p.address.city == 'Toronto'
Even a deeply nested model works! I've tried doing so with an @Immutable model, to no avail:
@groovy.transform.Immutable
class Person {
Address address
}
@groovy.transform.Immutable
class Address {
String city
}
//works:
Address a = new Address('Toronto')
Person p = new Person(a)
assert p.address.city == 'Toronto'
//not works:
Person p = new Person([address: [city: 'Toronto']])
// ==> java.lang.RuntimeException: @Immutable processor doesn't know how to handle field 'address' of type 'java.util.LinkedHashMap' while constructing class Person.
Doing such is particularly awesome going from JSON -> Map -> POGO.
Any ideas how?