从Data.json转换为柴郡



我是Clojure的新手。有时我仍然在阅读功能方面很难。

我正在尝试更改此功能以使用CheckShire。这是我的尝试:

defn- json->messages [json]
(let [records          (:amazon.aws.sqs/records (cheshire/decode
                                                json
                                                :key-fn key-reader
                                                :value-fn value-reader))
    add-origin-queue (fn [record]
                       (let [event-source-arn (:amazon.aws.sqs/event-source-arn record)
                             queue-name       (arn->queue-name event-source-arn)]
                         (assoc record :amazon.aws.sqs/queue-name queue-name)))]
(map add-origin-queue records)))

功能键读取器函数:

(def ^:private
key-reader
(memoize (fn [key]
         (let [kebab-key (if (= "md5OfBody" key)
                           "md5-of-body"
                           (csk/->kebab-case key))]
           (keyword "amazon.aws.sqs" kebab-key)))))

功能:

(def ^:private
value-reader
(memoize (fn [key value]
         (if (= key :amazon.aws.sqs/receipt-handle)
           value-reader
           value))))

i比这样称呼该功能:

(JSON->消息MSG(

msg是一个JSON字符串。

但是,我在下面的尝试下遇到错误:

tech.matterindustries.titan.ion.lambda.sqs-receive/json-> messages(sqs_receive.clj:36(上的tech.matterustries.titan.ion.lambda.sqs-sqs-sqs-sqs-sqs-sqs rambda:36(上的执行错误(arityException(。 错误的Args(5(传递给:cheshire.core/parse-smile

您将错误的ARG数发送到cheshire.core/parse-smile。您有示例数据吗?

也请保持您的代码清洁&格式化,这样:

(defn- json->messages
  [json]
  (let [records          (:amazon.aws.sqs/records (cheshire/decode json :key-fn key-reader :value-fn value-reader))
        add-origin-queue (fn [record]
                           (let [event-source-arn (:amazon.aws.sqs/event-source-arn record)
                                 queue-name       (arn->queue-name event-source-arn)]
                             (assoc record :amazon.aws.sqs/queue-name queue-name)))]
    (map add-origin-queue records)))

我在柴郡文档中找不到decode,但是在来源中有:

(def decode "Alias to parse-string for clojure-json users" parse-string)

我对他们不完整的文档感到失望。

快速的Google显示了文档:

(parse-string string & [key-fn array-coerce-fn])
Returns the Clojure object corresponding to the given JSON-encoded string.
An optional key-fn argument can be either true (to coerce keys to keywords),
false to leave them as strings, or a function to provide custom coercion.
The array-coerce-fn is an optional function taking the name of an array field,
and returning the collection to be used for array values.

这可能不清楚。这意味着有3种拨打parse-string的合法方法:

(parse-string  <json-str>)
(parse-string  <json-str>  <key-fn>)
(parse-string  <json-str>  <key-fn>  <array-coerce-fn>)

因此,您可以使用1、2或3个args调用它。您不能在示例中添加:key-fn:value-fn地图键。

还请注意,您的key-readervalue-reader看起来像它们不匹配Cheshire/Read-string所期望的

相关内容

  • 没有找到相关文章

最新更新