Skip to content

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

Open
@rajcspsg

Description

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?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions