-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba452df
commit 513e437
Showing
2 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters