1

I am trying to convert JSON file to thrift binary format to save storage and network bandwidth. this is irrelevant to my question. But want to describe its context.

So let say, I have a map from json, and Java Class Event

(def m 
 {"a" 1 
  "b" 2})

java class

package my-test;

class Event {
   public String a;
   public String b;
   public void setA(String a) { this.a = a; }
   public void setB(String b) { this.b = b; }
}

How can I get a Event object with value m?


P.S.

tried to use call-method* mentioned in https://groups.google.com/forum/#!topic/clojure/YJNRnGXLr2I

(defn call-method* [obj m & args]
  (eval `(. ~obj ~(symbol m) ~@args)))

but Can't embed object in code, maybe print-dup not defined raised. I have no idea why.

=> (def o (Event.))
=> (call-method* o "setA" "1")
CompilerException java.lang.RuntimeException: Can't embed object in code,  
maybe print-dup not defined: mytest.Event@681d891, compiling:   
 (/private/var/folders/hd/xxl2lfmn5wggspxwwtk2r2zr0000gn/T/form-init2883112335729429073.clj:1:1)

P.S.

This Question Dynamic method calls in a Clojure macro? is more appropriate to read.

1
  • About thrift, I tried to set instance of thrift generated java class with clojure.java.data. It didn't worked with clojure.java.data/to-java because thrift generated java class is not a bean by default. thrift --gen java:beans a.thrift worked for me. Commented Jan 15, 2016 at 5:57

1 Answer 1

1

clojure.java.data/to-java worked.

(import 'mytest.Event)
(use 'clojure.java.data)
(def m {"a" "1"})
(println (bean (to-java Event m)))
Sign up to request clarification or add additional context in comments.

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.