Charred Clojure Library

repository·master·Indexed 43 days ago

https://github.com/cnuernber/charred

A high-performance, zero-dependency Clojure library for character-based file parsing optimized for CSV and JSON. It provides an API compatible with clojure.data.csv and clojure.data.json with performance comparable to Jackson or Univocity.

Tokens
606
Snippets
3
Records
5
Agent score
6%

What's inside Charred

  1. Optimize JSON performance with specialized parse and write functions

    master
    For high-frequency operations involving many small JSON objects, do not call charred/read-json repeatedly. Instead, create a specialized parse function using parse-json-fn (or a similar pathway for writing) configured with the exact options you need. These returned functions are thread-safe and significantly more efficient when passed strings or char[] data.
  2. Tune Charred for different file sizes

    master

    Charred is optimized for large files. To achieve optimal performance based on your input size, consider the following tuning strategies:

    • Large Files (> 1MB): The system is tuned for these by default.
    • Small Files/Streams (< 1MB):
      • Set :async? to false.
      • Use a smaller :bufsize (e.g., 8192).
      • For maximum performance, slurp the stream into a string and parse it in an offline threadpool.
    • General Tuning: If parsing many files of a specific size, you should gridsearch combinations of :bufsize and :async? to find the optimal configuration for your specific workload.
  3. Use the Charred API for JSON parsing and writing

    master

    Charred provides a high-performance API for reading and writing JSON, following the same interface as clojure.data.json. You can parse JSON strings into Clojure maps and write Clojure maps to JSON strings. You can also provide a :key-fn to transform keys (e.g., converting strings to keywords).

    (require '[charred.api :as charred])
    
    ;; Read JSON string into a map
    (charred/read-json "{\"a\": 1, \"b\": 2}")
    ;; => {"a" 1, "b" 2}
    
    ;; Read JSON string with keyword keys
    (charred/read-json "{\"a\": 1, \"b\": 2}" :key-fn keyword)
    ;; => {:a 1, :b 2}
    
    ;; Write a map to a JSON string
    (charred/write-json-str {:a 1 :b 2})
    ;; => "{\"a\":1,\"b\":2}"