How can I use clojure.core / bean recursively?

So, I think clojure.core / bean is pretty close to what I want, but I am working with a Java application embedded in beans, so I get these cards:

{:month-total 3835.0 :name "Jan Meat Diner" :owners #<BarOwner[] [Lcom.fancypants.BarOwner;@1fb332d} 

Like, I call a bean recursively on a Java object so that I can make my imaginary BarOwner object also emit itself as a map:

 {:month-total 3835.0 :name "Jan Meat Diner" :owners { [:name "Jack"] [:name "Jill"] } } 

Change 1

I found that clojure/java.data and from-java are probably better for this kind of thing than bean .

+6
source share
2 answers

Although this is probably not the perfect answer to “how to use a bean recursively,” using more of the richer Contrib libraries within the Clojure community has allowed it. Specifically

clojure / java.data

provides a simple recursive bean solution and can be configured to handle java types specifically for hairy cases. I recommend this to other people who want to use a bean .

+4
source

It is very difficult to know what a bean is and what is not. This seems to be a trick for beans inside beans and properties that are lists. You probably want to add additional classes to the probably-bean? function probably-bean? and perhaps some support for properties that are maps.

 (defn probably-bean? [o] (and (not (coll? o)) ((complement #{Class Long String clojure.lang.Keyword}) (class o)))) (defn transf [o] (cond (instance? java.util.List o) (into [] (map transf o)) (probably-bean? o) (into {} (seq (bean o))) :else o)) (defn to-bean [o] (clojure.walk/prewalk #(transf %) o)) 
0
source

Source: https://habr.com/ru/post/926551/


All Articles