Fork me on GitHub
#jvm
<
2023-07-15
>
RAJKUMAR06:07:50

I'm having issue with java version and jackson

RAJKUMAR06:07:06

I'm having behavior of metosin/jsonista 0.3.7 work differently on java 1.8 and java 11

RAJKUMAR06:07:20

below is the java code

RAJKUMAR06:07:27

(ns com.example.jsonista-issue
  (:refer-clojure :exclude [read])
  (:require
   [jsonista.core :as json]
   [java-time.api :as time])
  (:import
   (com.fasterxml.jackson.databind ObjectMapper)
   (com.fasterxml.jackson.datatype.joda JodaModule)))

(def ^ObjectMapper base-object-mapper
  (json/object-mapper {:decode-key-fn true
                       :modules       [(JodaModule.)]
                       :date-format   "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"}))

(defn generate-string [obj]
  (json/write-value-as-string obj base-object-mapper))

(defn -main [& args]
  (let [instant (time/instant)].   (println (type instant))
    (println (System/getProperty "java.version"))
    (println (System/getProperty "java.vendor"))
    (println (generate-string instant))))

RAJKUMAR06:07:48

in 1.8 it gives below output

RAJKUMAR06:07:50

1.8.0_352
Azul Systems, Inc.
"2023-07-15T06:06:22.595Z"

RAJKUMAR06:07:59

and java 11 it gives below output

RAJKUMAR06:07:34

11.0.17
Azul Systems, Inc.
"2023-07-15T06:08:48.553587Z"

RAJKUMAR06:07:46

if you see in java 11 the precision is higher

RAJKUMAR06:07:01

Any idea how to fix this?

hiredman07:07:22

Almost certainly time/instant is not returning a joda time object, but a java Instant, so the first thing to do would be to use the Jackson module for the new java date time stuff

RAJKUMAR07:07:12

okay I updated the code inline above

RAJKUMAR07:07:19

to print (println (type instant))

RAJKUMAR07:07:49

in both cases (i.e., java 8 and 11) it is printing java.time.Instant