Using Java objects as clojure mappings
•
Java
I have a Java class that I want to use in clojure But I want to use it as a clojure map What steps are required to do this?
I've seen the code for ipersistentmap – does the Java class implement this? Or should there be some clojure code to implement a protocol?
I know I can write some mapping code to explicitly convert code from Java objects to maps, but the solution has a high effort / return Besides, I may encounter the same situation again
Specific example: I have a parser written in Java I want to use it to parse some text and then access the content of the parsed data structure, just as it is in clojure map:
(def parser (new MyParser)) (let [parse-tree (parser ... parse some text ...)] ((parse-tree :items) "itemid"))
Solution
The function bean thought of:
Examples from the web site:
user=> (import java.util.Date) java.util.Date user=> (def *Now* (Date.)) #'user/*Now* user=> (bean *Now*) {:seconds 57,:date 13,:class java.util.Date,:minutes 55,:hours 17,:year 110,:timezoneOffset -330,:month 6,:day 2,:time 1279023957492}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码