CrossClj

0.2.1 docs

SourceDocs



RECENT

    metosin/jsonista

    Clojars

    May 23, 2018


    OWNER
    Metosin
    Tampere, Helsinki, Finland
    www.metosin.fi

    Readme

    Index of all namespaces


    « Project + dependencies

    Clojure library for fast JSON encoding and decoding.

    jsonista.coreDocsSource
    JSON encoding and decoding based on Jackson Databind.
    
    Encoding example:
    
        (require '[jsonista.core :as json])
        (json/write-value-as-string {:hello 1})
        ;; => "{\"hello\":1}"
    
    Decoding example:
    
        (def +data+ (json/write-value-as-string {:foo "bar"}))
        (json/read-value +data+)
        ;; => {"foo" "bar"}
    
    ## Configuration
    
    You can configure encoding and decoding by creating a custom mapper object
    with jsonista.core/object-mapper. The options are passed in as a map.
    
    For example, to convert map keys into keywords while decoding:
    
        (json/from-json +data+ (json/object-mapper {:decode-key-fn true}))
        ;; => {:foo "bar"}
    
    See the docstring of [[object-mapper]] for all available options.
    
    ## Custom encoders
    
    Custom encoder is a function that take a value and a JsonGenerator object as
    the parameters. The function should call JsonGenerator methods to emit the
    desired JSON. This is the same as how custom encoders work in Cheshire.
    
    Custom encoders are configured by the object-mapper option :encoders, which is a
    map from types to encoder functions.
    
    For example, to encode java.awt.Color:
    
         (let [encoders {java.awt.Color (fn [color gen] (.writeString gen (str color)))}
               mapper (json/object-mapper {:encoders encoders})]
           (json/write-value-as-string (java.awt.Color. 1 2 3) mapper))
         ;; => "\"java.awt.Color[r=1,g=2,b=3]\""
    
    ## Jsonista vs. Cheshire
    
    jsonista uses Jackson Databind while Cheshire uses Jackson Core. In our
    benchmarks, jsonista performs better than Cheshire (take look at
    json_perf_test.clj). On the other hand, Cheshire has a wider set of features
    and has been used in production much more.
    The README below is fetched from the published project artifact. Some relative links may be broken.

    jsonista Continuous Integration status

    jsonissa / jsonista / jsoniin, jsonilla / jsonilta / jsonille

    Clojure library for fast JSON encoding and decoding.

    Much faster than Cheshire while still having all the necessary features for web development. Designed for use with Muuntaja.

    Blogged: * Faster JSON processing with jsonista

    Latest version

    Clojars Project

    Requires Java1.8+

    Quickstart

    (require '[jsonista.core :as j])
    
    (j/write-value-as-string {"hello" 1})
    ;; => "{\"hello\":1}"
    
    (j/read-value *1)
    ;; => {"hello" 1}
    

    Examples

    Changing how map keys are encoded & decoded:

    (defn reverse-string [s] (apply str (reverse s)))
    
    (def mapper
      (j/object-mapper
        {:encode-key-fn (comp reverse-string name)
         :decode-key-fn (comp keyword reverse-string)}))
    
    (-> {:kikka "kukka"}
        (doto prn)
        (j/write-value-as-string mapper)
        (doto prn)
        (j/read-value mapper)
        (prn))
    ; {:kikka "kukka"}
    ; "{\"akkik\":\"kukka\"}"
    ; {:kikka "kukka"}
    

    Reading & writing directly into a file:

    (def file (java.io.File. "hello.json"))
    
    (j/write-value file {"hello" "world"})
    
    (slurp file)
    ;; => "{\"hello\":\"world\"}"
    
    (j/read-value file)
    ;; => {"hello" "world"}
    

    Adding support for joda-time Classes, used by clj-time.

    ;; [com.fasterxml.jackson.datatype/jackson-datatype-joda "2.9.5"]
    (import '[com.fasterxml.jackson.datatype.joda JodaModule])
    (import '[org.joda.time LocalDate])
    
    (def mapper
      (j/object-mapper
        {:modules [(JodaModule.)]}))
    
    (j/write-value-as-string (LocalDate. 0) mapper)
    ; "\"1970-01-01\""
    

    Performance

    • All standard encoders and decoders are written in Java
    • Protocol dispatch with read-value & write-value
    • Jackson ObjectMapper is used directly
    • Small functions to support JVM Inlining

    See perf-tests for details.

    encode

    decode

    License

    Copyright © 2016-2018 Metosin Oy.

    Distributed under the Eclipse Public License, the same as Clojure.