Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to apply key-decode-fn only to top level keys #72

Open
rajcspsg opened this issue Jul 21, 2023 · 0 comments
Open

how to apply key-decode-fn only to top level keys #72

rajcspsg opened this issue Jul 21, 2023 · 0 comments

Comments

@rajcspsg
Copy link

Hi,

I've a requirement where I can decode only keys.

consider the below example -

(require '[jsonista.core :as json])
(import
 (com.fasterxml.jackson.core JsonFactory$Feature)
 (com.fasterxml.jackson.databind ObjectMapper)
 (com.fasterxml.jackson.datatype.joda JodaModule))

(def s "{\"name\":\"person1\",\"book\":\"Demo Book\",\"hash\":{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}}")

(def ^ObjectMapper object-mapper
  (let [obj-mapper (json/object-mapper {:decode-key-fn true :modules [(JodaModule.)]})]
    (doto obj-mapper
      (-> .getFactory (.disable JsonFactory$Feature/USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING)))
    obj-mapper))

(json/read-value s object-mapper)

The output I would like to get is

{:book "Demo Book", :hash {"key3" "value3", "key2" "value2", "key1" "value1"}, "name" "person1"}

In the json above, the top level is converted to keys and nested are not converted to keys but kept as string.

:decode-key-fn true make all keys and without :decode-key-fn true all keys are strings which is not I want. How can I achieve the same?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant