Skip to content

Commit

Permalink
added exact and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacervello committed Aug 10, 2017
1 parent ba452df commit 513e437
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
41 changes: 41 additions & 0 deletions src/cheshire/exact.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns cheshire.exact
(:require [cheshire.factory :as factory]
[cheshire.parse :as parse]
[cheshire.core :as core])
(:import (java.io StringReader Reader BufferedReader
Writer)
(com.fasterxml.jackson.core JsonFactory)))

(defn- exact-parse [jp parsed]
(let [valid-json? (try (nil? (.nextToken jp))
(catch Exception _ false))]
(if valid-json?
parsed
(throw (Exception. "Invalid json")))))

(defn parse-string
"Like cheshire.core/parse-string
but with only valid json string"
([string] (parse-string string nil nil))
([string key-fn] (parse-string string key-fn nil))
([^String string key-fn array-coerce-fn]
(when string
(let [jp (.createParser ^JsonFactory (or factory/*json-factory*
factory/json-factory)
^Reader (StringReader. string))]
(exact-parse jp (parse/parse jp key-fn nil array-coerce-fn))))))

(defn parse-string-strict
([string] (parse-string-strict string nil nil))
([string key-fn] (parse-string-strict string key-fn nil))
([^String string key-fn array-coerce-fn]
(when string
(let [jp (.createParser ^JsonFactory (or factory/*json-factory*
factory/json-factory)
^Writer (StringReader. string))]
(exact-parse jp (parse/parse-strict jp key-fn nil array-coerce-fn))))))

(def decode parse-string)
(core/copy-arglists decode parse-string)
(def decode-strict parse-string-strict)
(core/copy-arglists decode-strict parse-string-strict)
15 changes: 10 additions & 5 deletions test/cheshire/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:use [clojure.test]
[clojure.java.io :only [file reader]])
(:require [cheshire.core :as json]
[cheshire.exact :as json-exact]
[cheshire.generate :as gen]
[cheshire.factory :as fact]
[cheshire.parse :as parse])
Expand Down Expand Up @@ -399,10 +400,14 @@

(deftest t-invalid-json
(are [x y] (= x (try
(binding [cheshire.parse/*valid-json-only* true] y)
y
(catch Exception e
(.getMessage e))))
"Invalid json" (json/decode "{\"foo\": 1}asdf")
"Invalid json" (json/decode "{\"foo\": 123}null")
"Invalid json" (json/decode "\"hello\" : 123}")
{"foo" 1} (json/decode "{\"foo\": 1}")))
"Invalid json" (json-exact/decode "{\"foo\": 1}asdf")
"Invalid json" (json-exact/decode "{\"foo\": 123}null")
"Invalid json" (json-exact/decode "\"hello\" : 123}")
{"foo" 1} (json/decode "{\"foo\": 1}")
"Invalid json" (json-exact/decode-strict "{\"foo\": 1}asdf")
"Invalid json" (json-exact/decode-strict "{\"foo\": 123}null")
"Invalid json" (json-exact/decode-strict "\"hello\" : 123}")
{"foo" 1} (json/decode-strict "{\"foo\": 1}")))

0 comments on commit 513e437

Please sign in to comment.