Hi Currently I am using java.data (https://github.com/clojure/java.data) to convert java pojos to clojure compatible types. It does not work for nested objects.
For ex:
class Abc {
Map<String, Def> someMap;
}
Class Def {
String b;
}
If I pass sample instance of Abc to java.data, I get the output as:
{
:someMap {
"keyString" #object[com.sample.Def 0xb33584d "com.sample.Def@b33584d"]
}
}
But I want the output as:
{
:someMap {
"keyString" {
"b" "value"
}
}
}
How can I fix this?
I tried clojure.core bean (https://clojuredocs.org/clojure.core/bean) and it dint seem to work as well.
Thank you in advance.
In order for this to work, your Java objects need to conform to the JavaBean specification. This means they need methods
.getXXX()
to read object properties (at least), and also.setXXX()
to construct a new object. Example:Class
Inner
:Class
Outer
:and Clojure code to decode the nested JavaBean objects:
The results show what happens:
The raw
java-obj
is opaque to Clojure. Usingjd/from-java
only unpacks the outer layer using the JavaBean getters. Usingjd/from-java-deep
(notice the required options map, left empty here) will recursively unpack the JavaBean using the appropriate getters on each object based on its java class.All of the above code is based on this template project. Enjoy!