loading configuration file in clojure as data structure

前端 未结 5 1986
小鲜肉
小鲜肉 2020-12-24 12:10

Is there a reader function in clojure to parse clojure data structure? My use case is to read configuration properties files and one value for a property should be a list. I

5条回答
  •  鱼传尺愫
    2020-12-24 12:24

    If you want to read java-style properties files, look at Dave Ray's answer - though properties files have many limitations.

    If you are using Clojure 1.5 or later, I suggest you use edn, the extensible data notation used in Datomic - it's basically clojure data structures, with no arbitrary code execution, and the ability to add tags for things like instances or arbitrary types.

    The simplest way to use it is via read-string and slurp:

    (require 'clojure.edn)
    (clojure.edn/read-string (slurp "filename.edn"))
    

    That's it. Note that read-string only reads a single variable, so you should set up your configuration as a map:

    { :property1 ["value1" "value2"] }
    

    Then:

    (require 'clojure.edn)
    (def config (clojure.edn/read-string (slurp "config.edn")))
    (println (:property1 config))
    

    returns

    ["value1" "value2"]
    

提交回复
热议问题