Commit 6caa40dc authored by Ken Barber's avatar Ken Barber

Make com.puppetlabs.cheshire a proxy for cheshire.core common functions

This is so we can provide default encoders at least, and in the future we may
wish to wrap this functionality.

Tests included, and all usage of cheshire.core has now been cutover to
com.puppetlabs.cheshire.
Signed-off-by: default avatarKen Barber <ken@bob.sh>
parent b05989fd
(ns ^{:doc "Cheshire related functions
This front-ends the common set of core cheshire functions:
* generate-string
* generate-stream
* parse-string
* parse-stream
This namespace when 'required' will also setup some common JSON encoders
globally, so you can avoid doing this for each call."}
com.puppetlabs.cheshire
(:require [cheshire.generate :as generate]
[cheshire.core :as core]
[clj-time.coerce :as coerce]))
(defn add-common-json-encoders!*
......@@ -17,6 +25,7 @@
(def
^{:doc "Registers some common encoders for cheshire JSON encoding.
This is a memoize function, to avoid unnecessary calls to add-encoder.
Ideally this function should be called once in your apply, for example your
......@@ -28,3 +37,11 @@
add-common-json-encoders! (memoize add-common-json-encoders!*))
(add-common-json-encoders!)
(def generate-string core/generate-string)
(def generate-stream core/generate-stream)
(def parse-string core/parse-string)
(def parse-stream core/parse-stream)
......@@ -8,8 +8,7 @@
[java.io IOException Writer])
(:require [ring.util.response :as rr]
[ring.util.io :as rio]
[com.puppetlabs.cheshire]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[clojure.reflect :as r]
......
......@@ -4,8 +4,7 @@
(:import [org.apache.activemq.broker BrokerService]
[org.apache.activemq ScheduledMessage]
[org.apache.activemq.usage SystemUsage])
(:require [com.puppetlabs.cheshire]
[cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[clamq.activemq :as activemq]
[clamq.protocol.connection :as mq-conn]
[clamq.protocol.consumer :as mq-consumer]
......
......@@ -91,7 +91,7 @@
(:require [clojure.string :as string]
[clojure.tools.logging :as log]
[clojure.set :as set]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[digest]
[com.puppetlabs.utils :as pl-utils])
(:use [clojure.core.match :only [match]]))
......
......@@ -4,8 +4,7 @@
[com.puppetlabs.puppetdb.cli.import :only [parse-metadata]])
(:import [com.puppetlabs.archive TarGzReader TarGzWriter]
[org.apache.commons.compress.archivers.tar TarArchiveEntry])
(:require [com.puppetlabs.cheshire]
[cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[clojure.java.io :as io]
[clojure.string :as string]
[com.puppetlabs.archive :as archive]
......
......@@ -43,8 +43,7 @@
[com.puppetlabs.puppetdb.catalog.utils :as catutils]
[com.puppetlabs.puppetdb.command :as command]
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.cheshire]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[clj-http.client :as client]
[clj-http.util :as util]
[fs.core :as fs])
......
......@@ -13,8 +13,7 @@
[com.puppetlabs.concurrent :only [bounded-pmap]]
[clj-http.util :only [url-encode]]
[com.puppetlabs.puppetdb.catalogs :only [catalog-version]])
(:require [com.puppetlabs.cheshire]
[cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[fs.core :as fs]
[clojure.java.io :as io]
[clj-http.client :as client]
......
......@@ -9,7 +9,7 @@
[com.puppetlabs.puppetdb.command :as command]
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.archive :as archive]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[clojure.java.io :as io])
(:import [com.puppetlabs.archive TarGzReader]
[org.apache.commons.compress.archivers.tar TarArchiveEntry])
......
......@@ -55,8 +55,7 @@
[clojure.java.jdbc :as sql]
[clojure.string :as string]
[clojure.tools.logging :as log]
[com.puppetlabs.cheshire]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.puppetdb.http.server :as server]
[com.puppetlabs.ssl :as ssl])
(:use [clojure.java.io :only [file]]
......
......@@ -10,7 +10,7 @@
;; by the property value.
(ns com.puppetlabs.puppetdb.cli.version
(:require [cheshire.core :as json])
(:require [com.puppetlabs.cheshire :as json])
(:use [com.puppetlabs.puppetdb.version :only [version]]
[com.puppetlabs.puppetdb.scf.migrate :only [desired-schema-version]]))
......
......@@ -65,8 +65,7 @@
[com.puppetlabs.mq :as mq]
[com.puppetlabs.utils :as pl-utils]
[clj-http.client :as client]
[com.puppetlabs.cheshire]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[clamq.protocol.consumer :as mq-cons]
[clamq.protocol.producer :as mq-producer]
[clamq.protocol.connection :as mq-conn])
......
......@@ -4,8 +4,7 @@
[com.puppetlabs.archive :as archive]
[com.puppetlabs.utils :as pl-utils]
[clj-time.format :as time-format]
[com.puppetlabs.cheshire]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[fs.core :as fs])
(:use [clojure.java.io :only [file make-parents]]
[clj-time.core :only [ago after?]]
......
......@@ -12,8 +12,7 @@
;; `[arg1 arg2 arg3]`.
(ns com.puppetlabs.puppetdb.core
(:require [com.puppetlabs.cheshire]
[com.puppetlabs.utils :as utils]
(:require [com.puppetlabs.utils :as utils]
[clojure.tools.namespace :as ns])
(:use [clojure.string :only (split)])
(:gen-class))
......
(ns com.puppetlabs.puppetdb.http.experimental.planetarium-catalog
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.planetarium-catalog :as c]
[ring.util.response :as rr])
......
......@@ -4,8 +4,7 @@
;; of PuppetDB queries embedded in HTTP parameters.
;;
(ns com.puppetlabs.puppetdb.http.query
(:require [com.puppetlabs.cheshire]
[cheshire.core :as json]))
(:require [com.puppetlabs.cheshire :as json]))
(defn- are-queries-different?
[req1 req2]
......
......@@ -12,7 +12,7 @@
;; If no facts are known for the node, the response is a 404 with an error message.
(ns com.puppetlabs.puppetdb.http.v1.facts
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.facts :as f]
[ring.util.response :as rr])
......
......@@ -25,7 +25,7 @@
[clojure.pprint :as pp]
[clojure.tools.logging :as log]
[clojure.string :as s]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.http :as pl-http]
[ring.util.response :as rr])
(:use com.puppetlabs.middleware
......
......@@ -29,7 +29,7 @@
;; `["foo.example.com", "bar.example.com", "baz.example.com"]`
;;
(ns com.puppetlabs.puppetdb.http.v1.node
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.nodes :as node]
[ring.util.response :as rr])
......
......@@ -68,7 +68,7 @@
(ns com.puppetlabs.puppetdb.http.v1.resources
(:require [com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.resources :as r]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[ring.util.response :as rr])
(:use [net.cgrand.moustache :only [app]]
com.puppetlabs.middleware
......
......@@ -3,7 +3,7 @@
[com.puppetlabs.puppetdb.query.resources :as r]
[com.puppetlabs.puppetdb.http.query :as http-q]
[ring.util.response :as rr]
[cheshire.core :as json])
[com.puppetlabs.cheshire :as json])
(:use [net.cgrand.moustache :only [app]]
[com.puppetlabs.middleware :only (verify-accepts-json validate-query-params)]
[com.puppetlabs.jdbc :only (with-transacted-connection get-result-count)]
......
(ns com.puppetlabs.puppetdb.http.v3.aggregate-event-counts
(:require [com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.aggregate-event-counts :as aggregate-event-counts]
[cheshire.core :as json])
[com.puppetlabs.cheshire :as json])
(:use [com.puppetlabs.jdbc :only (with-transacted-connection)]
[com.puppetlabs.middleware :only [verify-accepts-json validate-query-params]]
[net.cgrand.moustache :only [app]]))
......
(ns com.puppetlabs.puppetdb.http.v3.catalogs
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.catalogs :as c]
[ring.util.response :as rr])
......
(ns com.puppetlabs.puppetdb.http.v3.event-counts
(:require [com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.event-counts :as event-counts]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.puppetdb.query.paging :as paging])
(:use [com.puppetlabs.jdbc :only (with-transacted-connection)]
[com.puppetlabs.middleware :only [verify-accepts-json validate-query-params wrap-with-paging-options]]
......
......@@ -2,7 +2,7 @@
(:require [com.puppetlabs.http :as pl-http]
[com.puppetlabs.utils :as pl-utils]
[com.puppetlabs.puppetdb.query.events :as query]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[ring.util.response :as rr]
[com.puppetlabs.puppetdb.query.paging :as paging])
(:use [net.cgrand.moustache :only [app]]
......
......@@ -2,7 +2,7 @@
(:require [com.puppetlabs.puppetdb.query.facts :as f]
[com.puppetlabs.puppetdb.http.query :as http-q]
[com.puppetlabs.http :as pl-http]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.puppetdb.query.paging :as paging])
(:use [net.cgrand.moustache :only [app]]
com.puppetlabs.middleware
......
(ns com.puppetlabs.puppetdb.http.v3.nodes
(:require [com.puppetlabs.puppetdb.query.paging :as paging]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.puppetdb.query.nodes :as node]
[com.puppetlabs.puppetdb.http.v2.facts :as f]
[com.puppetlabs.puppetdb.http.v2.resources :as r]
......
......@@ -2,7 +2,7 @@
(:require [com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.reports :as query]
[ring.util.response :as rr]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.puppetdb.query.paging :as paging])
(:use [net.cgrand.moustache :only [app]]
com.puppetlabs.middleware
......
......@@ -4,7 +4,7 @@
[com.puppetlabs.http :as pl-http]
[com.puppetlabs.puppetdb.query.resources :as r]
[ring.util.response :as rr]
[cheshire.core :as json])
[com.puppetlabs.cheshire :as json])
(:use [net.cgrand.moustache :only [app]]
[com.puppetlabs.middleware :only (verify-accepts-json validate-query-params wrap-with-paging-options)]
[com.puppetlabs.jdbc :only (with-transacted-connection get-result-count)]
......
......@@ -3,7 +3,7 @@
(ns com.puppetlabs.puppetdb.query.events
(:require [com.puppetlabs.utils :as utils]
[clojure.string :as string]
[cheshire.core :as json])
[com.puppetlabs.cheshire :as json])
(:use [com.puppetlabs.jdbc :only [underscores->dashes dashes->underscores valid-jdbc-query? add-limit-clause]]
[com.puppetlabs.puppetdb.scf.storage :only [db-serialize sql-regexp-match]]
[com.puppetlabs.puppetdb.query :only [compile-term compile-and compile-or compile-not-v2 execute-query]]
......
......@@ -5,7 +5,7 @@
(ns com.puppetlabs.puppetdb.query.paging
(:import [com.fasterxml.jackson.core JsonParseException])
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[clojure.string :as string])
(:use [com.puppetlabs.utils :only [keyset seq-contains? parse-int order-by-expr?]]
[com.puppetlabs.jdbc :only [underscores->dashes]]
......
......@@ -5,7 +5,7 @@
;; right format and picking out the desired columns.
;;
(ns com.puppetlabs.puppetdb.query.resources
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[clojure.string :as string]
[com.puppetlabs.utils :as utils])
(:use [com.puppetlabs.jdbc :only [limited-query-to-vec
......
......@@ -7,7 +7,7 @@
(:use [clj-time.coerce :only [to-timestamp]]
[com.puppetlabs.validation :only [defmodel validate-against-model!]]
[com.puppetlabs.puppetdb.command.constants :only [command-names]])
(:require [cheshire.core :as json]
(:require [com.puppetlabs.cheshire :as json]
[com.puppetlabs.utils :as utils]
[clojure.string :as s]))
......
......@@ -51,7 +51,7 @@
(:require [clojure.java.jdbc :as sql]
[clojure.tools.logging :as log]
[clojure.string :as string]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.utils :as utils])
(:use [clojure.set]
[clj-time.coerce :only [to-timestamp]]
......
......@@ -26,8 +26,7 @@
[clojure.java.jdbc :as sql]
[clojure.string :as str]
[clojure.tools.logging :as log]
[com.puppetlabs.cheshire]
[cheshire.core :as json])
[com.puppetlabs.cheshire :as json])
(:use [clj-time.coerce :only [to-timestamp]]
[clj-time.core :only [ago secs now]]
[metrics.meters :only (meter mark!)]
......
......@@ -9,7 +9,7 @@
[clojure.string :as string]
[clj-http.client :as client]
[ring.util.codec :as ring-codec]
[cheshire.core :as json]
[com.puppetlabs.cheshire :as json]
[com.puppetlabs.puppetdb.scf.storage :as scf-store]))
;; ### PuppetDB current version
......
(ns com.puppetlabs.test.cheshire
(:require [clj-time.core :as clj-time])
(:import (java.io StringWriter StringReader))
(:use clojure.test
com.puppetlabs.cheshire))
(deftest test-generate-string
(testing "should generate a json string"
(is (= (generate-string {:a 1 :b 2})
"{\"a\":1,\"b\":2}")))
(testing "should generate a json string that has a Joda DataTime object in it and not explode"
(is (= (generate-string {:a 1 :b (clj-time/date-time 1986 10 14 4 3 27 456)})
"{\"a\":1,\"b\":\"1986-10-14T04:03:27.456Z\"}"))))
(deftest test-generate-stream
(testing "should generate a json string from a stream"
(let [sw (StringWriter.)]
(generate-stream {:a 1 :b 2} sw)
(is (= (.toString sw)
"{\"a\":1,\"b\":2}")))))
(deftest test-parse-string
(testing "should return a map from parsing a json string"
(is (= (parse-string "{\"a\":1,\"b\":2}")
{"a" 1 "b" 2}))))
(deftest test-parse-stream
(testing "should return map from parsing a json stream"
(is (= (parse-stream (StringReader. "{\"a\":1,\"b\":2}"))
{"a" 1 "b" 2}))))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment