how to translate a java object to a map in clojure?

clojure, java

Solution

Have you seen the bean function? The example code above doesn't follow the JavaBean rules, but if your objects do (using getters), then bean will turn it into a Clojure map for you.

public class Person {
  private String name;
  public Person(String n) {
    this.name = n;
  }
  public String getName() {
    return name;
  }
}

Use like this:

(def p (Person. "xyz"))
(bean p)
;=> {:name "xyz" :class Person}

Problem

For example: ``` class Person { String name; } ``` I would like to get `{:name "xxx}` from an instance of Person. I know `(from_json (.toJson person))` can do it. Is there any better way to achieve it?

Original source