ruedigergad / bowerick

Easing simple Message-oriented Middleware (MoM) Tasks with Clojure (and Java)
Eclipse Public License 1.0
26 stars 1 forks source link

Not handling clojure ratio correctly? #2

Open tsaixingwei opened 4 years ago

tsaixingwei commented 4 years ago

Clojure ratios are being converted to floating points when sent to and consumed from a JMS queue.

See this simple test code where the ratio 24/7 is converted to 3.428571428571429 when consumed.

(ns jms.test_jms
  (:require [bowerick.jms :as j]))

(def url "tcp://127.0.0.1:61616")

(def destination "/topic/my.test.topic")

(def consumer (j/create-json-consumer
                url
                destination
                (fn [data] (println "Received:" data))))

(def producer (j/create-json-producer url destination))

(producer "foo")
;;=> nil
;Received: foo

(producer '(1 7 0 1))
;;=> nil
;Received: (1 7 0 1)

(producer #{true false #inst"2008-05-10T00:00:00.000-00:00" 33.33M})
;;=> nil
;Received: (true false 2008-05-10T00:00:00Z 33.33)

;; Warning: Note that it does not handle ratios well converting it to floating point instead.
(producer 24/7)
;;=> nil
;Received: 3.428571428571429

(j/close producer)
;;Closing producer: tcp://127.0.0.1:61616 /topic/my.test.topic

(j/close consumer)
;;Closing consumer: tcp://127.0.0.1:61616 /topic/my.test.topic
ruedigergad commented 4 years ago

Hi,

thanks a lot for the feedback and sorry for the delayed answer.

bowerick uses Cheshire for JSON serialization. It seem that Cheshire defaults to converting Ratios to floating point (double) values: https://github.com/dakrone/cheshire/blob/4525b23da1c17decba363202402a8a195d21705f/test/cheshire/test/core.clj#L20

Could you add a bit more context for your use case? Do you know which field is expected to be of Ratio type? Could you, e.g., convert the ratio to a string yourself and use (read-string "2/3") for reading it back?

Cheers, Ruediger

tsaixingwei commented 4 years ago

No particular use case as yet. I was just testing the library to see the extent of its functionality. As you have suggested, I could have it converted to a string and read it back into a Ratio type. But that is assuming I control both the consumer and producer.

Not a big issue though. Shall I leave this issue open until it is handled by Cheshire itself?