3

I have many java classes, which can contain another class

public class SomeClass(){
    private String key;
    private String value;
    private AnotherClass anotherClass;
}

public class AnotherClass(){
    private String anotherkey;
    private String anothervalue;
}

How can convert SomeClass to json in clojure? Already tried to use cheshire, clojure/data.json, but unsuccessfully

4 Answers 4

3

I have renamed your example classes to make it more concrete:

public class Person {
  String  name;
  Address address;
}

public class Address {
  String street;
  String city;
}

Here is an outline of what to do. You simply define a conversion function for each class, and then compose them hierarchically:

(defn address->clj [address]
  {:street  (.-street address)
    city    (.-city address)})

(defn person->clj [person]
  {:name     (.-name person)
    address  (address->clj (.-address person))})

Consider some sample data (NOTE: constructor details omitted)

(def addr-1    (Address. "123 Main St" "Anytown"))
(def person-1  (Person.  "Joe Smith" addr-1))

(someClass->clj  person-1)    ; Convert Java person-1 obj

You will get a Clojure map back that looks like:

{:name      "Joe Smith"
 :address   {:street "123 Main St" 
             :city   "Anytown"}}

Update

If you are working with a Java bean, please see also:

Sign up to request clarification or add additional context in comments.

2 Comments

@ykembayev I don't understand people who come here and ask questions, and get detailed answers like this which no doubt took time to compile, and don't even bother to upvote the answer.
@Josh Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.
2

Thanks everybody. Problem was solved by clj-gson

Comments

1

I am not sure, I understand your question correctly. If you would like to convert java bean to clojure data structure, you could simply use clojure bean API. Using postwalk, you could also convert all inner part too. Find an example in bellow

  (let [ao (doto (AnotherClass.)
                 (.setAnotherkey "Ano")
                 (.setAnothervalue "Ano-value"))
    o (doto  (SomeClass.)
             (.setKey "A")
             (.setValue "A-value")
             (.setAnotherClass ao))]
   (->> (dissoc (bean o) :class)
         (w/postwalk (fn [v]
                    (if (contains? #{AnotherClass SomeClass} (type v))
                     (dissoc (bean v) :class)
                     v)))))

;;Output {:anotherClass {:anotherkey "Ano", :anothervalue "Ano-value"}, :key "A", :value "A-value"}

1 Comment

thanks, I searched for universal method do it. Problem was solved by clj-gson
1

With clojure.data.json you can extend writer protocol when necessary. E.g.:

(require 'clojure.data.json)
(extend-type java.util.UUID
  clojure.data.json/JSONWriter
  (-write [object ^PrintWriter out]
     (.print out (str " something "))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.