├── dev-resources
├── json10b.json
├── json100b.json
├── json1k.json
├── 1000-null.json
├── nested-arrays.json
├── 1000-numbers.json
├── 1000-strings.json
└── json10k.json
├── .gitignore
├── deps.edn
├── .github
└── workflows
│ ├── test.yml
│ ├── snapshot.yml
│ ├── doc-build.yml
│ └── release.yml
├── src
├── assembly
│ └── aot.xml
├── test
│ ├── clojure
│ │ └── clojure
│ │ │ └── data
│ │ │ ├── json_gen_test.clj
│ │ │ ├── json_compat_0_1_test.clj
│ │ │ ├── json_test_suite_test.clj
│ │ │ └── json_test.clj
│ └── clojure-perf
│ │ └── clojure
│ │ └── data
│ │ └── json_perf_test.clj
└── main
│ └── clojure
│ └── clojure
│ └── data
│ └── json.clj
├── CONTRIBUTING.md
├── project.clj
├── pom.xml
├── LICENSE
├── epl-v10.html
└── README.md
/dev-resources/json10b.json:
--------------------------------------------------------------------------------
1 | {"imu":42}
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .cpcache/
3 | .idea/
4 | *.iml
5 | .lein*
6 | .nrepl*
7 |
--------------------------------------------------------------------------------
/deps.edn:
--------------------------------------------------------------------------------
1 | {:paths ["src/main/clojure"]
2 | :deps {org.clojure/clojure {:mvn/version "1.9.0"}}}
3 |
--------------------------------------------------------------------------------
/dev-resources/json100b.json:
--------------------------------------------------------------------------------
1 | {"number":100,"boolean":true,"list":[{"kikka":"kukka"}],"nested":{"map":"this is value","secret":1}}
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: Test
2 |
3 | on: [push]
4 |
5 | jobs:
6 | call-test:
7 | uses: clojure/build.ci/.github/workflows/test.yml@master
8 |
--------------------------------------------------------------------------------
/.github/workflows/snapshot.yml:
--------------------------------------------------------------------------------
1 | name: Snapshot on demand
2 |
3 | on: [workflow_dispatch]
4 |
5 | jobs:
6 | call-snapshot:
7 | uses: clojure/build.ci/.github/workflows/snapshot.yml@master
8 | secrets: inherit
9 |
--------------------------------------------------------------------------------
/.github/workflows/doc-build.yml:
--------------------------------------------------------------------------------
1 | name: Build API Docs
2 |
3 | on:
4 | workflow_dispatch:
5 |
6 | jobs:
7 | call-doc-build-workflow:
8 | uses: clojure/build.ci/.github/workflows/doc-build.yml@master
9 | with:
10 | project: clojure/data.json
11 |
--------------------------------------------------------------------------------
/src/assembly/aot.xml:
--------------------------------------------------------------------------------
1 |
2 | aot
3 |
4 | jar
5 |
6 | false
7 |
8 |
9 | src/resources
10 | /
11 | true
12 |
13 |
14 | target/classes
15 | /
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | This is a [Clojure contrib] project.
2 |
3 | Under the Clojure contrib [guidelines], this project cannot accept
4 | pull requests. All patches must be submitted via [JIRA].
5 |
6 | See [Contributing] on the Clojure website for
7 | more information on how to contribute.
8 |
9 | [Clojure contrib]: https://clojure.org/community/contrib_libs
10 | [Contributing]: https://clojure.org/community/contributing
11 | [JIRA]: https://clojure.atlassian.net/browse/DJSON
12 | [guidelines]: https://clojure.org/community/contrib_howto
13 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release on demand
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | releaseVersion:
7 | description: "Version to release"
8 | required: true
9 | snapshotVersion:
10 | description: "Snapshot version after release"
11 | required: true
12 |
13 | jobs:
14 | call-release:
15 | uses: clojure/build.ci/.github/workflows/release.yml@master
16 | with:
17 | releaseVersion: ${{ github.event.inputs.releaseVersion }}
18 | snapshotVersion: ${{ github.event.inputs.snapshotVersion }}
19 | secrets: inherit
--------------------------------------------------------------------------------
/dev-resources/json1k.json:
--------------------------------------------------------------------------------
1 | {"results":[{"gender":"male","name":{"title":"mr","first":"morris","last":"lambert"},"location":{"street":"7239 hillcrest rd","city":"nowra","state":"australian capital territory","postcode":7541},"email":"morris.lambert@example.com","login":{"username":"smallbird414","password":"carole","salt":"yO9OBSsk","md5":"658323a603522238fb32a86b82eafd55","sha1":"289f6e9a8ccd42b539e0c43283e788aeb8cd0f6e","sha256":"57bca99b2b4e78aa2171eda4db3f35e7631ca3b30f157bdc7ea089a855c66668"},"dob":"1950-07-13 09:18:34","registered":"2012-04-07 00:05:32","phone":"08-2274-7839","cell":"0452-558-702","id":{"name":"TFN","value":"740213762"},"picture":{"large":"https://randomuser.me/api/portraits/men/95.jpg","medium":"https://randomuser.me/api/portraits/med/men/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/95.jpg"},"nat":"AU"}],"info":{"seed":"fb0c2b3c7cedc7af","results":1,"page":1,"version":"1.1"}}
2 |
--------------------------------------------------------------------------------
/project.clj:
--------------------------------------------------------------------------------
1 | ;; NOTE: Used only for perf testing - this project is built with Maven (see pom.xml)
2 | (defproject clojure.data.json "1.1.1-SNAPSHOT"
3 | :dependencies [[org.clojure/clojure "1.12.0"]]
4 | :source-paths ["src/main/clojure"]
5 | :java-source-paths ["src/main/java"]
6 | :java-test-paths ["src/test/java"]
7 | :test-paths ["src/test/clojure" "src/test/clojure-perf"]
8 | :profiles {:dev {:dependencies [[com.clojure-goes-fast/clj-async-profiler "1.5.1"]
9 | [com.clojure-goes-fast/clj-java-decompiler "0.3.6"]
10 | [org.clojure/test.check "1.1.1"]
11 | [criterium/criterium "0.4.6"]
12 | [metosin/jsonista "0.3.12"]
13 | [cheshire/cheshire "5.13.0"]
14 | [org.openjdk.jmh/jmh-core "1.37"]
15 | [jmh-clojure "0.4.1"]
16 | [com.jsoniter/jsoniter "0.9.23"]]
17 | :resource-paths ["dev-resources"]
18 | :global-vars {*warn-on-reflection* true}}}
19 | :jvm-opts ["-Djdk.attach.allowAttachSelf=true"])
20 |
--------------------------------------------------------------------------------
/src/test/clojure/clojure/data/json_gen_test.clj:
--------------------------------------------------------------------------------
1 | (ns clojure.data.json-gen-test
2 | (:require [clojure.data.json :as json]
3 | [clojure.spec.alpha :as s]
4 | [clojure.spec.gen.alpha :as sgen]
5 | [clojure.spec.test.alpha :as stest]
6 | [clojure.test :refer :all]))
7 |
8 | (s/def ::json-number
9 | (s/with-gen
10 | number?
11 | #(sgen/one-of [(sgen/large-integer) (sgen/double* {:infinite? false :NaN? false})])))
12 |
13 | (s/def ::json-scalar (s/or :boolean boolean?
14 | :number ::json-number
15 | :string string?
16 | :nil nil?))
17 |
18 | (s/def ::json-value (s/or :object ::json-object
19 | :array ::json-array
20 | :scalar ::json-scalar))
21 |
22 | (s/def ::json-array (s/coll-of ::json-value :gen-max 12))
23 | (s/def ::json-object (s/map-of string? ::json-value
24 | :gen-max 10))
25 |
26 | (s/fdef json/write-str
27 | :args (s/cat :json ::json-value)
28 | :ret string?
29 | :fn #(= (->> % :args :json (s/unform ::json-value))
30 | (json/read-str (-> % :ret))))
31 |
32 | (deftest roundtrip
33 | (let [results (stest/check `json/write-str)]
34 | (is (every? nil? (mapv :failure results)))))
35 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | data.json
5 | 2.5.2-SNAPSHOT
6 | data.json
7 | Generating/parsing JSON from/to Clojure data structures
8 | https://github.com/clojure/data.json
9 |
10 |
11 |
12 | Stuart Sierra
13 | mail@stuartsierra.com
14 | http://stuartsierra.com/
15 | -5
16 |
17 |
18 |
19 |
20 | org.clojure
21 | pom.contrib
22 | 1.3.0
23 |
24 |
25 |
26 | scm:git:git@github.com:clojure/data.json
27 | scm:git:git@github.com:clojure/data.json
28 | git@github.com:clojure/data.json.git
29 | HEAD
30 |
31 |
32 |
33 | 1.9.0
34 |
35 |
36 |
37 |
38 | org.clojure
39 | test.check
40 | 1.1.1
41 | test
42 |
43 |
44 |
45 |
46 |
47 |
48 | com.theoryinpractise
49 | clojure-maven-plugin
50 | 1.7.1
51 |
52 | false
53 | -Dclojure.compiler.direct-linking=true
54 |
55 |
56 |
57 | clojure-compile
58 | compile
59 |
60 | compile
61 |
62 |
63 | true
64 |
65 | clojure.data.json
66 | clojure.data.json-compat-0-1
67 |
68 |
69 |
70 |
71 |
72 |
73 | maven-jar-plugin
74 | 3.0.2
75 |
76 |
77 | default-jar
78 | package
79 |
80 | jar
81 |
82 |
83 |
84 | **/*.clj
85 |
86 |
87 |
88 |
89 |
90 |
91 | maven-assembly-plugin
92 | 3.0.0
93 |
94 |
95 | aot-jar
96 | package
97 |
98 | single
99 |
100 |
101 |
102 | src/assembly/aot.xml
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/dev-resources/1000-null.json:
--------------------------------------------------------------------------------
1 | [null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
--------------------------------------------------------------------------------
/dev-resources/nested-arrays.json:
--------------------------------------------------------------------------------
1 | ["a", ["b", "c", "d", "e", "f"], [[], "a"], [["a", ["b", "c", "d", "e", "f"]], "a"], ["b", "c", "d", "e", "f"], ["a", ["a", [[], "a"]]], ["a", [["b", "c", "d", "e", "f"], "a"]], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], [], ["b", "c", "d", "e", "f"], ["a", []], ["b", "c", "d", "e", "f"], [], [[[["a", [[[["a", ["b", "c", "d", "e", "f"]], "a"], "a"], "a"]], "a"], "a"], "a"], [], [[["a", ["a", [["a", ["a", [[["a", ["b", "c", "d", "e", "f"]], "a"], "a"]]], "a"]]], "a"], "a"], [["a", [[["a", [[], "a"]], "a"], "a"]], "a"], ["a", []], ["b", "c", "d", "e", "f"], ["a", [[[[["b", "c", "d", "e", "f"], "a"], "a"], "a"], "a"]], ["a", []], [], [[], "a"], [[["b", "c", "d", "e", "f"], "a"], "a"], ["a", ["a", ["b", "c", "d", "e", "f"]]], ["a", [["a", ["a", ["b", "c", "d", "e", "f"]]], "a"]], ["a", ["a", ["a", [[[], "a"], "a"]]]], ["a", [[], "a"]], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], [["a", []], "a"], [["a", ["a", ["b", "c", "d", "e", "f"]]], "a"], ["a", [["a", ["a", [["b", "c", "d", "e", "f"], "a"]]], "a"]], ["a", ["b", "c", "d", "e", "f"]], ["a", [[[["b", "c", "d", "e", "f"], "a"], "a"], "a"]], [["a", []], "a"], ["a", []], [], [], ["a", [["a", ["a", ["a", ["a", []]]]], "a"]], [["a", ["b", "c", "d", "e", "f"]], "a"], ["a", ["b", "c", "d", "e", "f"]], ["a", [[], "a"]], ["a", ["a", ["a", [[[[], "a"], "a"], "a"]]]], ["b", "c", "d", "e", "f"], [[["b", "c", "d", "e", "f"], "a"], "a"], [["a", ["b", "c", "d", "e", "f"]], "a"], [], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], [["b", "c", "d", "e", "f"], "a"], [["a", ["b", "c", "d", "e", "f"]], "a"], [["a", ["a", ["a", [[[[], "a"], "a"], "a"]]]], "a"], ["a", ["a", ["a", ["a", []]]]], [], [[[], "a"], "a"], ["b", "c", "d", "e", "f"], ["a", [["a", [[], "a"]], "a"]], [["a", []], "a"], [[[["b", "c", "d", "e", "f"], "a"], "a"], "a"], ["a", []], [[["a", [[["a", ["b", "c", "d", "e", "f"]], "a"], "a"]], "a"], "a"], [], ["a", [["a", [["a", ["a", [[[], "a"], "a"]]], "a"]], "a"]], [], ["a", ["a", ["a", ["a", ["a", []]]]]], [[[["b", "c", "d", "e", "f"], "a"], "a"], "a"], [["a", ["a", [[], "a"]]], "a"], [], ["a", ["a", []]], ["b", "c", "d", "e", "f"], ["a", ["a", [[[["a", [["b", "c", "d", "e", "f"], "a"]], "a"], "a"], "a"]]], ["a", ["a", [[], "a"]]], [], ["b", "c", "d", "e", "f"], ["a", [[], "a"]], ["a", [[], "a"]], [[[[["a", []], "a"], "a"], "a"], "a"], [[[[[[], "a"], "a"], "a"], "a"], "a"], ["a", []], ["a", ["b", "c", "d", "e", "f"]], ["a", []], ["a", []], ["a", []], [], ["a", []], ["a", ["a", []]], [["a", ["b", "c", "d", "e", "f"]], "a"], [[["a", [[["a", ["b", "c", "d", "e", "f"]], "a"], "a"]], "a"], "a"], [["a", ["a", ["a", [[], "a"]]]], "a"], ["a", ["b", "c", "d", "e", "f"]], [["b", "c", "d", "e", "f"], "a"], [["a", ["a", ["b", "c", "d", "e", "f"]]], "a"], [], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], ["a", ["b", "c", "d", "e", "f"]], [["a", ["a", [[["b", "c", "d", "e", "f"], "a"], "a"]]], "a"], ["a", [[[], "a"], "a"]], [], [], [["a", ["a", [["a", [[[["a", []], "a"], "a"], "a"]], "a"]]], "a"], [["b", "c", "d", "e", "f"], "a"], ["a", ["a", [["b", "c", "d", "e", "f"], "a"]]], ["b", "c", "d", "e", "f"], ["a", ["b", "c", "d", "e", "f"]], ["a", []], ["b", "c", "d", "e", "f"], [], ["a", ["a", ["a", [[[["a", ["a", ["a", ["a", ["a", ["a", ["a", [["a", [[[[], "a"], "a"], "a"]], "a"]]]]]]]], "a"], "a"], "a"]]]], [[[], "a"], "a"], [], ["b", "c", "d", "e", "f"], [[["a", [["b", "c", "d", "e", "f"], "a"]], "a"], "a"], [], ["b", "c", "d", "e", "f"], ["a", ["a", ["a", [[[[[[], "a"], "a"], "a"], "a"], "a"]]]], [["a", [[["b", "c", "d", "e", "f"], "a"], "a"]], "a"], [[[[[["a", ["a", ["b", "c", "d", "e", "f"]]], "a"], "a"], "a"], "a"], "a"], ["a", [["b", "c", "d", "e", "f"], "a"]], ["a", ["a", ["a", [["a", [["a", [[], "a"]], "a"]], "a"]]]], [[], "a"], [["a", [[["a", [["a", [[["a", ["b", "c", "d", "e", "f"]], "a"], "a"]], "a"]], "a"], "a"]], "a"], ["a", [["b", "c", "d", "e", "f"], "a"]], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], [[], "a"], [[[[[["a", [["b", "c", "d", "e", "f"], "a"]], "a"], "a"], "a"], "a"], "a"], ["a", ["a", ["b", "c", "d", "e", "f"]]], [["a", [[], "a"]], "a"], [], ["a", [["a", []], "a"]], [[["b", "c", "d", "e", "f"], "a"], "a"], [], [[[], "a"], "a"], [[["a", ["a", ["a", ["a", ["a", ["a", ["a", ["b", "c", "d", "e", "f"]]]]]]]], "a"], "a"], ["a", [[], "a"]], [[[["a", ["a", []]], "a"], "a"], "a"], ["a", [[["a", [["a", ["a", []]], "a"]], "a"], "a"]], ["a", [[], "a"]], ["a", []], ["a", ["b", "c", "d", "e", "f"]], [], [["a", []], "a"], ["a", [["a", ["b", "c", "d", "e", "f"]], "a"]], ["b", "c", "d", "e", "f"], ["a", [["b", "c", "d", "e", "f"], "a"]], ["a", ["a", []]], [], [[["a", [[[], "a"], "a"]], "a"], "a"], ["b", "c", "d", "e", "f"], [], [], ["b", "c", "d", "e", "f"], ["a", []], [["a", [["a", ["b", "c", "d", "e", "f"]], "a"]], "a"], ["b", "c", "d", "e", "f"], ["a", ["a", ["b", "c", "d", "e", "f"]]], ["b", "c", "d", "e", "f"], ["a", ["a", ["a", [[["a", [[[["b", "c", "d", "e", "f"], "a"], "a"], "a"]], "a"], "a"]]]], ["a", ["b", "c", "d", "e", "f"]], ["a", ["b", "c", "d", "e", "f"]], [[["a", [["b", "c", "d", "e", "f"], "a"]], "a"], "a"], [["a", [["b", "c", "d", "e", "f"], "a"]], "a"], [["a", [["b", "c", "d", "e", "f"], "a"]], "a"], ["a", [["a", [["a", [[], "a"]], "a"]], "a"]], [[["b", "c", "d", "e", "f"], "a"], "a"], [[[[], "a"], "a"], "a"], [[["b", "c", "d", "e", "f"], "a"], "a"], ["a", ["b", "c", "d", "e", "f"]], ["b", "c", "d", "e", "f"], [[[[["a", ["b", "c", "d", "e", "f"]], "a"], "a"], "a"], "a"], [[[], "a"], "a"], [["a", []], "a"], [], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], ["b", "c", "d", "e", "f"], [["a", [[[[], "a"], "a"], "a"]], "a"], ["b", "c", "d", "e", "f"], ["a", [["b", "c", "d", "e", "f"], "a"]], [[[], "a"], "a"], [["a", ["b", "c", "d", "e", "f"]], "a"], [[["b", "c", "d", "e", "f"], "a"], "a"], ["b", "c", "d", "e", "f"], [], [[], "a"], ["a", ["a", ["a", ["a", ["a", ["a", []]]]]]], ["a", ["a", ["b", "c", "d", "e", "f"]]], [[["a", [["a", ["a", [["a", ["a", [["b", "c", "d", "e", "f"], "a"]]], "a"]]], "a"]], "a"], "a"], [[["a", [[], "a"]], "a"], "a"], ["a", [["a", ["a", ["a", [[["b", "c", "d", "e", "f"], "a"], "a"]]]], "a"]], ["a", [["a", [[], "a"]], "a"]], ["a", ["b", "c", "d", "e", "f"]], ["a", []], [], [[[[["a", []], "a"], "a"], "a"], "a"], ["b", "c", "d", "e", "f"], ["a", []], ["a", ["a", ["a", ["b", "c", "d", "e", "f"]]]]]
2 |
--------------------------------------------------------------------------------
/src/test/clojure-perf/clojure/data/json_perf_test.clj:
--------------------------------------------------------------------------------
1 | (ns clojure.data.json-perf-test
2 | (:require [cheshire.core :as cheshire]
3 | [clj-async-profiler.core :as prof]
4 | [clojure.data.json :as json]
5 | [criterium.core :refer :all]
6 | [jsonista.core :as jsonista]
7 | [clojure.string :as str])
8 | (:import com.jsoniter.JsonIterator))
9 |
10 | (defmacro profiling [times & body]
11 | `(try
12 | (prof/start {})
13 | (dotimes [_# ~times]
14 | ~@body)
15 | (finally
16 | (prof/stop {:transform (fn [s#]
17 | (if (or (str/index-of s# "err_codes_unix")
18 | (str/index-of s# "["))
19 | nil
20 | (-> s#
21 | (str/replace #"^.+/.*read-profiling.+?;" "START;")
22 | (str/replace #"^.*nextToken.*" "jackson-next-token")
23 | (str/replace #"^.*getText.*" "jackson-get-text")
24 | (str/replace #"^.*next.token.*" "data-json-next-token")
25 | (str/replace #"^.*read.quoted.string.*" "data-json-read_quoted_string")
26 | (str/replace #".*assoc.*" "ASSOC;")
27 | (str/replace #".*Map.*" "ASSOC;"))))}))))
28 |
29 | (defn json-data [size]
30 | (slurp (str "dev-resources/json" size ".json")))
31 |
32 | (defn do-read-bench [size]
33 | (let [json (json-data size)]
34 | (println "Results for" size "json:")
35 | (println "data.json:")
36 | (println (with-out-str (quick-bench (json/read-str json))))
37 | (println "cheshire:")
38 | (println (with-out-str (quick-bench (cheshire/parse-string-strict json))))
39 | (println "jsonista:")
40 | (println (with-out-str (quick-bench (jsonista/read-value json))))
41 | (println "jsoniter:")
42 | (println (with-out-str (quick-bench (.read (JsonIterator/parse ^String json)))))))
43 |
44 | (defn do-write-bench [size]
45 | (let [edn (json/read-str (json-data size))]
46 | (println "Results for" size "json:")
47 | (println "data.json:")
48 | (println (with-out-str (quick-bench (json/write-str edn))))
49 | (println "cheshire:")
50 | (println (with-out-str (quick-bench (cheshire/generate-string edn))))
51 | (println "jsonista:")
52 | (println (with-out-str (quick-bench (jsonista/write-value-as-string edn))))))
53 |
54 | (defn read-bench-all-sizes []
55 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
56 | (do-read-bench size)))
57 |
58 | (defn write-bench-all-sizes []
59 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
60 | (do-write-bench size)))
61 |
62 | (defn profile-read-all-sizes []
63 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
64 | (let [json (json-data size)]
65 | (profiling 10000 (json/read-str json))
66 | (Thread/sleep 1000))))
67 |
68 | (defn profile-write-all-sizes []
69 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
70 | (let [edn (json/read-str (json-data size))]
71 | (profiling 10000 (json/write-str edn))
72 | (Thread/sleep 1000))))
73 |
74 | (defn read-bench-data-json []
75 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
76 | (let [json (json-data size)]
77 | (quick-bench (json/read-str json)))))
78 |
79 | (defn write-bench-data-json []
80 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
81 | (let [json (json/read-str (json-data size))]
82 | (quick-bench (json/write-str json)))))
83 |
84 | (defn write-profiling []
85 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
86 | (let [json (json/read-str (json-data size))]
87 | (profiling 10000 (json/write-str json)))))
88 |
89 | (defn read-profiling []
90 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
91 | (let [json (json-data size)]
92 | (profiling 10000 (json/read-str json)))))
93 |
94 | (defn cheshire-read-profiling []
95 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
96 | (let [json (json-data size)]
97 | (profiling 10000 (cheshire/parse-string-strict json)))))
98 |
99 | (defn jsonista-read-profiling []
100 | (doseq [size ["10b" "100b" "1k" "10k" "100k"]]
101 | (let [json (json-data size)]
102 | (profiling 10000 (jsonista/read-value json)))))
103 |
104 |
105 | (defn benchit [data]
106 | (let [json (json/write-str data)]
107 | (quick-bench (json/read-str json))
108 | (quick-bench (cheshire/parse-string-strict json))))
109 |
110 | ;; Used to generate 1000-numbers.json
111 | (defn gen-int []
112 | (str (rand-int 1000)))
113 |
114 | (defn gen-frac []
115 | (str "." (rand-int 1000)))
116 |
117 | (defn gen-exp []
118 | (str (rand-nth ["e" "E"]) (rand-nth ["" "-" "+"]) (rand-int 5)))
119 |
120 | (defn generate-random-json-number []
121 | (case (rand-int 8)
122 | ; int
123 | 0 (gen-int)
124 | 1 (str "-" (gen-int))
125 | ; frac
126 | 2 (str (gen-int) (gen-frac))
127 | 3 (str "-" (gen-int) (gen-frac))
128 | ; exp
129 | 4 (str (gen-int) (gen-exp))
130 | 5 (str "-" (gen-int) (gen-exp))
131 | ; combo
132 | 6 (str (gen-int) (gen-frac) (gen-exp))
133 | 7 (str "-" (gen-int) (gen-frac) (gen-exp))))
134 |
135 | (defn generate-random-json-numbers [n]
136 | ;; used to generate dev-resources/1000-numbers.json
137 | (->> (for [x (range n)]
138 | (generate-random-json-number))
139 | (str/join ", ")
140 | (#(str "[" % "]"))))
141 |
142 | (defn read-bench-all-sizes-numbers []
143 | (let [json (slurp "dev-resources/1000-numbers.json")]
144 | (println "data.json:")
145 | (println (with-out-str (quick-bench (json/read-str json))))
146 | (println "cheshire:")
147 | (println (with-out-str (quick-bench (cheshire/parse-string-strict json))))
148 | (println "jsonista:")
149 | (println (with-out-str (quick-bench (jsonista/read-value json))))
150 | (println "jsoniter:")
151 | (println (with-out-str (quick-bench (.read (JsonIterator/parse ^String json)))))))
152 |
153 | (defn read-bench-nested-arrays []
154 | (let [json (slurp "dev-resources/nested-arrays.json")]
155 | (println "Results for nested arrays:")
156 | (println "data.json:")
157 | (println (with-out-str (quick-bench (json/read-str json))))
158 | (println "cheshire:")
159 | (println (with-out-str (quick-bench (cheshire/parse-string-strict json))))
160 | (println "jsonista:")
161 | (println (with-out-str (quick-bench (jsonista/read-value json))))
162 | (println "jsoniter:")
163 | (println (with-out-str (quick-bench (.read (JsonIterator/parse ^String json)))))))
164 |
--------------------------------------------------------------------------------
/src/test/clojure/clojure/data/json_compat_0_1_test.clj:
--------------------------------------------------------------------------------
1 | ;; Copyright (c) Stuart Sierra, 2012. All rights reserved. The use and
2 | ;; distribution terms for this software are covered by the Eclipse
3 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 | ;; By using this software in any fashion, you are agreeing to be bound
5 | ;; by the terms of this license. You must not remove this notice, or
6 | ;; any other, from this software.
7 |
8 | (ns clojure.data.json-compat-0-1-test
9 | "Compatibility tests for the 0.1.x data.json API."
10 | (:use clojure.test
11 | [clojure.data.json :only (read-json json-str pprint-json)]))
12 |
13 | (deftest can-read-from-pushback-reader
14 | (let [s (java.io.PushbackReader. (java.io.StringReader. "42"))]
15 | (is (= 42 (read-json s)))))
16 |
17 | (deftest can-read-from-reader
18 | (let [s (java.io.StringReader. "42")]
19 | (is (= 42 (read-json s)))))
20 |
21 | (deftest can-read-numbers
22 | (is (= 42 (read-json "42")))
23 | (is (= -3 (read-json "-3")))
24 | (is (= 3.14159 (read-json "3.14159")))
25 | (is (= 6.022e23 (read-json "6.022e23"))))
26 |
27 | (deftest can-read-null
28 | (is (= nil (read-json "null"))))
29 |
30 | (deftest can-read-strings
31 | (is (= "Hello, World!" (read-json "\"Hello, World!\""))))
32 |
33 | (deftest handles-escaped-slashes-in-strings
34 | (is (= "/foo/bar" (read-json "\"\\/foo\\/bar\""))))
35 |
36 | (deftest handles-unicode-escapes
37 | (is (= " \u0beb " (read-json "\" \\u0bEb \""))))
38 |
39 | (deftest handles-unicode-outside-bmp
40 | (is (= "\"smiling face: \uD83D\uDE03\""
41 | (json-str "smiling face: \uD83D\uDE03" :escape-unicode false)))
42 | (is (= "\"smiling face: \\ud83d\\ude03\""
43 | (json-str "smiling face: \uD83D\uDE03" :escape-unicode true))))
44 |
45 | (deftest handles-escaped-whitespace
46 | (is (= "foo\nbar" (read-json "\"foo\\nbar\"")))
47 | (is (= "foo\rbar" (read-json "\"foo\\rbar\"")))
48 | (is (= "foo\tbar" (read-json "\"foo\\tbar\""))))
49 |
50 | (deftest can-read-booleans
51 | (is (= true (read-json "true")))
52 | (is (= false (read-json "false"))))
53 |
54 | (deftest can-ignore-whitespace
55 | (is (= nil (read-json "\r\n null"))))
56 |
57 | (deftest can-read-arrays
58 | (is (= [1 2 3] (read-json "[1,2,3]")))
59 | (is (= ["Ole" "Lena"] (read-json "[\"Ole\", \r\n \"Lena\"]"))))
60 |
61 | (deftest can-read-objects
62 | (is (= {:a 1, :b 2} (read-json "{\"a\": 1, \"b\": 2}"))))
63 |
64 | (deftest can-read-nested-structures
65 | (is (= {:a [1 2 {:b [3 "four"]} 5.5]}
66 | (read-json "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"))))
67 |
68 | (deftest disallows-non-string-keys
69 | (is (thrown? Exception (read-json "{26:\"z\""))))
70 |
71 | (deftest disallows-barewords
72 | (is (thrown? Exception (read-json " foo "))))
73 |
74 | (deftest disallows-unclosed-arrays
75 | (is (thrown? Exception (read-json "[1, 2, "))))
76 |
77 | (deftest disallows-unclosed-objects
78 | (is (thrown? Exception (read-json "{\"a\":1, "))))
79 |
80 | (deftest can-get-string-keys
81 | (is (= {"a" [1 2 {"b" [3 "four"]} 5.5]}
82 | (read-json "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}" false true nil))))
83 |
84 | (declare pass1-string)
85 |
86 | (deftest pass1-test
87 | (let [input (read-json pass1-string false true nil)]
88 | (is (= "JSON Test Pattern pass1" (first input)))
89 | (is (= "array with 1 element" (get-in input [1 "object with 1 member" 0])))
90 | (is (= 1234567890 (get-in input [8 "integer"])))
91 | (is (= "rosebud" (last input)))))
92 |
93 | ; from http://www.json.org/JSON_checker/test/pass1.json
94 | (def pass1-string
95 | "[
96 | \"JSON Test Pattern pass1\",
97 | {\"object with 1 member\":[\"array with 1 element\"]},
98 | {},
99 | [],
100 | -42,
101 | true,
102 | false,
103 | null,
104 | {
105 | \"integer\": 1234567890,
106 | \"real\": -9876.543210,
107 | \"e\": 0.123456789e-12,
108 | \"E\": 1.234567890E+34,
109 | \"\": 23456789012E66,
110 | \"zero\": 0,
111 | \"one\": 1,
112 | \"space\": \" \",
113 | \"quote\": \"\\\"\",
114 | \"backslash\": \"\\\\\",
115 | \"controls\": \"\\b\\f\\n\\r\\t\",
116 | \"slash\": \"/ & \\/\",
117 | \"alpha\": \"abcdefghijklmnopqrstuvwyz\",
118 | \"ALPHA\": \"ABCDEFGHIJKLMNOPQRSTUVWYZ\",
119 | \"digit\": \"0123456789\",
120 | \"0123456789\": \"digit\",
121 | \"special\": \"`1~!@#$%^&*()_+-={':[,]}|;.>?\",
122 | \"hex\": \"\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A\",
123 | \"true\": true,
124 | \"false\": false,
125 | \"null\": null,
126 | \"array\":[ ],
127 | \"object\":{ },
128 | \"address\": \"50 St. James Street\",
129 | \"url\": \"http://www.JSON.org/\",
130 | \"comment\": \"// /* */\": \" \",
132 | \" s p a c e d \" :[1,2 , 3
133 |
134 | ,
135 |
136 | 4 , 5 , 6 ,7 ],\"compact\":[1,2,3,4,5,6,7],
137 | \"jsontext\": \"{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}\",
138 | \"quotes\": \"" \\u0022 %22 0x22 034 "\",
139 | \"\\/\\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?\"
140 | : \"A key can be any string\"
141 | },
142 | 0.5 ,98.6
143 | ,
144 | 99.44
145 | ,
146 |
147 | 1066,
148 | 1e1,
149 | 0.1e1,
150 | 1e-1,
151 | 1e00,2e+00,2e-00
152 | ,\"rosebud\"]")
153 |
154 |
155 | (deftest can-print-json-strings
156 | (is (= "\"Hello, World!\"" (json-str "Hello, World!")))
157 | (is (= "\"\\\"Embedded\\\" Quotes\"" (json-str "\"Embedded\" Quotes"))))
158 |
159 | (deftest can-print-unicode
160 | (is (= "\"\\u1234\\u4567\"" (json-str "\u1234\u4567"))))
161 |
162 | (deftest can-print-nonescaped-unicode
163 | (is (= "\"\u1234\u4567\"" (json-str "\u1234\u4567" :escape-unicode false))))
164 |
165 | (deftest can-print-json-null
166 | (is (= "null" (json-str nil))))
167 |
168 | (deftest can-print-ratios-as-doubles
169 | (is (= "0.75" (json-str 3/4))))
170 |
171 | (deftest can-print-bigints
172 | (is (= "12345678901234567890" (json-str 12345678901234567890))))
173 |
174 | (deftest can-print-json-arrays
175 | (is (= "[1,2,3]" (json-str [1 2 3])))
176 | (is (= "[1,2,3]" (json-str (list 1 2 3))))
177 | (is (= "[1,2,3]" (json-str (sorted-set 1 2 3))))
178 | (is (= "[1,2,3]" (json-str (seq [1 2 3])))))
179 |
180 | (deftest can-print-java-arrays
181 | (is (= "[1,2,3]" (json-str (into-array [1 2 3])))))
182 |
183 | (deftest can-print-empty-arrays
184 | (is (= "[]" (json-str [])))
185 | (is (= "[]" (json-str (list))))
186 | (is (= "[]" (json-str #{}))))
187 |
188 | (deftest can-print-json-objects
189 | (is (= "{\"a\":1,\"b\":2}" (json-str (sorted-map :a 1 :b 2)))))
190 |
191 | (deftest object-keys-must-be-strings
192 | (is (= "{\"1\":1,\"2\":2}" (json-str (sorted-map 1 1 2 2)))))
193 |
194 | (deftest can-print-empty-objects
195 | (is (= "{}" (json-str {}))))
196 |
197 | (deftest accept-sequence-of-nils
198 | (is (= "[null,null,null]" (json-str [nil nil nil]))))
199 |
200 | (deftest error-on-nil-keys
201 | (is (thrown? Exception (json-str {nil 1}))))
202 |
203 | (deftest characters-in-symbols-are-escaped
204 | (is (= "\"foo\\u1b1b\"" (json-str (symbol "foo\u1b1b")))))
205 |
206 | (deftest default-throws-on-eof
207 | (is (thrown? java.io.EOFException (read-json ""))))
208 |
209 | (deftest can-accept-eof
210 | (is (= ::eof (read-json "" true false ::eof))))
211 |
212 | (deftest characters-in-map-keys-are-escaped
213 | (is (= (json-str {"\"" 42}) "{\"\\\"\":42}")))
214 |
215 | ;;; Pretty-printer
216 |
217 | (deftest pretty-printing
218 | (let [x (read-json pass1-string false)]
219 | (is (= x (read-json (with-out-str (pprint-json x)) false)))))
220 |
221 | (defn benchmark []
222 | (dotimes [_ 8]
223 | (time
224 | (dotimes [_ 100]
225 | (assert (= (read-json pass1-string false)
226 | (read-json (json-str (read-json pass1-string false)) false)))))))
227 |
--------------------------------------------------------------------------------
/dev-resources/1000-numbers.json:
--------------------------------------------------------------------------------
1 | [-507.524, 635E-3, 365.845E4, -402, -917, 834.293E3, -366, 314, 273E3, -520, -222.183, -147.117E-3, -309.615E+2, -859, -200.726e+4, -176e0, 890.614, -417.612, -260.452, 794.869e1, -301.12, -379.661, 27e4, 21.417E+0, 342.909, -677.504, 713e+3, 782.7E+0, -336.432E1, 693.200, 882.348E1, 258.703, -120.915, -918e+0, -552e+1, -856.738E+0, 573e+1, 447, 647, -85.793, -408, 821.838, 173.751, -812.373, 434e+1, 326, -328.289E+0, -353, -118.704, -474.860E-2, 34, -112, -147, -242E-0, -212.606e+0, 761e4, -555e1, -112, -750.133, -258.306, 313.697e4, -442E2, 1.201, -396.665, -221.440, -434, 152E3, 212.994E+3, 684.200E3, -603.999, -219, -539.300, 636.941, -97.724e-4, -123, -930, 75, -583e+4, -449.950E-3, -238e+0, -829.367e-0, -640.416, -363, -953, -653E-4, 160.703E-2, 879.486, 328, -905.215, -73.202e+0, -666.957E+4, 581.548E+2, 44.750, 26, 210, -281.251e+1, 676, -64.992E+4, -645E+4, 359e-4, 58.155e-3, 579.357e-4, -924.213e+1, -283.552e+4, -923e1, -551.804, 925, -479, 565.79, -598, -817e2, 58.192, -564, 329.412, 67, 770, 728.596E3, -256, 22, -643, -865, -359, 537, 621, 935.562E-3, -259, 131.604e+1, -546.361e+4, 73.557e-4, 374.826, 687.414, -798, 479e4, 565E1, -754e-3, -339, -742.82, -56.11, 986.568, 226.572e2, -320.927E+3, 650.573, -812.344E0, 300.666E-0, -246.290, -418.558, -290E-4, 384e3, 427.638, -744.66, 46e0, 739.638e0, -380, -699, -270, 65.581, -752e0, -131, -669, -337E-1, -621.224e+0, -900.525E-0, 518.384, 712.396, 552.967, 82.293, -278, 925.703, -461e3, 994, -579, 5.424e-1, -501.261, -158.609e4, 780.248E+0, 809.405e2, 799.176, 940E+3, -539.365E+4, 88.185E-0, -970.107E+2, 612.331E+1, 869e2, -344, 223, 862, 593E4, 561, -78, 398, 4, 988.376, 411.175, 711.624, -709.929e+2, -159E2, -59e-4, 140.47, -758.513, 574.712, -656e+3, -326, 257.982, -386E+1, -40.776, -515.922E+1, -448.93, -238.954E+1, 776, 382.210E+3, -261.196E+3, -441.49, -429E4, 440, -907e+3, 84e+2, -292.903, 462.520E-0, -400, -155, 33.0E2, -258, -772E-2, -500, 858, 184.163E0, 455, -504.202, -660e-1, -357.380, -332.409e3, 75.653, -462e-4, 272.223, 560E-2, 983.905, 660e-3, 165, -131.808E-3, 522.134E1, -165.226E-0, 33.13, 636E-4, -85E1, 330, -422E+0, 920, -904.597E3, -84.14, 870, -764e4, 48e-3, -320.591e-1, -7E1, -963.370E-2, 924.395e+4, 863.633e0, -345e-0, 817.820e+2, -169.606, 368e3, 705E-0, 812e-1, 184, -171.392, 159.149, -131.211e1, -916, 285.143E-1, -528.570e-3, -618, -239.78, -263, 366E-4, 409, -974, -489.250, 536.300E0, -18, -134.174, 229, 468, -535e-0, -662.495e+4, -445.780e+0, -710.955e-2, -112.149E-0, -479, 926e+2, 19.275e4, -80.884E4, 610, 957e-4, -194, 553e3, 961.501, -605, -595.833E0, -403.225e-3, -443E+0, 28e4, -466.556e+4, -3, 626E+4, 833.973E+3, 392, 628e3, 721.381e-1, 612E0, 350e1, 231, 154, 94e-2, -337, -353E+3, 260.543E-2, 307, -999E+2, 197, 666.458, -961.817, 852e+0, -885, 21.604E0, 43e-1, 859.883e-3, 903.526E+2, 899e1, -208.971e1, -179.681, -923.303e+0, -299, 614e-4, 329.513, 859.653E-3, -275e-1, -339.491e-4, 240.447, 879.34, -563, -204.587, -83.319, 897, -87.390, -533e-1, -395.394E-2, -578, -210, 465, -530.572, 619, -931.380, 361.470, 743, 799.303, 775, -982e-2, -551, -121, 250e-1, 567E-3, 889.252, 273.440e4, 110, 534, 367, 783.630, -840e-0, -655, 239.406E1, -79.420e-3, 331.243, 133.737E-2, 585.572, 176, -857.566, -740e-4, 394.456e+1, 13.6e-0, 360.806, 26.606, 587.869, -128, -285.208e+1, -215, 43.728e-0, -761.149, -511.972e-3, 568.666, -131E+2, -847, 714.859, 360.905, 118, -38, -518, -891.729E2, 553, 465e-2, 713, 710E-4, 576.986, -987e0, 51.896, -637e-1, -722.850, 817.26e+3, -565, -253, 388.784e-4, 304, 874.607, 171.804e+4, 305, 497, 55.430E+2, -663.60, -551.133E+4, 601, 860, 731, 649e0, -77, 266.760E4, -243.155, 122.68e0, 779, 964.302E+3, 650, -180, 500.649, -204e0, 839.500, -630.885, 442.819e-0, 969.178e-1, -138e-0, 672, 432e0, 765, 829, 592, 90E-3, 3E1, 43, 4, -450.874, 789.932e-4, -526.44E4, -159.767e+1, 372.167e+4, 124E-2, -578, -126e1, -929.883E-2, -81.776, -59E-2, -491E-0, -260.269, 583.532, -765, 373.613, 373.343e+0, -88, 771.982, 450.976e-0, -948, -383, -677e+1, 238E0, -316.149e1, -938, -398.733, -498.769, -370.913, -233e+2, 749.836e0, -443.102E4, 100.426, 751, 922e-3, 891, -736.297, 837e+2, 770, 816.155, -656E+4, -891.111, -339.928e+2, 2.718, 191E+1, -860.207e+0, 988, 811.838, 840.86E3, 962.27, -566.975, -941, 215e3, -872.392, -539, 887.725, -286.945e4, -837.875, 64e-3, -692.821E-2, -932.743, -323.353, -582.861, -999.24E+0, -612, 365.497, -345.14, -803.86e-1, -935, -707E+0, 321, -265.592, -857.873, -566.98e3, 436.237e+4, 221, 367, 816, 87.637E0, -316e-1, -426.356, -130.397E-4, -305.314E1, -172, 790.5E-2, 219e0, -99.548E+4, -520e-0, -847E+3, 912.230e+4, -869, -483, 191E0, 324.759, -38.374e1, -472, -879E4, -216E+0, 325e+1, 20, 434.843E2, -971.734, -532.434E-3, 537e-2, 762E-0, 785E-3, 196e+0, -757.435, 856, 981.906, -891e-2, 100.70, -872.308E1, 101.57, 498.392, -521.826e-3, 867, -601.983, -656.946, -535E+1, -134.546, -899.195, 616, 397.382E3, 214.625e+4, -579E+2, -687, 525.567, -900e-1, 925E-2, 254.75, 171.348e-4, -135, -86e+4, -342, -830.355, -20E+0, 5.354E-3, 92e3, -944, 787, -272.867e0, -576e-3, -752.389E+3, 360.398, 263, -876.798, -28e3, 700, -339.76e-0, 314E0, -904, 995.805, -911E-3, -680e4, -508, 56, 283.116, 444, 435E3, 526e+3, -661, 133.529e4, -534e-2, -742e-2, -846.107, -186.10E-4, -864.595, -532.187, 323.394, 521.958, -735.866, 750.578e-0, -232.554E4, -463e-1, 186.870e+3, 217e-3, 856.537, -690.583e1, -536e+3, 10e+4, -955.966E+2, 120e4, -889E-0, 481, 31.111E-0, -241.40E-3, -351.630, -658, 43, 263.593, 612e-0, 468, -267, -423e+3, 981e+1, -661.854E3, 685.865E0, -959, -673.669e2, 723.756E0, 934.913, 589, -452.177, 683e2, -310.810, -879e-0, 998, 360.559, 893, 119.982, -421.852, 580.837, -786.477E+0, 663, -218E-4, -61.906, -958.519, -801, 257.776, 804.356, -775.673E+1, -982, 309.358, 143.647, -973.443, 326e1, -800e-2, -704, -505, 407, -384e+3, 723.575E-0, 523e-3, 453, -199, 880, -957, -24.18e-1, -483, 88E+1, 193.309, 0.850E+0, 620, 892e0, 403.236e-2, -251.555, 87, -871.511e-2, -98.337, -229.459, -926, -86.44E+3, -529.411E-4, 481E2, -437.473, -675, 801.424e+1, 580E-2, 92.166, 42E-2, 979.681, -68E3, -501.784E-4, -603, -150, 193, -511.916E3, -310, 85E-2, -727E-0, 930, -298E2, -939.796e2, 832.120, 580e+2, -898.520, -433.59, -30.687, -961, 24.951e-0, 803.430, 507.764, -855.122E-0, 169.669e-1, -586.355, 503.820, -613.902, 643, 543, 441, 213.97, -982.563E-3, -55, 480.89e-0, -300e1, -219, 99, 849.516E-0, 39, -873e-0, 507.918e+0, -731.723e-3, 63.780, -501.303e+4, 704, 30.343e+1, -580e-0, -825.414, -838.802E3, -121.456, -521, 371E3, -962.200, 297, -736E+2, -184, -502, -98.326, -550.403e+0, -694.371, -644E2, 717, 898e4, -252.548E-4, 991.338, -549.308e+4, -438, -650.988, -200.659e+0, -612, -167, -297.501, -513, -755.678E-2, -344.835E3, 551, 205, 919.617, 657.876e4, -331, 747.723, 296, -349E+1, -307E-1, -959e4, 108, -808E+0, 102, 9, 228E0, 360.829, -26.821e-2, 233E+0, 253, -682e+4, -255e+4, 740.148, -405.893, 306e-1, -810.405, 277.788, 515.535E+4, -214.682e-3, -807.221, 734, -318, 951, 526e+1, -685.389, 289.854e2, 157e+1, 867e1, -521e+2, -411, 581.788, -41e+2, -421.246e+0, 586e3, 357.456, -841E+0, -34e-3, 999.914, 408, 435.891E-3, 866.891E-2, -376.994, 271.621e-4, -51E1, 828.998e-4, 653.953e+3, -345.784e-1, 850e+3, -443.941, 187.542, -780, -752e-4, 377.59e2, 389, -561.852, 608E3, -993E4, -985.375e0, -414, 346.994, -432.789e4, -956, -904, -317, 13.396e+3, 240, -414E+1, -261.857, -563E+2, -511.27, -176, 415.865, 694, 503.571, 191.59, -220.55e0, -575, 928e+0, 508.602E4, -561, 475e+4, -619.246, -824.903E-4, 244, -762e0, 461.259, -71E4, -972.760, -657.243, -852, -366.2e4, 809, 590.9e1, 88, -295E+1, 886E-0, 102, -798E2, -422.848E1, -984, 108.259, 288.549, -180.512E+2, 489.193E2, 983E1, -383.183E+4, -190.377e-3, -541E2, -773.668, 496, -133.76E-4, 732.784e-0, 624.840e4, -652.591e0, -499, 207.290, -678E-0, -728, 328e+0, 66.836, 843E1, -682.686e3, -187.858e+3, -451.756, 541.990E+0, 452, -332, -219.883, -405, 136.754e2, -953.132, -115.235, 491.21e-4, -791.895, 807, -465.101, 488, 603.144, -695, 54.172E+3, 556, -31e0, -912, -782E+3, 469, 501, -334, -957.874E+4, 341, -549E+0, -948, 81.854E0, 846.782e+4, 236.579e3, 69e+2, -26.867, -996E0, -3.351E+0, -463.90, 323, -331E-3, 236, 219e-1, 944E3, 596.561E+1, -920.983, 614, 863E4, 755e0, -634, 551.976, 993.955e0, -267.705e+4, -497e+0, 178.570e-4, -989, -590.715E+0, 355.886, -730, 864, -127.611E-0, 171.489, -418.819, -917, -214.334e0, -53, -755.816E2, 25.109E-1, -277, -294.162, -306, 902.494, -433, 461, -805.76, -348.623E-3, 8, 772E+0, 873e-2, -0, 108.971, -565.35, -244.517, -846, 180.285E-4, -759, 236e+2, 480.184E0, 631]
2 |
--------------------------------------------------------------------------------
/dev-resources/1000-strings.json:
--------------------------------------------------------------------------------
1 | ["string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string","string"]
--------------------------------------------------------------------------------
/dev-resources/json10k.json:
--------------------------------------------------------------------------------
1 | {"results":[{"gender":"male","name":{"title":"mr","first":"علی رضا","last":"محمدخان"},"location":{"street":"4326 پارک دانشجو","city":"آبادان","state":"خوزستان","postcode":36902},"email":"علی رضا.محمدخان@example.com","login":{"username":"lazypeacock819","password":"taxman","salt":"peX2qakA","md5":"0c39ef1320ec7f799065f3b3385a2f4e","sha1":"cd51cda2b75943b111707094d8a5652542d2dff0","sha256":"a1f97d14b6a878b963482de8d6aa789f5baaf9872e510031028b213241787a73"},"dob":"1953-04-25 02:27:20","registered":"2004-10-03 21:41:05","phone":"050-46102037","cell":"0990-753-1209","id":{"name":"","value":null},"picture":{"large":"https://randomuser.me/api/portraits/men/24.jpg","medium":"https://randomuser.me/api/portraits/med/men/24.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/24.jpg"},"nat":"IR"},{"gender":"female","name":{"title":"ms","first":"andrea","last":"peña"},"location":{"street":"7112 calle de la almudena","city":"la palma","state":"islas baleares","postcode":63878},"email":"andrea.peña@example.com","login":{"username":"purpleleopard218","password":"harvard","salt":"hc9Uu10H","md5":"0d1c50b840053c61f68eb11b9ff5c44b","sha1":"5bd624f5e4567f6340cb00a07d6d9cdb2046b219","sha256":"839fee4ad0d19068b4dab72546bed62fe48ae69456e4b1b383125484510950b7"},"dob":"1953-01-27 09:21:17","registered":"2005-07-01 21:44:40","phone":"986-752-877","cell":"623-685-112","id":{"name":"DNI","value":"93357290-Y"},"picture":{"large":"https://randomuser.me/api/portraits/women/81.jpg","medium":"https://randomuser.me/api/portraits/med/women/81.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/81.jpg"},"nat":"ES"},{"gender":"female","name":{"title":"miss","first":"sue","last":"romero"},"location":{"street":"247 white oak dr","city":"mackay","state":"new south wales","postcode":1327},"email":"sue.romero@example.com","login":{"username":"orangefish402","password":"llll","salt":"AjO4nICn","md5":"19521ff63dc4e49e85c5a80ed219231c","sha1":"dde550b31afc7ff7d852e4b3252e1fa5070e00e3","sha256":"d7b83280c4eb3b095295af9b7ef51b306fc3c7df3843c26bfec320da42bbf16f"},"dob":"1952-01-13 15:26:37","registered":"2014-04-04 13:07:34","phone":"01-5269-1704","cell":"0492-962-203","id":{"name":"TFN","value":"599157736"},"picture":{"large":"https://randomuser.me/api/portraits/women/13.jpg","medium":"https://randomuser.me/api/portraits/med/women/13.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/13.jpg"},"nat":"AU"},{"gender":"female","name":{"title":"mrs","first":"donna","last":"murphy"},"location":{"street":"8988 north road","city":"greystones","state":"kilkenny","postcode":53668},"email":"donna.murphy@example.com","login":{"username":"silverkoala656","password":"33333333","salt":"wWUk5zn2","md5":"9172751676aa17561fdd3174dd4c9326","sha1":"5c073d90d70e6e72c70e8c08dde95734cdad840e","sha256":"6fe569f123ad796a7ff0649f542a2333ccacd92bd70ca6d52d64cd317cdd0a33"},"dob":"1969-09-12 21:14:56","registered":"2012-06-10 23:04:02","phone":"061-625-5539","cell":"081-759-2651","id":{"name":"PPS","value":"7736182T"},"picture":{"large":"https://randomuser.me/api/portraits/women/68.jpg","medium":"https://randomuser.me/api/portraits/med/women/68.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/68.jpg"},"nat":"IE"},{"gender":"female","name":{"title":"mademoiselle","first":"marion","last":"faure"},"location":{"street":"4623 rue de l'abbé-roger-derry","city":"gollion","state":"bern","postcode":1333},"email":"marion.faure@example.com","login":{"username":"yellowbear160","password":"747474","salt":"ZIx9zcNs","md5":"15969c42e7eaf5f2ce0492b86a165393","sha1":"7c578c86f87b28a0f396a0f14d1ce3a870b31d16","sha256":"fe1bc95f4fdbb789c80d5912c576074e03fa3ab509a6de3e6fe9ad256d67233d"},"dob":"1946-02-10 21:20:20","registered":"2005-03-07 01:57:56","phone":"(805)-136-2619","cell":"(066)-198-2825","id":{"name":"AVS","value":"756.TAAE.FXMI.39"},"picture":{"large":"https://randomuser.me/api/portraits/women/20.jpg","medium":"https://randomuser.me/api/portraits/med/women/20.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/20.jpg"},"nat":"CH"},{"gender":"female","name":{"title":"ms","first":"carmen","last":"ellis"},"location":{"street":"90 bruce st","city":"hobart","state":"south australia","postcode":7153},"email":"carmen.ellis@example.com","login":{"username":"smallrabbit833","password":"berkeley","salt":"wWW0PXS7","md5":"16cdf0978f51bcede9fe63fbf52c7744","sha1":"99bf5b7219f65c2f19a6e1b7bfe9642a8c6465a4","sha256":"d96f60965aedf1f82f2b59b1789e32230bd26495a1b7257df8efc1e92bcf537d"},"dob":"1960-12-15 09:15:17","registered":"2007-06-30 17:58:06","phone":"02-5809-6469","cell":"0428-863-957","id":{"name":"TFN","value":"346283503"},"picture":{"large":"https://randomuser.me/api/portraits/women/85.jpg","medium":"https://randomuser.me/api/portraits/med/women/85.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/85.jpg"},"nat":"AU"},{"gender":"female","name":{"title":"miss","first":"emma","last":"gregory"},"location":{"street":"8541 mill road","city":"birmingham","state":"berkshire","postcode":"T3E 2XL"},"email":"emma.gregory@example.com","login":{"username":"goldenrabbit210","password":"cameron1","salt":"UvELNRRe","md5":"0db2f9fd269b3c43d3b906f46ebf5d78","sha1":"4881220c87d85434553f31be9d79893a1f8e35fa","sha256":"a6349fffe4c01b245d2d7107059dac008711174fd88201c908736dedc3b6448e"},"dob":"1947-05-04 06:23:14","registered":"2015-03-16 01:40:15","phone":"016977 97466","cell":"0795-686-594","id":{"name":"NINO","value":"PL 94 41 63 V"},"picture":{"large":"https://randomuser.me/api/portraits/women/82.jpg","medium":"https://randomuser.me/api/portraits/med/women/82.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/82.jpg"},"nat":"GB"},{"gender":"female","name":{"title":"miss","first":"debbie","last":"gilbert"},"location":{"street":"7338 grange road","city":"letterkenny","state":"tipperary","postcode":52466},"email":"debbie.gilbert@example.com","login":{"username":"heavygoose267","password":"coventry","salt":"NS7ZrqkE","md5":"775729a0cf119f1c32fdab5bf93e1927","sha1":"15052613cdce515dc8e1fccaa0c9f9a584e48faa","sha256":"4cb3c00151faf26ed85e9ba61f22376973e755f5219633b0247a69e7ef22bc01"},"dob":"1987-02-05 19:15:58","registered":"2005-10-14 13:25:45","phone":"031-061-6514","cell":"081-331-8027","id":{"name":"PPS","value":"9152780T"},"picture":{"large":"https://randomuser.me/api/portraits/women/69.jpg","medium":"https://randomuser.me/api/portraits/med/women/69.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/69.jpg"},"nat":"IE"},{"gender":"female","name":{"title":"ms","first":"charlene","last":"miller"},"location":{"street":"2718 oak lawn ave","city":"chesapeake","state":"rhode island","postcode":16733},"email":"charlene.miller@example.com","login":{"username":"bluetiger146","password":"older","salt":"tCyeeeyO","md5":"d567deed2bd685e872c7672844d416cc","sha1":"35dd6ce7ab488a5b1a4eed2d136860c8a0e1d5dd","sha256":"c5eb257b13d5bef4ba16cd7fd7189c1c7d0e09967a44d80ebc14e12e9aa73d10"},"dob":"1982-02-22 18:45:05","registered":"2010-06-04 18:47:51","phone":"(048)-109-3917","cell":"(867)-929-6436","id":{"name":"SSN","value":"172-89-2931"},"picture":{"large":"https://randomuser.me/api/portraits/women/93.jpg","medium":"https://randomuser.me/api/portraits/med/women/93.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/93.jpg"},"nat":"US"},{"gender":"male","name":{"title":"mr","first":"آرمین","last":"نكو نظر"},"location":{"street":"5745 میرزای شیرازی","city":"ساری","state":"سمنان","postcode":68243},"email":"آرمین.نكونظر@example.com","login":{"username":"heavymouse181","password":"daewoo","salt":"lAREYXd7","md5":"0cfffb14d991d31eeb27dc04734c2595","sha1":"65a84f96764d5abf58ffcc0df42ec8632a51cb05","sha256":"adad007af1894f671f58d841321a8db8ca4cd44073c3d1d1f443bf737b49da94"},"dob":"1981-12-12 14:53:24","registered":"2007-04-02 21:59:32","phone":"089-20174880","cell":"0999-186-4226","id":{"name":"","value":null},"picture":{"large":"https://randomuser.me/api/portraits/men/90.jpg","medium":"https://randomuser.me/api/portraits/med/men/90.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/90.jpg"},"nat":"IR"},{"gender":"female","name":{"title":"miss","first":"sheila","last":"dean"},"location":{"street":"8647 robinson rd","city":"grants pass","state":"nevada","postcode":48562},"email":"sheila.dean@example.com","login":{"username":"smallelephant691","password":"qian","salt":"M7a9TwKA","md5":"0814e040674d1081eb2f690a1b0d4ef3","sha1":"47dbb30166d7af1c35eb5ad45106bd328b5f095e","sha256":"5c687ab2e9a13108c280467f4ef20c092a49d9b5be722c586e9a40db708e1bf9"},"dob":"1991-07-13 13:12:06","registered":"2008-11-27 02:19:50","phone":"(877)-584-7016","cell":"(981)-462-9845","id":{"name":"SSN","value":"295-99-5901"},"picture":{"large":"https://randomuser.me/api/portraits/women/24.jpg","medium":"https://randomuser.me/api/portraits/med/women/24.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/24.jpg"},"nat":"US"},{"gender":"female","name":{"title":"miss","first":"olivia","last":"hein"},"location":{"street":"9022 mühlenweg","city":"holzminden","state":"thüringen","postcode":65296},"email":"olivia.hein@example.com","login":{"username":"orangeleopard758","password":"excite","salt":"QdkX2c25","md5":"68052177fb719d2e57a52da40318e2fe","sha1":"cb00df46d06887e0732b3ff94bba37ed33413a64","sha256":"89147b5a4ee19723cc367fc2b7f0a4a482ce692076111bd48212b88a6d74873a"},"dob":"1988-05-02 15:15:45","registered":"2010-01-24 00:08:23","phone":"0601-9202186","cell":"0179-7462091","id":{"name":"","value":null},"picture":{"large":"https://randomuser.me/api/portraits/women/26.jpg","medium":"https://randomuser.me/api/portraits/med/women/26.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/26.jpg"},"nat":"DE"},{"gender":"male","name":{"title":"mr","first":"mathias","last":"johansen"},"location":{"street":"2875 gyvelvej","city":"askeby","state":"midtjylland","postcode":88770},"email":"mathias.johansen@example.com","login":{"username":"bigmeercat405","password":"hughes","salt":"tRVWblrQ","md5":"69220f7f2fda0a922024babf5c71f548","sha1":"64264d1f3703fdb529e4d7e2a2ce4c7bfe5e4ea6","sha256":"ad73321a24ddf3c9cc9b23e056c97e50b006cfde75c8a1f1fbb3d06e5ab04b82"},"dob":"1957-04-07 17:58:27","registered":"2003-05-10 09:57:39","phone":"13309317","cell":"70639222","id":{"name":"CPR","value":"197517-4938"},"picture":{"large":"https://randomuser.me/api/portraits/men/35.jpg","medium":"https://randomuser.me/api/portraits/med/men/35.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/35.jpg"},"nat":"DK"}],"info":{"seed":"b13cc7728ab0cf73","results":13,"page":1,"version":"1.1"}}
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Eclipse Public License - v 1.0
2 |
3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM
5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
6 |
7 | 1. DEFINITIONS
8 |
9 | "Contribution" means:
10 |
11 | a) in the case of the initial Contributor, the initial code and documentation
12 | distributed under this Agreement, and
13 | b) in the case of each subsequent Contributor:
14 | i) changes to the Program, and
15 | ii) additions to the Program;
16 |
17 | where such changes and/or additions to the Program originate from and are
18 | distributed by that particular Contributor. A Contribution 'originates'
19 | from a Contributor if it was added to the Program by such Contributor
20 | itself or anyone acting on such Contributor's behalf. Contributions do not
21 | include additions to the Program which: (i) are separate modules of
22 | software distributed in conjunction with the Program under their own
23 | license agreement, and (ii) are not derivative works of the Program.
24 |
25 | "Contributor" means any person or entity that distributes the Program.
26 |
27 | "Licensed Patents" mean patent claims licensable by a Contributor which are
28 | necessarily infringed by the use or sale of its Contribution alone or when
29 | combined with the Program.
30 |
31 | "Program" means the Contributions distributed in accordance with this
32 | Agreement.
33 |
34 | "Recipient" means anyone who receives the Program under this Agreement,
35 | including all Contributors.
36 |
37 | 2. GRANT OF RIGHTS
38 | a) Subject to the terms of this Agreement, each Contributor hereby grants
39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to
40 | reproduce, prepare derivative works of, publicly display, publicly
41 | perform, distribute and sublicense the Contribution of such Contributor,
42 | if any, and such derivative works, in source code and object code form.
43 | b) Subject to the terms of this Agreement, each Contributor hereby grants
44 | Recipient a non-exclusive, worldwide, royalty-free patent license under
45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise
46 | transfer the Contribution of such Contributor, if any, in source code and
47 | object code form. This patent license shall apply to the combination of
48 | the Contribution and the Program if, at the time the Contribution is
49 | added by the Contributor, such addition of the Contribution causes such
50 | combination to be covered by the Licensed Patents. The patent license
51 | shall not apply to any other combinations which include the Contribution.
52 | No hardware per se is licensed hereunder.
53 | c) Recipient understands that although each Contributor grants the licenses
54 | to its Contributions set forth herein, no assurances are provided by any
55 | Contributor that the Program does not infringe the patent or other
56 | intellectual property rights of any other entity. Each Contributor
57 | disclaims any liability to Recipient for claims brought by any other
58 | entity based on infringement of intellectual property rights or
59 | otherwise. As a condition to exercising the rights and licenses granted
60 | hereunder, each Recipient hereby assumes sole responsibility to secure
61 | any other intellectual property rights needed, if any. For example, if a
62 | third party patent license is required to allow Recipient to distribute
63 | the Program, it is Recipient's responsibility to acquire that license
64 | before distributing the Program.
65 | d) Each Contributor represents that to its knowledge it has sufficient
66 | copyright rights in its Contribution, if any, to grant the copyright
67 | license set forth in this Agreement.
68 |
69 | 3. REQUIREMENTS
70 |
71 | A Contributor may choose to distribute the Program in object code form under
72 | its own license agreement, provided that:
73 |
74 | a) it complies with the terms and conditions of this Agreement; and
75 | b) its license agreement:
76 | i) effectively disclaims on behalf of all Contributors all warranties
77 | and conditions, express and implied, including warranties or
78 | conditions of title and non-infringement, and implied warranties or
79 | conditions of merchantability and fitness for a particular purpose;
80 | ii) effectively excludes on behalf of all Contributors all liability for
81 | damages, including direct, indirect, special, incidental and
82 | consequential damages, such as lost profits;
83 | iii) states that any provisions which differ from this Agreement are
84 | offered by that Contributor alone and not by any other party; and
85 | iv) states that source code for the Program is available from such
86 | Contributor, and informs licensees how to obtain it in a reasonable
87 | manner on or through a medium customarily used for software exchange.
88 |
89 | When the Program is made available in source code form:
90 |
91 | a) it must be made available under this Agreement; and
92 | b) a copy of this Agreement must be included with each copy of the Program.
93 | Contributors may not remove or alter any copyright notices contained
94 | within the Program.
95 |
96 | Each Contributor must identify itself as the originator of its Contribution,
97 | if
98 | any, in a manner that reasonably allows subsequent Recipients to identify the
99 | originator of the Contribution.
100 |
101 | 4. COMMERCIAL DISTRIBUTION
102 |
103 | Commercial distributors of software may accept certain responsibilities with
104 | respect to end users, business partners and the like. While this license is
105 | intended to facilitate the commercial use of the Program, the Contributor who
106 | includes the Program in a commercial product offering should do so in a manner
107 | which does not create potential liability for other Contributors. Therefore,
108 | if a Contributor includes the Program in a commercial product offering, such
109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify
110 | every other Contributor ("Indemnified Contributor") against any losses,
111 | damages and costs (collectively "Losses") arising from claims, lawsuits and
112 | other legal actions brought by a third party against the Indemnified
113 | Contributor to the extent caused by the acts or omissions of such Commercial
114 | Contributor in connection with its distribution of the Program in a commercial
115 | product offering. The obligations in this section do not apply to any claims
116 | or Losses relating to any actual or alleged intellectual property
117 | infringement. In order to qualify, an Indemnified Contributor must:
118 | a) promptly notify the Commercial Contributor in writing of such claim, and
119 | b) allow the Commercial Contributor to control, and cooperate with the
120 | Commercial Contributor in, the defense and any related settlement
121 | negotiations. The Indemnified Contributor may participate in any such claim at
122 | its own expense.
123 |
124 | For example, a Contributor might include the Program in a commercial product
125 | offering, Product X. That Contributor is then a Commercial Contributor. If
126 | that Commercial Contributor then makes performance claims, or offers
127 | warranties related to Product X, those performance claims and warranties are
128 | such Commercial Contributor's responsibility alone. Under this section, the
129 | Commercial Contributor would have to defend claims against the other
130 | Contributors related to those performance claims and warranties, and if a
131 | court requires any other Contributor to pay any damages as a result, the
132 | Commercial Contributor must pay those damages.
133 |
134 | 5. NO WARRANTY
135 |
136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN
137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE,
139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each
140 | Recipient is solely responsible for determining the appropriateness of using
141 | and distributing the Program and assumes all risks associated with its
142 | exercise of rights under this Agreement , including but not limited to the
143 | risks and costs of program errors, compliance with applicable laws, damage to
144 | or loss of data, programs or equipment, and unavailability or interruption of
145 | operations.
146 |
147 | 6. DISCLAIMER OF LIABILITY
148 |
149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY
150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL,
151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE
155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY
156 | OF SUCH DAMAGES.
157 |
158 | 7. GENERAL
159 |
160 | If any provision of this Agreement is invalid or unenforceable under
161 | applicable law, it shall not affect the validity or enforceability of the
162 | remainder of the terms of this Agreement, and without further action by the
163 | parties hereto, such provision shall be reformed to the minimum extent
164 | necessary to make such provision valid and enforceable.
165 |
166 | If Recipient institutes patent litigation against any entity (including a
167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself
168 | (excluding combinations of the Program with other software or hardware)
169 | infringes such Recipient's patent(s), then such Recipient's rights granted
170 | under Section 2(b) shall terminate as of the date such litigation is filed.
171 |
172 | All Recipient's rights under this Agreement shall terminate if it fails to
173 | comply with any of the material terms or conditions of this Agreement and does
174 | not cure such failure in a reasonable period of time after becoming aware of
175 | such noncompliance. If all Recipient's rights under this Agreement terminate,
176 | Recipient agrees to cease use and distribution of the Program as soon as
177 | reasonably practicable. However, Recipient's obligations under this Agreement
178 | and any licenses granted by Recipient relating to the Program shall continue
179 | and survive.
180 |
181 | Everyone is permitted to copy and distribute copies of this Agreement, but in
182 | order to avoid inconsistency the Agreement is copyrighted and may only be
183 | modified in the following manner. The Agreement Steward reserves the right to
184 | publish new versions (including revisions) of this Agreement from time to
185 | time. No one other than the Agreement Steward has the right to modify this
186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The
187 | Eclipse Foundation may assign the responsibility to serve as the Agreement
188 | Steward to a suitable separate entity. Each new version of the Agreement will
189 | be given a distinguishing version number. The Program (including
190 | Contributions) may always be distributed subject to the version of the
191 | Agreement under which it was received. In addition, after a new version of the
192 | Agreement is published, Contributor may elect to distribute the Program
193 | (including its Contributions) under the new version. Except as expressly
194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or
195 | licenses to the intellectual property of any Contributor under this Agreement,
196 | whether expressly, by implication, estoppel or otherwise. All rights in the
197 | Program not expressly granted under this Agreement are reserved.
198 |
199 | This Agreement is governed by the laws of the State of New York and the
200 | intellectual property laws of the United States of America. No party to this
201 | Agreement will bring a legal action under this Agreement more than one year
202 | after the cause of action arose. Each party waives its rights to a jury trial in
203 | any resulting litigation.
204 |
205 |
206 |
--------------------------------------------------------------------------------
/epl-v10.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Eclipse Public License - Version 1.0
8 |
25 |
26 |
27 |
28 |
29 |
30 | Eclipse Public License - v 1.0
31 |
32 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE
33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR
34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS
35 | AGREEMENT.
36 |
37 | 1. DEFINITIONS
38 |
39 | "Contribution" means:
40 |
41 | a) in the case of the initial Contributor, the initial
42 | code and documentation distributed under this Agreement, and
43 | b) in the case of each subsequent Contributor:
44 | i) changes to the Program, and
45 | ii) additions to the Program;
46 | where such changes and/or additions to the Program
47 | originate from and are distributed by that particular Contributor. A
48 | Contribution 'originates' from a Contributor if it was added to the
49 | Program by such Contributor itself or anyone acting on such
50 | Contributor's behalf. Contributions do not include additions to the
51 | Program which: (i) are separate modules of software distributed in
52 | conjunction with the Program under their own license agreement, and (ii)
53 | are not derivative works of the Program.
54 |
55 | "Contributor" means any person or entity that distributes
56 | the Program.
57 |
58 | "Licensed Patents" mean patent claims licensable by a
59 | Contributor which are necessarily infringed by the use or sale of its
60 | Contribution alone or when combined with the Program.
61 |
62 | "Program" means the Contributions distributed in accordance
63 | with this Agreement.
64 |
65 | "Recipient" means anyone who receives the Program under
66 | this Agreement, including all Contributors.
67 |
68 | 2. GRANT OF RIGHTS
69 |
70 | a) Subject to the terms of this Agreement, each
71 | Contributor hereby grants Recipient a non-exclusive, worldwide,
72 | royalty-free copyright license to reproduce, prepare derivative works
73 | of, publicly display, publicly perform, distribute and sublicense the
74 | Contribution of such Contributor, if any, and such derivative works, in
75 | source code and object code form.
76 |
77 | b) Subject to the terms of this Agreement, each
78 | Contributor hereby grants Recipient a non-exclusive, worldwide,
79 | royalty-free patent license under Licensed Patents to make, use, sell,
80 | offer to sell, import and otherwise transfer the Contribution of such
81 | Contributor, if any, in source code and object code form. This patent
82 | license shall apply to the combination of the Contribution and the
83 | Program if, at the time the Contribution is added by the Contributor,
84 | such addition of the Contribution causes such combination to be covered
85 | by the Licensed Patents. The patent license shall not apply to any other
86 | combinations which include the Contribution. No hardware per se is
87 | licensed hereunder.
88 |
89 | c) Recipient understands that although each Contributor
90 | grants the licenses to its Contributions set forth herein, no assurances
91 | are provided by any Contributor that the Program does not infringe the
92 | patent or other intellectual property rights of any other entity. Each
93 | Contributor disclaims any liability to Recipient for claims brought by
94 | any other entity based on infringement of intellectual property rights
95 | or otherwise. As a condition to exercising the rights and licenses
96 | granted hereunder, each Recipient hereby assumes sole responsibility to
97 | secure any other intellectual property rights needed, if any. For
98 | example, if a third party patent license is required to allow Recipient
99 | to distribute the Program, it is Recipient's responsibility to acquire
100 | that license before distributing the Program.
101 |
102 | d) Each Contributor represents that to its knowledge it
103 | has sufficient copyright rights in its Contribution, if any, to grant
104 | the copyright license set forth in this Agreement.
105 |
106 | 3. REQUIREMENTS
107 |
108 | A Contributor may choose to distribute the Program in object code
109 | form under its own license agreement, provided that:
110 |
111 | a) it complies with the terms and conditions of this
112 | Agreement; and
113 |
114 | b) its license agreement:
115 |
116 | i) effectively disclaims on behalf of all Contributors
117 | all warranties and conditions, express and implied, including warranties
118 | or conditions of title and non-infringement, and implied warranties or
119 | conditions of merchantability and fitness for a particular purpose;
120 |
121 | ii) effectively excludes on behalf of all Contributors
122 | all liability for damages, including direct, indirect, special,
123 | incidental and consequential damages, such as lost profits;
124 |
125 | iii) states that any provisions which differ from this
126 | Agreement are offered by that Contributor alone and not by any other
127 | party; and
128 |
129 | iv) states that source code for the Program is available
130 | from such Contributor, and informs licensees how to obtain it in a
131 | reasonable manner on or through a medium customarily used for software
132 | exchange.
133 |
134 | When the Program is made available in source code form:
135 |
136 | a) it must be made available under this Agreement; and
137 |
138 | b) a copy of this Agreement must be included with each
139 | copy of the Program.
140 |
141 | Contributors may not remove or alter any copyright notices contained
142 | within the Program.
143 |
144 | Each Contributor must identify itself as the originator of its
145 | Contribution, if any, in a manner that reasonably allows subsequent
146 | Recipients to identify the originator of the Contribution.
147 |
148 | 4. COMMERCIAL DISTRIBUTION
149 |
150 | Commercial distributors of software may accept certain
151 | responsibilities with respect to end users, business partners and the
152 | like. While this license is intended to facilitate the commercial use of
153 | the Program, the Contributor who includes the Program in a commercial
154 | product offering should do so in a manner which does not create
155 | potential liability for other Contributors. Therefore, if a Contributor
156 | includes the Program in a commercial product offering, such Contributor
157 | ("Commercial Contributor") hereby agrees to defend and
158 | indemnify every other Contributor ("Indemnified Contributor")
159 | against any losses, damages and costs (collectively "Losses")
160 | arising from claims, lawsuits and other legal actions brought by a third
161 | party against the Indemnified Contributor to the extent caused by the
162 | acts or omissions of such Commercial Contributor in connection with its
163 | distribution of the Program in a commercial product offering. The
164 | obligations in this section do not apply to any claims or Losses
165 | relating to any actual or alleged intellectual property infringement. In
166 | order to qualify, an Indemnified Contributor must: a) promptly notify
167 | the Commercial Contributor in writing of such claim, and b) allow the
168 | Commercial Contributor to control, and cooperate with the Commercial
169 | Contributor in, the defense and any related settlement negotiations. The
170 | Indemnified Contributor may participate in any such claim at its own
171 | expense.
172 |
173 | For example, a Contributor might include the Program in a commercial
174 | product offering, Product X. That Contributor is then a Commercial
175 | Contributor. If that Commercial Contributor then makes performance
176 | claims, or offers warranties related to Product X, those performance
177 | claims and warranties are such Commercial Contributor's responsibility
178 | alone. Under this section, the Commercial Contributor would have to
179 | defend claims against the other Contributors related to those
180 | performance claims and warranties, and if a court requires any other
181 | Contributor to pay any damages as a result, the Commercial Contributor
182 | must pay those damages.
183 |
184 | 5. NO WARRANTY
185 |
186 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION,
189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY
190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
191 | responsible for determining the appropriateness of using and
192 | distributing the Program and assumes all risks associated with its
193 | exercise of rights under this Agreement , including but not limited to
194 | the risks and costs of program errors, compliance with applicable laws,
195 | damage to or loss of data, programs or equipment, and unavailability or
196 | interruption of operations.
197 |
198 | 6. DISCLAIMER OF LIABILITY
199 |
200 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT,
202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING
203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR
206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
208 |
209 | 7. GENERAL
210 |
211 | If any provision of this Agreement is invalid or unenforceable under
212 | applicable law, it shall not affect the validity or enforceability of
213 | the remainder of the terms of this Agreement, and without further action
214 | by the parties hereto, such provision shall be reformed to the minimum
215 | extent necessary to make such provision valid and enforceable.
216 |
217 | If Recipient institutes patent litigation against any entity
218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the
219 | Program itself (excluding combinations of the Program with other
220 | software or hardware) infringes such Recipient's patent(s), then such
221 | Recipient's rights granted under Section 2(b) shall terminate as of the
222 | date such litigation is filed.
223 |
224 | All Recipient's rights under this Agreement shall terminate if it
225 | fails to comply with any of the material terms or conditions of this
226 | Agreement and does not cure such failure in a reasonable period of time
227 | after becoming aware of such noncompliance. If all Recipient's rights
228 | under this Agreement terminate, Recipient agrees to cease use and
229 | distribution of the Program as soon as reasonably practicable. However,
230 | Recipient's obligations under this Agreement and any licenses granted by
231 | Recipient relating to the Program shall continue and survive.
232 |
233 | Everyone is permitted to copy and distribute copies of this
234 | Agreement, but in order to avoid inconsistency the Agreement is
235 | copyrighted and may only be modified in the following manner. The
236 | Agreement Steward reserves the right to publish new versions (including
237 | revisions) of this Agreement from time to time. No one other than the
238 | Agreement Steward has the right to modify this Agreement. The Eclipse
239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may
240 | assign the responsibility to serve as the Agreement Steward to a
241 | suitable separate entity. Each new version of the Agreement will be
242 | given a distinguishing version number. The Program (including
243 | Contributions) may always be distributed subject to the version of the
244 | Agreement under which it was received. In addition, after a new version
245 | of the Agreement is published, Contributor may elect to distribute the
246 | Program (including its Contributions) under the new version. Except as
247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no
248 | rights or licenses to the intellectual property of any Contributor under
249 | this Agreement, whether expressly, by implication, estoppel or
250 | otherwise. All rights in the Program not expressly granted under this
251 | Agreement are reserved.
252 |
253 | This Agreement is governed by the laws of the State of New York and
254 | the intellectual property laws of the United States of America. No party
255 | to this Agreement will bring a legal action under this Agreement more
256 | than one year after the cause of action arose. Each party waives its
257 | rights to a jury trial in any resulting litigation.
258 |
259 |
260 |
261 |
262 |
--------------------------------------------------------------------------------
/src/test/clojure/clojure/data/json_test_suite_test.clj:
--------------------------------------------------------------------------------
1 | (ns clojure.data.json-test-suite-test
2 | (:require [clojure.data.json :as json]
3 | [clojure.test :refer :all]
4 | [clojure.string :as str]))
5 |
6 | (deftest i-number-double-huge-neg-exp-test
7 | (is (= [0.0] (json/read-str "[123.456e-789]"))))
8 |
9 | (deftest i-number-huge-exp-test
10 | (is (= [##Inf]
11 | (json/read-str "[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006]"))))
12 |
13 | (deftest i-number-neg-int-huge-exp-test
14 | (is (= [##-Inf] (json/read-str "[-1e+9999]"))))
15 |
16 | (deftest i-number-pos-double-huge-exp-test
17 | (is (= [##Inf] (json/read-str "[1.5e+9999]"))))
18 |
19 | (deftest i-number-real-neg-overflow-test
20 | (is (= [##-Inf] (json/read-str "[-123123e100000]"))))
21 |
22 | (deftest i-number-real-pos-overflow-test
23 | (is (= [##Inf] (json/read-str "[123123e100000]"))))
24 |
25 | (deftest i-number-real-underflow-test
26 | (is (= [0.0] (json/read-str "[123e-10000000]"))))
27 |
28 | (deftest i-number-too-big-neg-int-test
29 | (is (= [-123123123123123123123123123123N]
30 | (json/read-str "[-123123123123123123123123123123]"))))
31 |
32 | (deftest i-number-too-big-pos-int-test
33 | (is (= [100000000000000000000N] (json/read-str "[100000000000000000000]"))))
34 |
35 | (deftest i-number-very-big-negative-int-test
36 | (is (= [-237462374673276894279832749832423479823246327846N]
37 | (json/read-str "[-237462374673276894279832749832423479823246327846]"))))
38 |
39 | (deftest n-array-1-true-without-comma-test
40 | (is (thrown? Exception (json/read-str "[1 true]"))))
41 |
42 | (deftest n-array-colon-instead-of-comma-test
43 | (is (thrown? Exception (json/read-str "[\"\": 1]"))))
44 |
45 | (deftest n-array-comma-and-number-test
46 | (is (thrown? Exception (json/read-str "[,1]"))))
47 |
48 | (deftest n-array-double-comma-test
49 | (is (thrown? Exception (json/read-str "[1,,2]"))))
50 |
51 | (deftest n-array-double-extra-comma-test
52 | (is (thrown? Exception (json/read-str "[\"x\",,]"))))
53 |
54 | (deftest n-array-extra-comma-test
55 | (is (thrown? Exception (json/read-str "[\"\",]"))))
56 |
57 | (deftest n-array-incomplete-invalid-value-test
58 | (is (thrown? Exception (json/read-str "[x"))))
59 |
60 | (deftest n-array-incomplete-test
61 | (is (thrown? Exception (json/read-str "[\"x\""))))
62 |
63 | (deftest n-array-inner-array-no-comma-test
64 | (is (thrown? Exception (json/read-str "[3[4]]"))))
65 |
66 | (deftest n-array-items-separated-by-semicolon-test
67 | (is (thrown? Exception (json/read-str "[1:2]"))))
68 |
69 | (deftest n-array-just-comma-test
70 | (is (thrown? Exception (json/read-str "[,]"))))
71 |
72 | (deftest n-array-just-minus-test
73 | (is (thrown? Exception (json/read-str "[-]"))))
74 |
75 | (deftest n-array-missing-value-test
76 | (is (thrown? Exception (json/read-str "[ , \"\"]"))))
77 |
78 | (deftest n-array-newlines-unclosed-test
79 | (is (thrown? Exception (json/read-str "[\"a\",\n4\n,1,"))))
80 |
81 | (deftest n-array-number-and-comma-test
82 | (is (thrown? Exception (json/read-str "[1,]"))))
83 |
84 | (deftest n-array-number-and-several-commas-test
85 | (is (thrown? Exception (json/read-str "[1,,]"))))
86 |
87 | (deftest n-array-spaces-vertical-tab-formfeed-test
88 | (is (thrown? Exception (json/read-str "[\"a\"\\f]"))))
89 |
90 | (deftest n-array-star-inside-test
91 | (is (thrown? Exception (json/read-str "[*]"))))
92 |
93 | (deftest n-array-unclosed-test
94 | (is (thrown? Exception (json/read-str "[\"\""))))
95 |
96 | (deftest n-array-unclosed-trailing-comma-test
97 | (is (thrown? Exception (json/read-str "[1,"))))
98 |
99 | (deftest n-array-unclosed-with-new-lines-test
100 | (is (thrown? Exception (json/read-str "[1,\n1\n,1"))))
101 |
102 | (deftest n-array-unclosed-with-object-inside-test
103 | (is (thrown? Exception (json/read-str "[{}"))))
104 |
105 | (deftest n-number-++-test
106 | (is (thrown? Exception (json/read-str "[++1234]"))))
107 |
108 | (deftest n-number-+1-test
109 | (is (thrown? Exception (json/read-str "[+1]"))))
110 |
111 | (deftest n-number-+Inf-test
112 | (is (thrown? Exception (json/read-str "[+Inf]"))))
113 |
114 | (deftest n-number--01-test
115 | (is (thrown? Exception (json/read-str "[-01]"))))
116 |
117 | (deftest n-number--1.0.-test
118 | (is (thrown? Exception (json/read-str "[-1.0.]"))))
119 |
120 | (deftest n-number--2.-test
121 | (is (thrown? Exception (json/read-str "[-2.]"))))
122 |
123 | (deftest n-number--NaN-test
124 | (is (thrown? Exception (json/read-str "[-NaN]"))))
125 |
126 | (deftest n-number-.-1-test
127 | (is (thrown? Exception (json/read-str "[.-1]"))))
128 |
129 | (deftest n-number-.2e-3-test
130 | (is (thrown? Exception (json/read-str "[.2e-3]"))))
131 |
132 | (deftest n-number-0-capital-E+-test
133 | (is (thrown? Exception (json/read-str "[0E+]"))))
134 |
135 | (deftest n-number-0-capital-E-test
136 | (is (thrown? Exception (json/read-str "[0E]"))))
137 |
138 | (deftest n-number-0.1.2-test
139 | (is (thrown? Exception (json/read-str "[0.1.2]"))))
140 |
141 | (deftest n-number-0.3e+-test
142 | (is (thrown? Exception (json/read-str "[0.3e+]"))))
143 |
144 | (deftest n-number-0.3e-test
145 | (is (thrown? Exception (json/read-str "[0.3e]"))))
146 |
147 | (deftest n-number-0.e1-test
148 | (is (thrown? Exception (json/read-str "[0.e1]"))))
149 |
150 | (deftest n-number-0e+-test
151 | (is (thrown? Exception (json/read-str "[0e+]"))))
152 |
153 | (deftest n-number-0e-test
154 | (is (thrown? Exception (json/read-str "[0e]"))))
155 |
156 | (deftest n-number-1-000-test
157 | (is (thrown? Exception (json/read-str "[1 000.0]"))))
158 |
159 | (deftest n-number-1.0e+-test
160 | (is (thrown? Exception (json/read-str "[1.0e+]"))))
161 |
162 | (deftest n-number-1.0e--test
163 | (is (thrown? Exception (json/read-str "[1.0e-]"))))
164 |
165 | (deftest n-number-1.0e-test
166 | (is (thrown? Exception (json/read-str "[1.0e]"))))
167 |
168 | (deftest n-number-1eE2-test
169 | (is (thrown? Exception (json/read-str "[1eE2]"))))
170 |
171 | (deftest n-number-2.e+3-test
172 | (is (thrown? Exception (json/read-str "[2.e+3]"))))
173 |
174 | (deftest n-number-2.e-3-test
175 | (is (thrown? Exception (json/read-str "[2.e-3]"))))
176 |
177 | (deftest n-number-2.e3-test
178 | (is (thrown? Exception (json/read-str "[2.e3]"))))
179 |
180 | (deftest n-number-9.e+-test
181 | (is (thrown? Exception (json/read-str "[9.e+]"))))
182 |
183 | (deftest n-number-Inf-test
184 | (is (thrown? Exception (json/read-str "[Inf]"))))
185 |
186 | (deftest n-number-NaN-test
187 | (is (thrown? Exception (json/read-str "[NaN]"))))
188 |
189 | (deftest n-number-expression-test
190 | (is (thrown? Exception (json/read-str "[1+2]"))))
191 |
192 | (deftest n-number-hex-1-digit-test
193 | (is (thrown? Exception (json/read-str "[0x1]"))))
194 |
195 | (deftest n-number-hex-2-digits-test
196 | (is (thrown? Exception (json/read-str "[0x42]"))))
197 |
198 | (deftest n-number-infinity-test
199 | (is (thrown? Exception (json/read-str "[Infinity]"))))
200 |
201 | (deftest n-number-invalid+--test
202 | (is (thrown? Exception (json/read-str "[0e+-1]"))))
203 |
204 | (deftest n-number-invalid-negative-real-test
205 | (is (thrown? Exception (json/read-str "[-123.123foo]"))))
206 |
207 | (deftest n-number-minus-infinity-test
208 | (is (thrown? Exception (json/read-str "[-Infinity]"))))
209 |
210 | (deftest n-number-minus-sign-with-trailing-garbage-test
211 | (is (thrown? Exception (json/read-str "[-foo]"))))
212 |
213 | (deftest n-number-minus-space-1-test
214 | (is (thrown? Exception (json/read-str "[- 1]"))))
215 |
216 | (deftest n-number-neg-int-starting-with-zero-test
217 | (is (thrown? Exception (json/read-str "[-012]"))))
218 |
219 | (deftest n-number-neg-real-without-int-part-test
220 | (is (thrown? Exception (json/read-str "[-.123]"))))
221 |
222 | (deftest n-number-neg-with-garbage-at-end-test
223 | (is (thrown? Exception (json/read-str "[-1x]"))))
224 |
225 | (deftest n-number-real-garbage-after-e-test
226 | (is (thrown? Exception (json/read-str "[1ea]"))))
227 |
228 | (deftest n-number-real-without-fractional-part-test
229 | (is (thrown? Exception (json/read-str "[1.]"))))
230 |
231 | (deftest n-number-starting-with-dot-test
232 | (is (thrown? Exception (json/read-str "[.123]"))))
233 |
234 | (deftest n-number-with-alpha-char-test
235 | (is (thrown? Exception (json/read-str "[1.8011670033376514H-308]"))))
236 |
237 | (deftest n-number-with-alpha-test
238 | (is (thrown? Exception (json/read-str "[1.2a-3]"))))
239 |
240 | (deftest n-number-with-leading-zero-test
241 | (is (thrown? Exception (json/read-str "[012]"))))
242 |
243 | (deftest n-object-non-string-key-but-huge-number-instead-test
244 | (is (thrown? Exception (json/read-str "{9999E9999:1}"))))
245 |
246 | (deftest n-structure-array-with-unclosed-string-test
247 | (is (thrown? Exception (json/read-str "[\"asd]"))))
248 |
249 | (deftest n-structure-end-array-test
250 | (is (thrown? Exception (json/read-str "]"))))
251 |
252 | (deftest n-structure-number-with-trailing-garbage-test
253 | (is (thrown? Exception (json/read-str "2@"))))
254 |
255 | (deftest n-structure-open-array-apostrophe-test
256 | (is (thrown? Exception (json/read-str "['"))))
257 |
258 | (deftest n-structure-open-array-comma-test
259 | (is (thrown? Exception (json/read-str "[,"))))
260 |
261 | (deftest n-structure-open-array-open-object-test
262 | (is (thrown? Exception (json/read-str "[{"))))
263 |
264 | (deftest n-structure-open-array-open-string-test
265 | (is (thrown? Exception (json/read-str "[\"a"))))
266 |
267 | (deftest n-structure-open-array-string-test
268 | (is (thrown? Exception (json/read-str "[\"a\""))))
269 |
270 | (deftest n-structure-open-object-close-array-test
271 | (is (thrown? Exception (json/read-str "{]"))))
272 |
273 | (deftest n-structure-open-object-open-array-test
274 | (is (thrown? Exception (json/read-str "{["))))
275 |
276 | (deftest n-structure-unclosed-array-partial-null-test
277 | (is (thrown? Exception (json/read-str "[ false, nul"))))
278 |
279 | (deftest n-structure-unclosed-array-test
280 | (is (thrown? Exception (json/read-str "[1"))))
281 |
282 | (deftest n-structure-unclosed-array-unfinished-false-test
283 | (is (thrown? Exception (json/read-str "[ true, fals"))))
284 |
285 | (deftest n-structure-unclosed-array-unfinished-true-test
286 | (is (thrown? Exception (json/read-str "[ false, tru"))))
287 |
288 | (deftest y-array-arraysWithSpaces-test
289 | (is (= [[]] (json/read-str "[[] ]"))))
290 |
291 | (deftest y-array-empty-string-test
292 | (is (= [""] (json/read-str "[\"\"]"))))
293 |
294 | (deftest y-array-empty-test
295 | (is (= [] (json/read-str "[]"))))
296 |
297 | (deftest y-array-ending-with-newline-test
298 | (is (= ["a"] (json/read-str "[\"a\"]"))))
299 |
300 | (deftest y-array-false-test
301 | (is (= [false] (json/read-str "[false]"))))
302 |
303 | (deftest y-array-heterogeneous-test
304 | (is (= [nil 1 "1" {}] (json/read-str "[null, 1, \"1\", {}]"))))
305 |
306 | (deftest y-array-null-test
307 | (is (= [nil] (json/read-str "[null]"))))
308 |
309 | (deftest y-array-with-1-and-newline-test
310 | (is (= [1] (json/read-str "[1\n]"))))
311 |
312 | (deftest y-array-with-leading-space-test
313 | (is (= [1] (json/read-str " [1]"))))
314 |
315 | (deftest y-array-with-several-null-test
316 | (is (= [1 nil nil nil 2] (json/read-str "[1,null,null,null,2]"))))
317 |
318 | (deftest y-array-with-trailing-space-test
319 | (is (= [2] (json/read-str "[2] "))))
320 |
321 | (deftest y-number-0e+1-test
322 | (is (= [0.0] (json/read-str "[0e+1]"))))
323 |
324 | (deftest y-number-0e1-test
325 | (is (= [0.0] (json/read-str "[0e1]"))))
326 |
327 | (deftest y-number-after-space-test
328 | (is (= [4] (json/read-str "[ 4]"))))
329 |
330 | (deftest y-number-double-close-to-zero-test
331 | (is (= [-1.0E-78]
332 | (json/read-str "[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001]"))))
333 |
334 | (deftest y-number-int-with-exp-test
335 | (is (= [200.0] (json/read-str "[20e1]"))))
336 |
337 | (deftest y-number-minus-zero-test
338 | (is (= [0] (json/read-str "[-0]"))))
339 |
340 | (deftest y-number-negative-int-test
341 | (is (= [-123] (json/read-str "[-123]"))))
342 |
343 | (deftest y-number-negative-one-test
344 | (is (= [-1] (json/read-str "[-1]"))))
345 |
346 | (deftest y-number-negative-zero-test
347 | (is (= [0] (json/read-str "[-0]"))))
348 |
349 | (deftest y-number-real-capital-e-neg-exp-test
350 | (is (= [0.01] (json/read-str "[1E-2]"))))
351 |
352 | (deftest y-number-real-capital-e-pos-exp-test
353 | (is (= [100.0] (json/read-str "[1E+2]"))))
354 |
355 | (deftest y-number-real-capital-e-test
356 | (is (= [1.0E22] (json/read-str "[1E22]"))))
357 |
358 | (deftest y-number-real-exponent-test
359 | (is (= [1.23E47] (json/read-str "[123e45]"))))
360 |
361 | (deftest y-number-real-fraction-exponent-test
362 | (is (= [1.23456E80] (json/read-str "[123.456e78]"))))
363 |
364 | (deftest y-number-real-neg-exp-test
365 | (is (= [0.01] (json/read-str "[1e-2]"))))
366 |
367 | (deftest y-number-real-pos-exponent-test
368 | (is (= [100.0] (json/read-str "[1e+2]"))))
369 |
370 | (deftest y-number-simple-int-test
371 | (is (= [123] (json/read-str "[123]"))))
372 |
373 | (deftest y-number-simple-real-test
374 | (is (= [123.456789] (json/read-str "[123.456789]"))))
375 |
376 | (deftest y-number-test
377 | (is (= [1.23E67] (json/read-str "[123e65]"))))
378 |
379 | (deftest y-object-extreme-numbers-test
380 | (is (= {"min" -1.0E28, "max" 1.0E28}
381 | (json/read-str "{\"min\": -1.0e+28, \"max\": 1.0e+28}"))))
382 |
383 | (deftest y-string-in-array-test
384 | (is (= ["asd"] (json/read-str "[\"asd\"]"))))
385 |
386 | (deftest y-string-in-array-with-leading-space-test
387 | (is (= ["asd"] (json/read-str "[ \"asd\"]"))))
388 |
389 | (deftest y-structure-true-in-array-test
390 | (is (= [true] (json/read-str "[true]"))))
391 |
392 | (deftest y-structure-whitespace-array-test
393 | (is (= [] (json/read-str " [] "))))
394 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | data.json
2 | ========================================
3 |
4 | JSON parser/generator to/from Clojure data structures.
5 |
6 | Key goals:
7 | * Compliant with JSON spec per https://json.org/
8 | * No external dependencies
9 |
10 |
11 | Releases and Dependency Information
12 | ----------------------------------------
13 |
14 | This project follows the version scheme MAJOR.MINOR.PATCH where each component provides some relative indication of the size of the change, but does not follow semantic versioning. In general, all changes endeavor to be non-breaking (by moving to new names rather than by breaking existing names).
15 |
16 | Latest stable release is [2.5.1]
17 |
18 | [CLI/`deps.edn`](https://clojure.org/reference/deps_and_cli) dependency information:
19 | ```clojure
20 | org.clojure/data.json {:mvn/version "2.5.1"}
21 | ```
22 |
23 | [Leiningen] dependency information:
24 |
25 | [org.clojure/data.json "2.5.1"]
26 |
27 | [Maven] dependency information:
28 |
29 |
30 | org.clojure
31 | data.json
32 | 2.5.1
33 |
34 |
35 | [Leiningen]: https://leiningen.org/
36 | [Maven]: https://maven.apache.org/
37 |
38 |
39 | Other versions:
40 |
41 | * [All Released Versions](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22data.json%22)
42 | * [Development Snapshots](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~data.json~~~)
43 | * [Development Snapshot Repositories](https://clojure.org/releases/downloads#_using_clojure_snapshot_releases)
44 |
45 |
46 |
47 | Usage
48 | ----------------------------------------
49 |
50 | [API Documentation](https://clojure.github.io/data.json/)
51 |
52 | Example usage:
53 |
54 | (ns example
55 | (:require [clojure.data.json :as json]))
56 |
57 | To convert to/from JSON strings, use `json/write-str` and `json/read-str`:
58 |
59 | (json/write-str {:a 1 :b 2})
60 | ;;=> "{\"a\":1,\"b\":2}"
61 |
62 | (json/read-str "{\"a\":1,\"b\":2}")
63 | ;;=> {"a" 1, "b" 2}
64 |
65 | Note that these operations are not symmetric: converting Clojure data
66 | into JSON is lossy.
67 |
68 |
69 | ### Converting Key/Value Types
70 |
71 | You can specify a `:key-fn` to convert map keys on the way in or out:
72 |
73 | (json/read-str "{\"a\":1,\"b\":2}"
74 | :key-fn keyword)
75 | ;;=> {:a 1, :b 2}
76 |
77 | (json/write-str {:a 1 :b 2}
78 | :key-fn #(.toUpperCase %))
79 | ;;=> "{\"A\":1,\"B\":2}"
80 |
81 | (json/read-str "{\"a\":1,\"b\":2}"
82 | :key-fn #(keyword "com.example" %))
83 | ;;=> {:com.example/a 1, :com.example/b 2}
84 |
85 | You can specify a `:value-fn` to convert map values on the way in or
86 | out. The value-fn will be called with two arguments, the key and the
87 | value, and it returns the updated value.
88 |
89 | (defn my-value-reader [key value]
90 | (if (= key :date)
91 | (java.sql.Date/valueOf value)
92 | value))
93 |
94 | (json/read-str "{\"number\":42,\"date\":\"2012-06-02\"}"
95 | :value-fn my-value-reader
96 | :key-fn keyword)
97 | ;;=> {:number 42, :date #inst "2012-06-02T04:00:00.000-00:00"}
98 |
99 | Be aware that `:value-fn` only works on maps (JSON objects). If your
100 | root data structure is, for example, a vector of dates, you will need
101 | to pre- or post-process it outside of data.json. [clojure.walk] may be
102 | useful for this.
103 |
104 | [clojure.walk]: https://clojure.github.io/clojure/clojure.walk-api.html
105 |
106 |
107 | ### Order of key-fn / value-fn
108 |
109 | If you specify both a `:key-fn` and a `:value-fn` when **reading**,
110 | the value-fn is called **after** the key has been processed by the
111 | key-fn.
112 |
113 | The **reverse** is true when **writing**:
114 |
115 | (defn my-value-writer [key value]
116 | (if (= key :date)
117 | (str (java.sql.Date. (.getTime value)))
118 | value))
119 |
120 | (json/write-str {:number 42, :date (java.util.Date. 112 5 2)}
121 | :value-fn my-value-writer
122 | :key-fn name)
123 | ;;=> "{\"number\":42,\"date\":\"2012-06-02\"}"
124 |
125 |
126 | ### Reading/Writing a Stream
127 |
128 | You can also read JSON directly from a java.io.Reader with `json/read`
129 | and write JSON directly to a java.io.Writer with `json/write`.
130 |
131 |
132 | ### More
133 |
134 | Other options are available. Refer to the [API Documentation] for details.
135 |
136 | [API Documentation]: https://clojure.github.io/data.json/
137 |
138 |
139 |
140 | Developer Information
141 | ----------------------------------------
142 |
143 | * [GitHub project](https://github.com/clojure/data.json)
144 | * [How to contribute](https://clojure.org/community/contributing)
145 | * [Bug Tracker](https://clojure.atlassian.net/browse/DJSON)
146 | * [Continuous Integration](https://github.com/clojure/data.json/actions/workflows/test.yml)
147 |
148 |
149 |
150 | Change Log
151 | ----------------------------------------
152 |
153 | * Next
154 | * Fix: [DJSON-56] During `read`, better error messages for chars < 32
155 | * Release [2.5.1] on 2024-Nov-26
156 | * Fix: `read` of JSON number followed by EOF can break subsequent read on supplier PBR from seeing EOF
157 | * Fix: `read` docstring updated to specify minimum buffer size when PushbackReader supplied (64)
158 | * Release [2.5.0] on 2023-Dec-21
159 | * Fix [DJSON-50]: `read` can take a PushbackReader for repeated read use case
160 | * Fix `write` docstring to add `:indent` option added in [DJSON-18]
161 | * Fix [DJSON-57]: Throw better exception when EOF encountered while reading array or object
162 | * Add [DJSON-46]: In `read`, add `:extra-data-fn` that can be provided to cause an eof check after value is read
163 | * Add [DJSON-54]: In `write`, add custom fallback fn for writing unknown types
164 | * Perf [DJSON-61]: Faster string writing when string is "simple"
165 | * Perf: Faster string writing when string is not simple
166 | * Perf: Faster `read-str`
167 | * Release [2.4.0] on 2021-Jul-12
168 | * Fix [DJSON-52]: Remove Classloader workaround to support Clojure 1.2.x and below
169 | * Fix [DJSON-53]: Move deprecated API functions from compat ns into main ns
170 | * Release [2.3.1] on 2021-May-19
171 | * Fix [DJSON-51]: Fix possible read of x00's in quoted strings on partial stream read
172 | * Release [2.3.0] on 2021-May-14
173 | * Fix [DJSON-48]: Make array parsing match spec
174 | * Fix [DJSON-18]: Make pprint-json much faster
175 | * Release [2.2.3] on 2021-May-6
176 | * Fix [DJSON-47]: Make number parsing match spec (reject invalid numbers)
177 | * Release [2.2.2] on 2021-Apr-26
178 | * Perf [DJSON-36]: Reapplied updated refactored code in read-array and read-object
179 | * Add [DJSON-45]: Generative tests for read/write roundtrip
180 | * Release [2.2.1] on 2021-Apr-19
181 | * Revert [DJSON-36]: Problem with transient batching in that change
182 | * Release [2.2.0] on 2021-Apr-16
183 | * Add [DJSON-41]: New support for writing java.time.Instant and java.util.Date (+ subclasses)
184 | * New options to `write` functions:
185 | * `:date-formatter` - `java.time.DateTimeFormatter` to use (default `DateTimeFormatter/ISO_INSTANT`)
186 | * `:sql-date-converter` - fn to convert `java.sql.Date` to `java.time.Instant` (default provided)
187 | * Perf [DJSON-36]: Refactor code in read-array and read-object
188 | * Release [2.1.1] on 2021-Apr-15
189 | * Fix [DJSON-43]: Fix buffer overflow in pushbackreader
190 | * Update parent pom to latest (1.1.0)
191 | * Release [2.1.0] on 2021-Apr-6
192 | * Fix [DJSON-39]: Support writing UUIDs (as strings)
193 | * Release [2.0.2] on 2021-Mar-27
194 | * Fix [DJSON-38]: Type-hint return type of write-str
195 | * Fix [DJSON-40]: Make named argument passing compatible with new
196 | * Release [2.0.1] on 2021-Mar-19
197 | * Fix [DJSON-37]: Fix off-by-one error reading long strings, regression in 2.0.0
198 | * Release [2.0.0] on 2021-Mar-19
199 | * Perf [DJSON-35]: Replace PrintWriter with more generic Appendable, reduce wrapping
200 | * Perf [DJSON-34]: More efficient writing for common path
201 | * Perf [DJSON-32]: Use option map instead of dynamic variables (affects read+write)
202 | * NOTE: Includes a breaking change in the internal JSONWriter protocol method signature
203 | * Perf [DJSON-33]: Improve speed of reading JSON strings
204 | * Fix [DJSON-30]: Fix bad test
205 | * Release [1.1.0] on 2021-Mar-5
206 | * Fix [DJSON-26]: write-object should check seq on loop var, not param
207 | * Use latest parent pom (will bump default clojure dep to 1.8.0)
208 | * Use direct linking on "aot" classifier lib
209 | * Release [1.0.0] on 2020-Feb-18
210 | * Release [0.2.7] on 2019-Nov-18
211 | * Fix [DJSON-29]: throw exception on missing object entries (extra commas)
212 | * Release [0.2.6] on 2015-Mar-6
213 | * Modify build to produce an AOT package with classifier "aot"
214 | * Release [0.2.5] on 2014-Jun-13
215 | * Fix [DJSON-17]: throw exception on Infinite or NaN floating-point
216 | values. Old behavior could produce invalid JSON.
217 | * Release [0.2.4] on 2014-Jan-10
218 | * Small change in behavior: `clojure.data.json/pprint` now adds a
219 | newline after its output just like `clojure.core/pprint`
220 | * Fix [DJSON-13]: flush output after pprint
221 | * Fix [DJSON-14]: handle EOF inside character escape
222 | * Fix [DJSON-15]: bad syntax in test
223 | * Release [0.2.3] on 2013-Aug-30
224 | * Enhancement [DJSON-9]: option to escape U+2028 and U+2029
225 | * Fix [DJSON-11]: printing unnecessary commas with value-fn
226 | * Release [0.2.2] on 2013-Apr-07
227 | * Fix [DJSON-7]: extra commas when removing key/value pairs)
228 | * Fix [DJSON-8]: wrong output stream in `write-json`
229 | * Release [0.2.1] on 2012-Oct-26
230 | * Restores backwards-compatibility with 0.1.x releases. The older
231 | 0.1.x APIs are marked as deprecated in their documentation. They
232 | will be removed in a future release.
233 | * Release [0.2.0] on 2012-Oct-12
234 | * **Not recommended for use**: this release introduced breaking API
235 | changes (renaming core functions) without any path for
236 | backwards-compatibility. Applications with transitive dependencies
237 | on both the 0.2.x and 0.1.x APIs cannot use this version.
238 | * New :key-fn and :value-fn permit flexible transformation
239 | of values when reading & writing JSON
240 | * Support for reading large integers as BigInt
241 | * Optional support for reading decimals as BigDecimal
242 | * Performance improvements
243 | * Release [0.1.3] on 2012-Mar-09
244 | * Fix writing strings containing characters outside the BMP
245 | * Release [0.1.2] on 2011-Oct-14
246 | * Better parsing of hexadecimal character escapes
247 | * Fix EOF-handling bug
248 | * Fix [DJSON-1]: reflection warnings
249 | * Release [0.1.1] on 2011-Jul-01
250 | * Ensure that printing to `*out*` always uses a PrintWriter.
251 | * Release [0.1.0] on 2011-Mar-18
252 | * Initial release.
253 | * Source-compatible with clojure.contrib.json, except for the name change.
254 |
255 | [DJSON-61]: https://clojure.atlassian.net/browse/DJSON-61
256 | [DJSON-57]: https://clojure.atlassian.net/browse/DJSON-57
257 | [DJSON-56]: https://clojure.atlassian.net/browse/DJSON-56
258 | [DJSON-54]: https://clojure.atlassian.net/browse/DJSON-54
259 | [DJSON-53]: https://clojure.atlassian.net/browse/DJSON-53
260 | [DJSON-52]: https://clojure.atlassian.net/browse/DJSON-52
261 | [DJSON-51]: https://clojure.atlassian.net/browse/DJSON-51
262 | [DJSON-50]: https://clojure.atlassian.net/browse/DJSON-50
263 | [DJSON-48]: https://clojure.atlassian.net/browse/DJSON-48
264 | [DJSON-47]: https://clojure.atlassian.net/browse/DJSON-47
265 | [DJSON-46]: https://clojure.atlassian.net/browse/DJSON-46
266 | [DJSON-45]: https://clojure.atlassian.net/browse/DJSON-45
267 | [DJSON-43]: https://clojure.atlassian.net/browse/DJSON-43
268 | [DJSON-41]: https://clojure.atlassian.net/browse/DJSON-41
269 | [DJSON-40]: https://clojure.atlassian.net/browse/DJSON-40
270 | [DJSON-39]: https://clojure.atlassian.net/browse/DJSON-39
271 | [DJSON-38]: https://clojure.atlassian.net/browse/DJSON-38
272 | [DJSON-37]: https://clojure.atlassian.net/browse/DJSON-37
273 | [DJSON-36]: https://clojure.atlassian.net/browse/DJSON-36
274 | [DJSON-35]: https://clojure.atlassian.net/browse/DJSON-35
275 | [DJSON-34]: https://clojure.atlassian.net/browse/DJSON-34
276 | [DJSON-33]: https://clojure.atlassian.net/browse/DJSON-33
277 | [DJSON-32]: https://clojure.atlassian.net/browse/DJSON-32
278 | [DJSON-30]: https://clojure.atlassian.net/browse/DJSON-30
279 | [DJSON-29]: https://clojure.atlassian.net/browse/DJSON-29
280 | [DJSON-26]: https://clojure.atlassian.net/browse/DJSON-26
281 | [DJSON-18]: https://clojure.atlassian.net/browse/DJSON-18
282 | [DJSON-17]: https://clojure.atlassian.net/browse/DJSON-17
283 | [DJSON-15]: https://clojure.atlassian.net/browse/DJSON-15
284 | [DJSON-14]: https://clojure.atlassian.net/browse/DJSON-14
285 | [DJSON-13]: https://clojure.atlassian.net/browse/DJSON-13
286 | [DJSON-11]: https://clojure.atlassian.net/browse/DJSON-11
287 | [DJSON-9]: https://clojure.atlassian.net/browse/DJSON-9
288 | [DJSON-8]: https://clojure.atlassian.net/browse/DJSON-8
289 | [DJSON-7]: https://clojure.atlassian.net/browse/DJSON-7
290 | [DJSON-1]: https://clojure.atlassian.net/browse/DJSON-1
291 |
292 | [2.5.1]: https://github.com/clojure/data.json/tree/v2.5.1
293 | [2.5.0]: https://github.com/clojure/data.json/tree/v2.5.0
294 | [2.4.0]: https://github.com/clojure/data.json/tree/v2.4.0
295 | [2.3.1]: https://github.com/clojure/data.json/tree/v2.3.1
296 | [2.3.0]: https://github.com/clojure/data.json/tree/v2.3.0
297 | [2.2.3]: https://github.com/clojure/data.json/tree/v2.2.3
298 | [2.2.2]: https://github.com/clojure/data.json/tree/v2.2.2
299 | [2.2.1]: https://github.com/clojure/data.json/tree/v2.2.1
300 | [2.2.0]: https://github.com/clojure/data.json/tree/v2.2.0
301 | [2.1.1]: https://github.com/clojure/data.json/tree/v2.1.1
302 | [2.1.0]: https://github.com/clojure/data.json/tree/data.json-2.1.0
303 | [2.0.2]: https://github.com/clojure/data.json/tree/data.json-2.0.2
304 | [2.0.1]: https://github.com/clojure/data.json/tree/data.json-2.0.1
305 | [2.0.0]: https://github.com/clojure/data.json/tree/data.json-2.0.0
306 | [1.1.0]: https://github.com/clojure/data.json/tree/data.json-1.1.0
307 | [1.0.0]: https://github.com/clojure/data.json/tree/data.json-1.0.0
308 | [0.2.7]: https://github.com/clojure/data.json/tree/data.json-0.2.7
309 | [0.2.6]: https://github.com/clojure/data.json/tree/data.json-0.2.6
310 | [0.2.5]: https://github.com/clojure/data.json/tree/data.json-0.2.5
311 | [0.2.4]: https://github.com/clojure/data.json/tree/data.json-0.2.4
312 | [0.2.3]: https://github.com/clojure/data.json/tree/data.json-0.2.3
313 | [0.2.2]: https://github.com/clojure/data.json/tree/data.json-0.2.2
314 | [0.2.1]: https://github.com/clojure/data.json/tree/data.json-0.2.1
315 | [0.2.0]: https://github.com/clojure/data.json/tree/data.json-0.2.0
316 | [0.1.3]: https://github.com/clojure/data.json/tree/data.json-0.1.3
317 | [0.1.2]: https://github.com/clojure/data.json/tree/data.json-0.1.2
318 | [0.1.1]: https://github.com/clojure/data.json/tree/data.json-0.1.1
319 | [0.1.0]: https://github.com/clojure/data.json/tree/data.json-0.1.0
320 |
321 |
322 |
323 | Copyright and License
324 | ----------------------------------------
325 |
326 | Copyright (c) Stuart Sierra, Rich Hickey, and contributors.
327 | All rights reserved. The use and
328 | distribution terms for this software are covered by the Eclipse Public
329 | License 1.0 (https://opensource.org/license/epl-1-0/) witch can
330 | be found in the file epl-v10.html at the root of this distribution.
331 | By using this software in any fashion, you are agreeing to be bound by
332 | the terms of this license. You must not remove this notice, or any
333 | other, from this software.
334 |
--------------------------------------------------------------------------------
/src/test/clojure/clojure/data/json_test.clj:
--------------------------------------------------------------------------------
1 | (ns clojure.data.json-test
2 | (:require [clojure.data.json :as json]
3 | [clojure.test :refer :all]
4 | [clojure.string :as str]))
5 |
6 | (defn pbr
7 | ([s]
8 | (pbr s 64))
9 | ([s size]
10 | (if (< size 64)
11 | (throw (RuntimeException. "Size must be >= 64"))
12 | (java.io.PushbackReader. (java.io.StringReader. s) size))))
13 |
14 | (deftest read-from-pushback-reader
15 | (is (= 42 (json/read (pbr "42"))))
16 | (is (= ["abc" "def"] (json/read (pbr "[\"abc\", \"def\"]")))))
17 |
18 | ;; DJSON-50 - pass PBR to safely do repeated read
19 | (deftest read-multiple
20 | (let [st "{\"foo\":\"some string\"}{\"foo\":\"another string\"}"
21 | pbr (pbr st)]
22 | (is (= {"foo" "some string"} (json/read pbr)))
23 | (is (= {"foo" "another string"} (json/read pbr))))
24 |
25 | (let [st "{\"foo\":\"some string\"}{\"foo\":\"another long ......................................................... string\"}"
26 | pbr (pbr st)]
27 | (is (= {"foo" "some string"} (json/read pbr)))
28 | (is (= {"foo" "another long ......................................................... string"} (json/read pbr)))))
29 |
30 | (defn read-then-eof [s]
31 | (let [r (pbr s)
32 | val (json/read r :eof-error? false :eof-value :EOF)]
33 | (is (= :EOF (json/read r :eof-error? false :eof-value :EOF)))
34 | val))
35 |
36 | (deftest read-multiple-eof
37 | (are [expected s] (= expected (read-then-eof s))
38 | 1.2 "1.2"
39 | 0 "0"
40 | 1 "1"
41 | 1.0 "1.0"
42 | "abc" "\"abc\""
43 | "\u2202" "\"\u2202\""
44 | [] "[]"
45 | [1 2] "[1, 2]")
46 | )
47 |
48 | (deftest read-from-reader
49 | (let [s (java.io.StringReader. "42")]
50 | (is (= 42 (json/read s)))))
51 |
52 | (deftest read-numbers
53 | (is (= 42 (json/read-str "42")))
54 | (is (= -3 (json/read-str "-3")))
55 | (is (= 3.14159 (json/read-str "3.14159")))
56 | (is (= 6.022e23 (json/read-str "6.022e23"))))
57 |
58 | (deftest read-bigint
59 | (is (= 123456789012345678901234567890N
60 | (json/read-str "123456789012345678901234567890"))))
61 |
62 | (deftest lenient-on-extra-data
63 | (is (= [42] (json/read-str "[42],abc")))
64 | (is (= [42] (json/read (java.io.StringReader. "[42],abc")))))
65 |
66 | (deftest strict-on-extra-data
67 | ;; on-extra-throw
68 | (is (thrown? clojure.lang.ExceptionInfo
69 | (json/read-str "[42],abc" :extra-data-fn json/on-extra-throw)))
70 | (is (thrown? clojure.lang.ExceptionInfo
71 | (json/read (java.io.StringReader. "[42],abc") :extra-data-fn json/on-extra-throw)))
72 |
73 | ;; on-extra-throw-remaining
74 | (try
75 | (json/read-str "[42],abc" :extra-data-fn json/on-extra-throw-remaining)
76 | (is false "expected exception to be thrown")
77 | (catch clojure.lang.ExceptionInfo e
78 | (is (= ",abc" (:remaining (ex-data e))))))
79 | (try
80 | (json/read-str "[1], 1]" :extra-data-fn json/on-extra-throw-remaining)
81 | (is false "expected exception to be thrown")
82 | (catch clojure.lang.ExceptionInfo e
83 | (is (= ", 1]" (:remaining (ex-data e))))))
84 |
85 | ;; check that empty input behavior not modified when :extra-data-fn specified
86 | (is (= :hi (json/read-str ""
87 | :eof-error? false, :eof-value :hi, :extra-data-fn json/on-extra-throw)))
88 | (is (= :hi (json/read (java.io.StringReader. "")
89 | :eof-error? false, :eof-value :hi, :extra-data-fn json/on-extra-throw))))
90 |
91 | (deftest write-bigint
92 | (is (= "123456789012345678901234567890"
93 | (json/write-str 123456789012345678901234567890N))))
94 |
95 | (deftest read-bigdec
96 | (is (= 3.14159M (json/read-str "3.14159" :bigdec true))))
97 |
98 | (deftest write-bigdec
99 | (is (= "3.14159" (json/write-str 3.14159M))))
100 |
101 | (deftest read-null
102 | (is (= nil (json/read-str "null"))))
103 |
104 | (deftest read-strings
105 | (is (= "Hello, World!" (json/read-str "\"Hello, World!\""))))
106 |
107 | (deftest escaped-slashes-in-strings
108 | (is (= "/foo/bar" (json/read-str "\"\\/foo\\/bar\""))))
109 |
110 | (deftest unicode-escapes
111 | (is (= " \u0beb " (json/read-str "\" \\u0bEb \""))))
112 |
113 | (deftest unicode-outside-bmp
114 | (is (= "\"smiling face: \uD83D\uDE03\""
115 | (json/write-str "smiling face: \uD83D\uDE03" :escape-unicode false)))
116 | (is (= "\"smiling face: \\ud83d\\ude03\""
117 | (json/write-str "smiling face: \uD83D\uDE03" :escape-unicode true))))
118 |
119 | (deftest escaped-whitespace
120 | (is (= "foo\nbar" (json/read-str "\"foo\\nbar\"")))
121 | (is (= "foo\rbar" (json/read-str "\"foo\\rbar\"")))
122 | (is (= "foo\tbar" (json/read-str "\"foo\\tbar\""))))
123 |
124 | (deftest read-booleans
125 | (is (= true (json/read-str "true")))
126 | (is (= false (json/read-str "false"))))
127 |
128 | (deftest ignore-whitespace
129 | (is (= nil (json/read-str "\r\n null"))))
130 |
131 | (deftest read-arrays
132 | (is (= (vec (range 35))
133 | (json/read-str "[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34]")))
134 | (is (= ["Ole" "Lena"] (json/read-str "[\"Ole\", \r\n \"Lena\"]"))))
135 |
136 | (deftest read-objects
137 | (is (= {:k1 1, :k2 2, :k3 3, :k4 4, :k5 5, :k6 6, :k7 7, :k8 8
138 | :k9 9, :k10 10, :k11 11, :k12 12, :k13 13, :k14 14, :k15 15, :k16 16}
139 | (json/read-str "{\"k1\": 1, \"k2\": 2, \"k3\": 3, \"k4\": 4,
140 | \"k5\": 5, \"k6\": 6, \"k7\": 7, \"k8\": 8,
141 | \"k9\": 9, \"k10\": 10, \"k11\": 11, \"k12\": 12,
142 | \"k13\": 13, \"k14\": 14, \"k15\": 15, \"k16\": 16}"
143 | :key-fn keyword))))
144 |
145 | (deftest read-nested-structures
146 | (is (= {:a [1 2 {:b [3 "four"]} 5.5]}
147 | (json/read-str "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"
148 | :key-fn keyword))))
149 |
150 | (deftest read-nested-structures-stream
151 | (is (= {:a [1 2 {:b [3 "four"]} 5.5]}
152 | (json/read (java.io.StringReader. "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}")
153 | :key-fn keyword))))
154 |
155 | (deftest reads-long-string-correctly
156 | (let [long-string (str/join "" (take 100 (cycle "abcde")))]
157 | (is (= long-string (json/read-str (str "\"" long-string "\""))))))
158 |
159 | (deftest disallows-non-string-keys
160 | (is (thrown? Exception (json/read-str "{26:\"z\""))))
161 |
162 | (deftest disallows-barewords
163 | (is (thrown? Exception (json/read-str " foo "))))
164 |
165 | (deftest disallows-unclosed-arrays
166 | (is (thrown? Exception (json/read-str "[1, 2, "))))
167 |
168 | (deftest disallows-unclosed-objects
169 | (is (thrown? Exception (json/read-str "{\"a\":1, "))))
170 |
171 | (deftest disallows-empty-entry-in-object
172 | (is (thrown? Exception (json/read-str "{\"a\":1,}")))
173 | (is (thrown? Exception (json/read-str "{\"a\":1, }")))
174 | (is (thrown? Exception (json/read-str "{\"a\":1,,,,}")))
175 | (is (thrown? Exception (json/read-str "{\"a\":1,,\"b\":2}"))))
176 |
177 | (deftest get-string-keys
178 | (is (= {"a" [1 2 {"b" [3 "four"]} 5.5]}
179 | (json/read-str "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"))))
180 |
181 | (deftest keywordize-keys
182 | (is (= {:a [1 2 {:b [3 "four"]} 5.5]}
183 | (json/read-str "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"
184 | :key-fn keyword))))
185 |
186 | (deftest convert-values
187 | (is (= {:number 42 :date (java.sql.Date. 55 6 12)}
188 | (json/read-str "{\"number\": 42, \"date\": \"1955-07-12\"}"
189 | :key-fn keyword
190 | :value-fn (fn [k v]
191 | (if (= :date k)
192 | (java.sql.Date/valueOf ^String v)
193 | v))))))
194 |
195 | (deftest omit-values
196 | (is (= {:number 42}
197 | (json/read-str "{\"number\": 42, \"date\": \"1955-07-12\"}"
198 | :key-fn keyword
199 | :value-fn (fn thisfn [k v]
200 | (if (= :date k)
201 | thisfn
202 | v)))))
203 | (is (= "{\"c\":1,\"e\":2}"
204 | (json/write-str (sorted-map :a nil, :b nil, :c 1, :d nil, :e 2, :f nil)
205 | :value-fn (fn remove-nils [k v]
206 | (if (nil? v)
207 | remove-nils
208 | v))))))
209 |
210 | (declare pass1-string)
211 |
212 | (deftest pass1-test
213 | (let [input (json/read-str pass1-string)]
214 | (is (= "JSON Test Pattern pass1" (first input)))
215 | (is (= "array with 1 element" (get-in input [1 "object with 1 member" 0])))
216 | (is (= 1234567890 (get-in input [8 "integer"])))
217 | (is (= "rosebud" (last input)))))
218 |
219 | ; from http://www.json.org/JSON_checker/test/pass1.json
220 | (def pass1-string
221 | "[
222 | \"JSON Test Pattern pass1\",
223 | {\"object with 1 member\":[\"array with 1 element\"]},
224 | {},
225 | [],
226 | -42,
227 | true,
228 | false,
229 | null,
230 | {
231 | \"integer\": 1234567890,
232 | \"real\": -9876.543210,
233 | \"e\": 0.123456789e-12,
234 | \"E\": 1.234567890E+34,
235 | \"\": 23456789012E66,
236 | \"zero\": 0,
237 | \"one\": 1,
238 | \"space\": \" \",
239 | \"quote\": \"\\\"\",
240 | \"backslash\": \"\\\\\",
241 | \"controls\": \"\\b\\f\\n\\r\\t\",
242 | \"slash\": \"/ & \\/\",
243 | \"alpha\": \"abcdefghijklmnopqrstuvwyz\",
244 | \"ALPHA\": \"ABCDEFGHIJKLMNOPQRSTUVWYZ\",
245 | \"digit\": \"0123456789\",
246 | \"0123456789\": \"digit\",
247 | \"special\": \"`1~!@#$%^&*()_+-={':[,]}|;.>?\",
248 | \"hex\": \"\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A\",
249 | \"true\": true,
250 | \"false\": false,
251 | \"null\": null,
252 | \"array\":[ ],
253 | \"object\":{ },
254 | \"address\": \"50 St. James Street\",
255 | \"url\": \"http://www.JSON.org/\",
256 | \"comment\": \"// /* */\": \" \",
258 | \" s p a c e d \" :[1,2 , 3
259 |
260 | ,
261 |
262 | 4 , 5 , 6 ,7 ],\"compact\":[1,2,3,4,5,6,7],
263 | \"jsontext\": \"{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}\",
264 | \"quotes\": \"" \\u0022 %22 0x22 034 "\",
265 | \"\\/\\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?\"
266 | : \"A key can be any string\"
267 | },
268 | 0.5 ,98.6
269 | ,
270 | 99.44
271 | ,
272 |
273 | 1066,
274 | 1e1,
275 | 0.1e1,
276 | 1e-1,
277 | 1e00,2e+00,2e-00
278 | ,\"rosebud\"]")
279 |
280 |
281 | (deftest print-json-strings
282 | (is (= "\"Hello, World!\"" (json/write-str "Hello, World!")))
283 | (is (= "\"\\\"Embedded\\\" Quotes\"" (json/write-str "\"Embedded\" Quotes"))))
284 |
285 | (deftest print-unicode
286 | (is (= "\"\\u1234\\u4567\"" (json/write-str "\u1234\u4567"))))
287 |
288 | (deftest print-nonescaped-unicode
289 | (is (= "\"\\u0000\\t\\u001f \"" (json/write-str "\u0000\u0009\u001f\u0020" :escape-unicode true)))
290 | (is (= "\"\\u0000\\t\\u001f \"" (json/write-str "\u0000\u0009\u001f\u0020" :escape-unicode false)))
291 | (is (= "\"\u1234\u4567\"" (json/write-str "\u1234\u4567" :escape-unicode false))))
292 |
293 | (deftest escape-special-separators
294 | (is (= "\"\\u2028\\u2029\"" (json/write-str "\u2028\u2029" :escape-unicode false)))
295 | (is (= "\"\u2028\u2029\"" (json/write-str "\u2028\u2029" :escape-js-separators false))))
296 |
297 | (deftest print-json-null
298 | (is (= "null" (json/write-str nil))))
299 |
300 | (deftest print-ratios-as-doubles
301 | (is (= "0.75" (json/write-str 3/4))))
302 |
303 | (deftest print-bigints
304 | (is (= "12345678901234567890" (json/write-str 12345678901234567890))))
305 |
306 | (deftest print-uuids
307 | (let [uid (java.util.UUID/randomUUID)
308 | roundtripped (java.util.UUID/fromString (json/read-str (json/write-str uid)))]
309 | (is (= uid roundtripped))))
310 |
311 | (def ^java.text.SimpleDateFormat date-format
312 | (doto (java.text.SimpleDateFormat. "dd-MM-yyyy hh:mm:ss")
313 | (.setTimeZone (java.util.TimeZone/getDefault))))
314 |
315 | (deftest print-util-date
316 | (let [date (.parse date-format "24-03-2006 15:49:00")
317 | epoch-millis (.getTime date)]
318 | (is (= epoch-millis (-> date
319 | json/write-str
320 | json/read-str
321 | java.time.Instant/parse
322 | .toEpochMilli)))))
323 |
324 | (deftest print-sql-date
325 | (let [date (.parse date-format "24-03-2006 15:49:00")
326 | sql-date (java.sql.Date. (.getTime date))
327 | epoch-millis-start-of-day (.getTime (.getTime (doto (java.util.Calendar/getInstance)
328 | (.setTime date)
329 | (.set java.util.Calendar/HOUR_OF_DAY 0)
330 | (.set java.util.Calendar/MINUTE 0)
331 | (.set java.util.Calendar/SECOND 0)
332 | (.set java.util.Calendar/MILLISECOND 0))))]
333 | (is (= epoch-millis-start-of-day (-> sql-date
334 | json/write-str
335 | json/read-str
336 | java.time.Instant/parse
337 | .toEpochMilli)))))
338 |
339 | (deftest print-time
340 | (let [time (java.time.Instant/parse "2006-03-24T15:49:00.000Z")]
341 | (is (= time (java.time.Instant/parse (json/read-str (json/write-str time)))))))
342 |
343 |
344 | (deftest print-time-supports-format
345 | (let [formatter (.withZone java.time.format.DateTimeFormatter/ISO_ZONED_DATE_TIME
346 | (java.time.ZoneId/systemDefault))
347 | date (.parse date-format "24-03-2006 15:49:00")
348 | time (.toInstant (.atZone (java.time.LocalDateTime/parse
349 | "2006-03-24T15:49:00.000Z"
350 | formatter)
351 | (java.time.ZoneId/systemDefault)))]
352 | (is (= time (->> (json/write-str date :date-formatter formatter)
353 | json/read-str
354 | (.parse formatter)
355 | (java.time.Instant/from))))))
356 |
357 | (deftest error-on-NaN
358 | (is (thrown? Exception (json/write-str Float/NaN)))
359 | (is (thrown? Exception (json/write-str Double/NaN))))
360 |
361 | (deftest error-on-infinity
362 | (is (thrown? Exception (json/write-str Float/POSITIVE_INFINITY)))
363 | (is (thrown? Exception (json/write-str Float/NEGATIVE_INFINITY)))
364 | (is (thrown? Exception (json/write-str Double/POSITIVE_INFINITY)))
365 | (is (thrown? Exception (json/write-str Double/NEGATIVE_INFINITY))))
366 |
367 | (defn- double-value [_ v]
368 | (if (and (instance? Double v)
369 | (or (.isNaN ^Double v)
370 | (.isInfinite ^Double v)))
371 | (str v)
372 | v))
373 |
374 | (deftest special-handler-for-double-NaN
375 | (is (= "{\"double\":\"NaN\"}"
376 | (json/write-str {:double Double/NaN}
377 | :value-fn double-value))))
378 |
379 | (deftest special-handler-for-double-infinity
380 | (is (= "{\"double\":\"Infinity\"}"
381 | (json/write-str {:double Double/POSITIVE_INFINITY}
382 | :value-fn double-value)))
383 | (is (= "{\"double\":\"-Infinity\"}"
384 | (json/write-str {:double Double/NEGATIVE_INFINITY}
385 | :value-fn double-value))))
386 |
387 | (deftest print-json-arrays
388 | (is (= "[1,2,3]" (json/write-str [1 2 3])))
389 | (is (= "[1,2,3]" (json/write-str (list 1 2 3))))
390 | (is (= "[1,2,3]" (json/write-str (sorted-set 1 2 3))))
391 | (is (= "[1,2,3]" (json/write-str (seq [1 2 3])))))
392 |
393 | (deftest print-java-arrays
394 | (is (= "[1,2,3]" (json/write-str (into-array [1 2 3])))))
395 |
396 | (deftest print-empty-arrays
397 | (is (= "[]" (json/write-str [])))
398 | (is (= "[]" (json/write-str (list))))
399 | (is (= "[]" (json/write-str #{}))))
400 |
401 | (deftest print-json-objects
402 | (is (= "{\"a\":1,\"b\":2}" (json/write-str (sorted-map :a 1 :b 2)))))
403 |
404 | (deftest object-keys-must-be-strings
405 | (is (= "{\"1\":1,\"2\":2}" (json/write-str (sorted-map 1 1 2 2)))))
406 |
407 | (deftest print-empty-objects
408 | (is (= "{}" (json/write-str {}))))
409 |
410 | (deftest accept-sequence-of-nils
411 | (is (= "[null,null,null]" (json/write-str [nil nil nil]))))
412 |
413 | (deftest error-on-nil-keys
414 | (is (thrown? Exception (json/write-str {nil 1}))))
415 |
416 | (deftest characters-in-symbols-are-escaped
417 | (is (= "\"foo\\u1b1b\"" (json/write-str (symbol "foo\u1b1b")))))
418 |
419 | (deftest default-throws-on-eof
420 | (is (thrown? java.io.EOFException (json/read-str ""))))
421 |
422 | (deftest throws-eof-in-unterminated-array
423 | (is (thrown? java.io.EOFException
424 | (json/read-str "[1, "))))
425 |
426 | (deftest throws-eof-in-unterminated-string
427 | (is (thrown? java.io.EOFException
428 | (json/read-str "\"missing end quote"))))
429 |
430 | (deftest throws-eof-in-escaped-chars
431 | (is (thrown? java.io.EOFException
432 | (json/read-str "\"\\"))))
433 |
434 | (deftest throws-eof-in-arrays
435 | (is (thrown? java.io.EOFException
436 | (json/read-str "[1,")))
437 | (is (thrown? java.io.EOFException
438 | (json/read-str "[1,2,"))))
439 |
440 | (deftest throws-eof-in-objects
441 | (is (thrown? java.io.EOFException
442 | (json/read-str "{")))
443 | (is (thrown? java.io.EOFException
444 | (json/read-str "{\"\":1,"))))
445 |
446 | (deftest accept-eof
447 | (is (= ::eof (json/read-str "" :eof-error? false :eof-value ::eof))))
448 |
449 | (deftest characters-in-map-keys-are-escaped
450 | (is (= "{\"\\\"\":42}" (json/write-str {"\"" 42}))))
451 |
452 | ;;; Indent
453 |
454 | (deftest print-json-arrays-indent
455 | (is (= "[\n 1,\n 2,\n 3\n]" (json/write-str [1 2 3] :indent true)))
456 | (is (= "[\n 1,\n 2,\n 3\n]" (json/write-str (list 1 2 3) :indent true)))
457 | (is (= "[\n 1,\n 2,\n 3\n]" (json/write-str (sorted-set 1 2 3) :indent true)))
458 | (is (= "[\n 1,\n 2,\n 3\n]" (json/write-str (seq [1 2 3]) :indent true))))
459 |
460 | (deftest print-java-arrays-indent
461 | (is (= "[\n 1,\n 2,\n 3\n]" (json/write-str (into-array [1 2 3]) :indent true))))
462 |
463 | (deftest print-empty-arrays-indent
464 | (is (= "[]" (json/write-str [] :indent true)))
465 | (is (= "[]" (json/write-str (list) :indent true)))
466 | (is (= "[]" (json/write-str #{} :indent true))))
467 |
468 | (deftest print-json-objects-indent
469 | (is (= "{\n \"a\": 1,\n \"b\": 2\n}" (json/write-str (sorted-map :a 1 :b 2) :indent true))))
470 |
471 | (deftest print-empty-objects-indent
472 | (is (= "{}" (json/write-str {} :indent true))))
473 |
474 | (deftest print-json-nested-indent
475 | (is (=
476 | "{
477 | \"a\": {
478 | \"b\": [
479 | 1,
480 | 2
481 | ],
482 | \"c\": [],
483 | \"d\": {}
484 | }
485 | }" (json/write-str {:a (sorted-map :b [1 2] :c [] :d {})} :indent true))))
486 |
487 |
488 | ;;; Pretty-printer
489 |
490 | (deftest pretty-printing
491 | (let [x (json/read-str pass1-string)]
492 | (is (= x (json/read-str (with-out-str (json/pprint x)))))))
493 |
494 | (deftest pretty-print-nonescaped-unicode
495 | (is (= "\"\u1234\u4567\"\n"
496 | (with-out-str
497 | (json/pprint "\u1234\u4567" :escape-unicode false)))))
498 |
499 | (defn benchmark []
500 | (dotimes [_ 8]
501 | (time
502 | (dotimes [_ 1000]
503 | (assert (= (json/read-str pass1-string)
504 | (json/read-str (json/write-str (json/read-str pass1-string)))))))))
505 |
506 | (defn djson-54-default-write-fn [x out options]
507 | (#'json/write-string (str x) out options))
508 |
509 | (deftest DJSON-54-test
510 | (is (thrown? Exception (json/write-str {:foo (java.net.URI. "http://clojure.org")})))
511 | (try (json/write-str {:foo (java.net.URI. "http://clojure.org")})
512 | (catch Exception e
513 | (is (= "Don't know how to write JSON of class java.net.URI" (.getMessage e)))))
514 | (is (= "{\"foo\":\"http:\\/\\/clojure.org\"}" (json/write-str {:foo (java.net.URI. "http://clojure.org")} :default-write-fn djson-54-default-write-fn))))
515 |
--------------------------------------------------------------------------------
/src/main/clojure/clojure/data/json.clj:
--------------------------------------------------------------------------------
1 | ;; Copyright (c) Stuart Sierra, 2012. All rights reserved. The use
2 | ;; and distribution terms for this software are covered by the Eclipse
3 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 | ;; which can be found in the file epl-v10.html at the root of this
5 | ;; distribution. By using this software in any fashion, you are
6 | ;; agreeing to be bound by the terms of this license. You must not
7 | ;; remove this notice, or any other, from this software.
8 |
9 | (ns ^{:author "Stuart Sierra"
10 | :doc "JavaScript Object Notation (JSON) parser/generator.
11 | See http://www.json.org/"}
12 | clojure.data.json
13 | (:refer-clojure :exclude (read))
14 | (:require [clojure.pprint :as pprint])
15 | (:import (java.io PrintWriter PushbackReader StringWriter
16 | Writer StringReader EOFException)))
17 |
18 | ;; CUSTOM PUSHBACK READER
19 |
20 | (set! *warn-on-reflection* true)
21 |
22 | (definterface InternalPBR
23 | (^int readChar [])
24 | (^long readChars [^chars buffer ^long start ^long bufflen])
25 | (^void unreadChar [^int c])
26 | (^void unreadChars [^chars buffer ^int off ^int bufflen])
27 | (^java.io.Reader toReader []))
28 |
29 | (deftype ReaderPBR [^PushbackReader rdr]
30 | InternalPBR
31 | (readChar [_]
32 | (.read rdr))
33 | (readChars [_ buffer start bufflen]
34 | (.read rdr ^chars buffer start bufflen))
35 | (unreadChar [_ c]
36 | ;; ASSERT: c should never be -1 (EOF)
37 | (.unread rdr c))
38 | (unreadChars [_ buffer start bufflen]
39 | (.unread rdr buffer start bufflen))
40 | (toReader [_]
41 | rdr))
42 |
43 | (comment
44 | (compile 'clojure.data.json)
45 | )
46 |
47 | (deftype StringPBR [^String s ^:unsynchronized-mutable ^long pos ^long len]
48 | InternalPBR
49 | (readChar [_]
50 | (if (< pos len)
51 | (let [p pos]
52 | (set! pos (unchecked-inc pos))
53 | (let [c (int (.charAt s p))]
54 | c))
55 | (let [i (int -1)]
56 | i)))
57 | (readChars [_ buffer start bufflen]
58 | (let [remaining (- len pos)
59 | n (Math/min remaining bufflen)]
60 | (when (pos? n)
61 | (let [p pos
62 | end (+ p n)]
63 | (set! pos end)
64 | (.getChars ^String s p end ^chars buffer start)))
65 | (if (pos? n) n -1)))
66 | (unreadChar [_ _c]
67 | ;; ASSERT: c should never be -1 (EOF)
68 | (set! pos (unchecked-dec pos))
69 | nil)
70 | (unreadChars [_ _buffer _start bufflen]
71 | (set! pos (unchecked-subtract pos bufflen))
72 | nil)
73 | (toReader [_]
74 | (StringReader. (.subSequence s pos len))))
75 |
76 | (defn- pushback-pbr
77 | [^PushbackReader r]
78 | (->ReaderPBR r))
79 |
80 | (defn- string-pbr
81 | [^String s]
82 | (->StringPBR s 0 (.length s)))
83 |
84 | ;;; JSON READER
85 |
86 | (set! *warn-on-reflection* true)
87 |
88 | (defn- default-write-key-fn
89 | [x]
90 | (cond (instance? clojure.lang.Named x)
91 | (name x)
92 | (nil? x)
93 | (throw (Exception. "JSON object properties may not be nil"))
94 | :else (str x)))
95 |
96 | (defn- default-value-fn [k v] v)
97 |
98 | (declare -read)
99 |
100 | (defmacro ^:private codepoint [c]
101 | (int c))
102 |
103 | (defn- codepoint-clause [[test result]]
104 | (cond (list? test)
105 | [(map int test) result]
106 | (= test :whitespace)
107 | ['(9 10 13 32) result]
108 | (= test :js-separators)
109 | ['(16r2028 16r2029) result]
110 | :else
111 | [(int test) result]))
112 |
113 | (defmacro ^:private codepoint-case [e & clauses]
114 | `(case ~e
115 | ~@(mapcat codepoint-clause (partition 2 clauses))
116 | ~@(when (odd? (count clauses))
117 | [(last clauses)])))
118 |
119 | (defn- read-hex-char [^InternalPBR stream]
120 | ;; Expects to be called with the head of the stream AFTER the
121 | ;; initial "\u". Reads the next four characters from the stream.
122 | (let [a (.readChar stream)
123 | b (.readChar stream)
124 | c (.readChar stream)
125 | d (.readChar stream)]
126 | (when (or (neg? a) (neg? b) (neg? c) (neg? d))
127 | (throw (EOFException.
128 | "JSON error (end-of-file inside Unicode character escape)")))
129 | (let [s (str (char a) (char b) (char c) (char d))]
130 | (char (Integer/parseInt s 16)))))
131 |
132 | (defn- read-escaped-char [^InternalPBR stream]
133 | ;; Expects to be called with the head of the stream AFTER the
134 | ;; initial backslash.
135 | (let [c (.readChar stream)]
136 | (when (neg? c)
137 | (throw (EOFException. "JSON error (end-of-file inside escaped char)")))
138 | (codepoint-case c
139 | (\" \\ \/) (char c)
140 | \b \backspace
141 | \f \formfeed
142 | \n \newline
143 | \r \return
144 | \t \tab
145 | \u (read-hex-char stream))))
146 |
147 | (defn- slow-read-string [^InternalPBR stream ^String already-read]
148 | (let [buffer (StringBuilder. already-read)]
149 | (loop []
150 | (let [c (.readChar stream)]
151 | (when (neg? c)
152 | (throw (EOFException. "JSON error (end-of-file inside string)")))
153 | (codepoint-case c
154 | \" (str buffer)
155 | \\ (do (.append buffer (read-escaped-char stream))
156 | (recur))
157 | (do (.append buffer (char c))
158 | (recur)))))))
159 |
160 | (defn- read-quoted-string [^InternalPBR stream]
161 | ;; Expects to be called with the head of the stream AFTER the
162 | ;; opening quotation mark.
163 | (let [buffer ^chars (char-array 64)
164 | read (.readChars stream buffer 0 64)
165 | end-index (unchecked-dec-int read)]
166 | (when (neg? read)
167 | (throw (EOFException. "JSON error (end-of-file inside string)")))
168 | (loop [i (int 0)]
169 | (let [c (int (aget buffer i))]
170 | (codepoint-case c
171 | \" (let [off (unchecked-inc-int i)
172 | len (unchecked-subtract-int read off)]
173 | (.unreadChars stream buffer off len)
174 | (String. buffer 0 i))
175 | \\ (let [off i
176 | len (unchecked-subtract-int read off)]
177 | (.unreadChars stream buffer off len)
178 | (slow-read-string stream (String. buffer 0 i)))
179 | (if (= i end-index)
180 | (do (.unreadChar stream c)
181 | (slow-read-string stream (String. buffer 0 i)))
182 | (recur (unchecked-inc-int i))))))))
183 |
184 | (defn- read-integer [^String string]
185 | (if (< (count string) 18) ; definitely fits in a Long
186 | (Long/valueOf string)
187 | (or (try (Long/valueOf string)
188 | (catch NumberFormatException e nil))
189 | (bigint string))))
190 |
191 | (defn- read-decimal [^String string bigdec?]
192 | (if bigdec?
193 | (bigdec string)
194 | (Double/valueOf string)))
195 |
196 | (defn- read-number [^InternalPBR stream bigdec?]
197 | (let [buffer (StringBuilder.)
198 | decimal? (loop [stage :minus]
199 | (let [c (.readChar stream)]
200 | (case stage
201 | :minus
202 | (codepoint-case c
203 | \-
204 | (do (.append buffer (char c))
205 | (recur :int-zero))
206 | \0
207 | (do (.append buffer (char c))
208 | (recur :frac-point))
209 | (\1 \2 \3 \4 \5 \6 \7 \8 \9)
210 | (do (.append buffer (char c))
211 | (recur :int-digit))
212 | (throw (Exception. "JSON error (invalid number literal)")))
213 | ;; Number must either be a single 0 or 1-9 followed by 0-9
214 | :int-zero
215 | (codepoint-case c
216 | \0
217 | (do (.append buffer (char c))
218 | (recur :frac-point))
219 | (\1 \2 \3 \4 \5 \6 \7 \8 \9)
220 | (do (.append buffer (char c))
221 | (recur :int-digit))
222 | (throw (Exception. "JSON error (invalid number literal)")))
223 | ;; at this point, there is at least one digit
224 | :int-digit
225 | (codepoint-case c
226 | (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
227 | (do (.append buffer (char c))
228 | (recur :int-digit))
229 | \.
230 | (do (.append buffer (char c))
231 | (recur :frac-first))
232 | (\e \E)
233 | (do (.append buffer (char c))
234 | (recur :exp-symbol))
235 | ;; early exit
236 | :whitespace
237 | (do (.unreadChar stream c)
238 | false)
239 | (\, \] \})
240 | (do (.unreadChar stream c)
241 | false)
242 | -1
243 | false
244 | (throw (Exception. "JSON error (invalid number literal)")))
245 | ;; previous character is a "0"
246 | :frac-point
247 | (codepoint-case c
248 | \.
249 | (do (.append buffer (char c))
250 | (recur :frac-first))
251 | (\e \E)
252 | (do (.append buffer (char c))
253 | (recur :exp-symbol))
254 | ;; early exit
255 | :whitespace
256 | (do (.unreadChar stream c)
257 | false)
258 | (\, \] \})
259 | (do (.unreadChar stream c)
260 | false)
261 | -1
262 | false
263 | ;; Disallow zero-padded numbers or invalid characters
264 | (throw (Exception. "JSON error (invalid number literal)")))
265 | ;; previous character is a "."
266 | :frac-first
267 | (codepoint-case c
268 | (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
269 | (do (.append buffer (char c))
270 | (recur :frac-digit))
271 | (throw (Exception. "JSON error (invalid number literal)")))
272 | ;; any number of following digits
273 | :frac-digit
274 | (codepoint-case c
275 | (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
276 | (do (.append buffer (char c))
277 | (recur :frac-digit))
278 | (\e \E)
279 | (do (.append buffer (char c))
280 | (recur :exp-symbol))
281 | ;; early exit
282 | :whitespace
283 | (do (.unreadChar stream c)
284 | true)
285 | (\, \] \})
286 | (do (.unreadChar stream c)
287 | true)
288 | -1
289 | true
290 | (throw (Exception. "JSON error (invalid number literal)")))
291 | ;; previous character is a "e" or "E"
292 | :exp-symbol
293 | (codepoint-case c
294 | (\- \+)
295 | (do (.append buffer (char c))
296 | (recur :exp-first))
297 | (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
298 | (do (.append buffer (char c))
299 | (recur :exp-digit)))
300 | ;; previous character is a "-" or "+"
301 | ;; must have at least one digit
302 | :exp-first
303 | (codepoint-case c
304 | (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
305 | (do (.append buffer (char c))
306 | (recur :exp-digit))
307 | (throw (Exception. "JSON error (invalid number literal)")))
308 | ;; any number of following digits
309 | :exp-digit
310 | (codepoint-case c
311 | (\0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
312 | (do (.append buffer (char c))
313 | (recur :exp-digit))
314 | :whitespace
315 | (do (.unreadChar stream c)
316 | true)
317 | (\, \] \})
318 | (do (.unreadChar stream c)
319 | true)
320 | -1
321 | true
322 | (throw (Exception. "JSON error (invalid number literal)"))))))]
323 | (if decimal?
324 | (read-decimal (str buffer) bigdec?)
325 | (read-integer (str buffer)))))
326 |
327 | (defn- next-token [^InternalPBR stream]
328 | (loop [c (.readChar stream)]
329 | (if (< 32 c)
330 | (int c)
331 | (codepoint-case (int c)
332 | :whitespace (recur (.readChar stream))
333 | c))))
334 |
335 | (defn invalid-array-exception []
336 | (Exception. "JSON error (invalid array)"))
337 |
338 | (defn- eof-array-exception []
339 | (EOFException. "JSON error (EOF in array)"))
340 |
341 | (defn- read-array* [^InternalPBR stream options]
342 | ;; Handles all array values after the first.
343 | (loop [result (transient [])]
344 | (let [r (conj! result (-read stream true nil options))]
345 | (codepoint-case (int (next-token stream))
346 | \] (persistent! r)
347 | \, (recur r)
348 | -1 (throw (eof-array-exception))
349 | (throw (invalid-array-exception))))))
350 |
351 | (defn- read-array [^InternalPBR stream options]
352 | ;; Expects to be called with the head of the stream AFTER the
353 | ;; opening bracket.
354 | ;; Only handles array value.
355 | (let [c (int (next-token stream))]
356 | (codepoint-case c
357 | \] []
358 | \, (throw (invalid-array-exception))
359 | -1 (throw (eof-array-exception))
360 | (do (.unreadChar stream c)
361 | (read-array* stream options)))))
362 |
363 | (defn- object-colon-exception []
364 | (Exception. "JSON error (missing `:` in object)"))
365 |
366 | (defn- eof-object-exception []
367 | (EOFException. "JSON error (EOF in object)"))
368 |
369 | (defn- invalid-key-exception [c]
370 | (if (= c -1)
371 | (throw (eof-object-exception))
372 | (throw (Exception. (str "JSON error (non-string key in object), found `" (char c) "`, expected `\"`")))))
373 |
374 | (comment
375 | (compile 'clojure.data.json)
376 | )
377 |
378 | (defn- read-key [^InternalPBR stream]
379 | (let [c (int (next-token stream))]
380 | (if (= c (codepoint \"))
381 | (let [key (read-quoted-string stream)]
382 | (if (= (codepoint \:) (int (next-token stream)))
383 | key
384 | (throw (object-colon-exception))))
385 | (if (= c (codepoint \}))
386 | nil
387 | (invalid-key-exception c)))))
388 |
389 | (defn- read-object [^InternalPBR stream options]
390 | ;; Expects to be called with the head of the stream AFTER the
391 | ;; opening bracket.
392 | (let [key-fn (get options :key-fn)
393 | value-fn (get options :value-fn)]
394 | (loop [result (transient {})]
395 | (if-let [key (read-key stream)]
396 | (let [key (cond-> key key-fn key-fn)
397 | value (-read stream true nil options)
398 | r (if value-fn
399 | (let [out-value (value-fn key value)]
400 | (if-not (= value-fn out-value)
401 | (assoc! result key out-value)
402 | result))
403 | (assoc! result key value))]
404 | (codepoint-case (int (next-token stream))
405 | \, (recur r)
406 | \} (persistent! r)
407 | -1 (throw (eof-object-exception))
408 | (throw (Exception. "JSON error (missing entry in object)"))))
409 | (let [r (persistent! result)]
410 | (if (empty? r)
411 | r
412 | (throw (Exception. "JSON error empty entry in object is not allowed"))))))))
413 |
414 | (defn- -read
415 | [^InternalPBR stream eof-error? eof-value options]
416 | (let [c (int (next-token stream))]
417 | (codepoint-case c
418 | ;; Read numbers
419 | (\- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
420 | (do (.unreadChar stream c)
421 | (read-number stream (:bigdec options)))
422 |
423 | ;; Read strings
424 | \" (read-quoted-string stream)
425 |
426 | ;; Read null as nil
427 | \n (if (and (= (codepoint \u) (.readChar stream))
428 | (= (codepoint \l) (.readChar stream))
429 | (= (codepoint \l) (.readChar stream)))
430 | nil
431 | (throw (Exception. "JSON error (expected null)")))
432 |
433 | ;; Read true
434 | \t (if (and (= (codepoint \r) (.readChar stream))
435 | (= (codepoint \u) (.readChar stream))
436 | (= (codepoint \e) (.readChar stream)))
437 | true
438 | (throw (Exception. "JSON error (expected true)")))
439 |
440 | ;; Read false
441 | \f (if (and (= (codepoint \a) (.readChar stream))
442 | (= (codepoint \l) (.readChar stream))
443 | (= (codepoint \s) (.readChar stream))
444 | (= (codepoint \e) (.readChar stream)))
445 | false
446 | (throw (Exception. "JSON error (expected false)")))
447 |
448 | ;; Read JSON objects
449 | \{ (read-object stream options)
450 |
451 | ;; Read JSON arrays
452 | \[ (read-array stream options)
453 |
454 | (if (neg? c) ;; Handle end-of-stream
455 | (if eof-error?
456 | (throw (EOFException. "JSON error (end-of-file)"))
457 | eof-value)
458 | (throw (Exception.
459 | (str "JSON error (unexpected character): " (char c))))))))
460 |
461 | (defn- -read1
462 | [^InternalPBR stream eof-error? eof-value options]
463 | (let [val (-read stream eof-error? eof-value options)]
464 | (if-let [extra-data-fn (:extra-data-fn options)]
465 | (if (or eof-error? (not (identical? eof-value val)))
466 | (let [c (.readChar stream)]
467 | (if (neg? c)
468 | val
469 | (do
470 | (.unreadChar stream c)
471 | (extra-data-fn val (.toReader stream)))))
472 | val)
473 | val)))
474 |
475 | (defn on-extra-throw
476 | "Pass as :extra-data-fn to `read` or `read-str` to throw if data is found
477 | after the first object."
478 | [val rdr]
479 | (throw (ex-info "Found extra data after json object" {:val val})))
480 |
481 | (defn on-extra-throw-remaining
482 | "Pass as :extra-data-fn to `read` or `read-str` to throw if data is found
483 | after the first object and return the remaining data in ex-data :remaining."
484 | [val rdr]
485 | (let [remaining (slurp rdr)]
486 | (throw (ex-info (str "Found extra data after json object: " remaining)
487 | {:val val, :remaining remaining}))))
488 |
489 | (def default-read-options {:bigdec false
490 | :key-fn nil
491 | :value-fn nil})
492 | (defn read
493 | "Reads a single item of JSON data from a java.io.Reader.
494 |
495 | If you wish to repeatedly read items from the same reader, you must
496 | supply a PushbackReader with buffer size >= 64, and reuse it on
497 | subsequent calls.
498 |
499 | Options are key-value pairs, valid options are:
500 |
501 | :eof-error? boolean
502 |
503 | If true (default) will throw exception if the stream is empty.
504 |
505 | :eof-value Object
506 |
507 | Object to return if the stream is empty and eof-error? is
508 | false. Default is nil.
509 |
510 | :bigdec boolean
511 |
512 | If true use BigDecimal for decimal numbers instead of Double.
513 | Default is false.
514 |
515 | :key-fn function
516 |
517 | Single-argument function called on JSON property names; return
518 | value will replace the property names in the output. Default
519 | is clojure.core/identity, use clojure.core/keyword to get
520 | keyword properties.
521 |
522 | :value-fn function
523 |
524 | Function to transform values in maps (\"objects\" in JSON) in
525 | the output. For each JSON property, value-fn is called with
526 | two arguments: the property name (transformed by key-fn) and
527 | the value. The return value of value-fn will replace the value
528 | in the output. If value-fn returns itself, the property will
529 | be omitted from the output. The default value-fn returns the
530 | value unchanged. This option does not apply to non-map
531 | collections.
532 |
533 | :extra-data-fn function
534 |
535 | If :extra-data-fn is not nil, then the reader will be checked
536 | for extra data after the read. If found, the extra-data-fn will
537 | be invoked with the read value and the reader. The result of
538 | the extra-data-fn will be returned."
539 | [reader & {:as options}]
540 | (let [{:keys [eof-error? eof-value]
541 | :or {eof-error? true}} options
542 | pbr (pushback-pbr
543 | (if (instance? PushbackReader reader)
544 | reader
545 | (PushbackReader. reader 64)))]
546 | (->> options
547 | (merge default-read-options)
548 | (-read1 pbr eof-error? eof-value))))
549 |
550 | (defn read-str
551 | "Reads one JSON value from input String. Options are the same as for
552 | read."
553 | [string & {:as options}]
554 | (let [{:keys [eof-error? eof-value]
555 | :or {eof-error? true}} options]
556 | (->> options
557 | (merge default-read-options)
558 | (-read1 (string-pbr string) eof-error? eof-value))))
559 |
560 | ;;; JSON WRITER
561 |
562 |
563 | (defprotocol JSONWriter
564 | (-write [object out options]
565 | "Print object to Appendable out as JSON"))
566 |
567 | (defn- ->hex-string [^Appendable out cp]
568 | (let [cpl (long cp)]
569 | (.append out "\\u")
570 | (cond
571 | (< cpl 16)
572 | (.append out "000")
573 | (< cpl 256)
574 | (.append out "00")
575 | (< cpl 4096)
576 | (.append out "0"))
577 | (.append out (Integer/toHexString cp))))
578 |
579 | (def ^{:tag "[S"} codepoint-decoder
580 | (let [shorts (short-array 128)]
581 | (dotimes [i 128]
582 | (codepoint-case i
583 | \" (aset shorts i (short 1))
584 | \\ (aset shorts i (short 1))
585 | \/ (aset shorts i (short 2))
586 | \backspace (aset shorts i (short 3))
587 | \formfeed (aset shorts i (short 4))
588 | \newline (aset shorts i (short 5))
589 | \return (aset shorts i (short 6))
590 | \tab (aset shorts i (short 7))
591 | (if (< i 32)
592 | (aset shorts i (short 8))
593 | (aset shorts i (short 0)))))
594 | shorts))
595 |
596 | (defn- slow-write-string [^CharSequence s ^Appendable out options]
597 | (let [decoder codepoint-decoder
598 | slash (get options :escape-slash)
599 | escape-js-separators (get options :escape-js-separators)
600 | escape-unicode (get options :escape-unicode)]
601 | (dotimes [i (.length s)]
602 | (let [cp (int (.charAt s i))]
603 | (if (< cp 128)
604 | (case (aget decoder cp)
605 | 0 (.append out (char cp))
606 | 1 (do (.append out (char (codepoint \\))) (.append out (char cp)))
607 | 2 (.append out (if slash "\\/" "/"))
608 | 3 (.append out "\\b")
609 | 4 (.append out "\\f")
610 | 5 (.append out "\\n")
611 | 6 (.append out "\\r")
612 | 7 (.append out "\\t")
613 | 8 (->hex-string out cp))
614 | (codepoint-case cp
615 | :js-separators (if escape-js-separators
616 | (->hex-string out cp)
617 | (.append out (char cp)))
618 | (if escape-unicode
619 | (->hex-string out cp) ; Hexadecimal-escaped
620 | (.append out (char cp)))))))))
621 |
622 | (defn- write-string [^CharSequence s ^Appendable out options]
623 | (let [decoder codepoint-decoder
624 | l (.length s)]
625 | (.append out \")
626 | (loop [i 0]
627 | (if (= i l)
628 | (.append out s)
629 | (let [cp (int (.charAt s i))]
630 | (if (and (< cp 128)
631 | (zero? (aget decoder cp)))
632 | (recur (unchecked-inc i))
633 | (do
634 | (.append out s 0 i)
635 | (slow-write-string (.subSequence s i l) out options))))))
636 | (.append out \")))
637 |
638 | (defn- write-indent [^Appendable out options]
639 | (let [indent-depth (:indent-depth options)]
640 | (.append out \newline)
641 | (loop [i indent-depth]
642 | (when (pos? i)
643 | (.append out " ")
644 | (recur (dec i))))))
645 |
646 | (defn- write-object [m ^Appendable out options]
647 | (let [key-fn (get options :key-fn)
648 | value-fn (get options :value-fn)
649 | indent (get options :indent)
650 | opts (cond-> options
651 | indent (update :indent-depth inc))]
652 | (.append out \{)
653 | (when (and indent (seq m))
654 | (write-indent out opts))
655 | (loop [x m, have-printed-kv false]
656 | (when (seq x)
657 | (let [[k v] (first x)
658 | out-key (key-fn k)
659 | out-value (value-fn k v)
660 | nxt (next x)]
661 | (when-not (string? out-key)
662 | (throw (Exception. "JSON object keys must be strings")))
663 | (if-not (= value-fn out-value)
664 | (do
665 | (when have-printed-kv
666 | (.append out \,)
667 | (when indent
668 | (write-indent out opts)))
669 | (write-string out-key out opts)
670 | (.append out \:)
671 | (when indent
672 | (.append out \space))
673 | (-write out-value out opts)
674 | (when (seq nxt)
675 | (recur nxt true)))
676 | (when (seq nxt)
677 | (recur nxt have-printed-kv))))))
678 | (when (and indent (seq m))
679 | (write-indent out options)))
680 | (.append out \}))
681 |
682 | (defn- write-array [s ^Appendable out options]
683 | (let [indent (get options :indent)
684 | opts (cond-> options
685 | indent (update :indent-depth inc))]
686 | (.append out \[)
687 | (when (and indent (seq s))
688 | (write-indent out opts))
689 | (loop [x s]
690 | (when (seq x)
691 | (let [fst (first x)
692 | nxt (next x)]
693 | (-write fst out opts)
694 | (when (seq nxt)
695 | (.append out \,)
696 | (when indent
697 | (write-indent out opts))
698 | (recur nxt)))))
699 | (when (and indent (seq s))
700 | (write-indent out options)))
701 | (.append out \]))
702 |
703 | (defn- write-bignum [x ^Appendable out options]
704 | (.append out (str x)))
705 |
706 | (defn- write-float [^Float x ^Appendable out options]
707 | (cond (.isInfinite x)
708 | (throw (Exception. "JSON error: cannot write infinite Float"))
709 | (.isNaN x)
710 | (throw (Exception. "JSON error: cannot write Float NaN"))
711 | :else
712 | (.append out (str x))))
713 |
714 | (defn- write-double [^Double x ^Appendable out options]
715 | (cond (.isInfinite x)
716 | (throw (Exception. "JSON error: cannot write infinite Double"))
717 | (.isNaN x)
718 | (throw (Exception. "JSON error: cannot write Double NaN"))
719 | :else
720 | (.append out (str x))))
721 |
722 | (defn- write-plain [x ^Appendable out options]
723 | (.append out (str x)))
724 |
725 | (defn- write-uuid [^java.util.UUID x ^Appendable out options]
726 | (.append out \")
727 | (.append out (.toString x))
728 | (.append out \"))
729 |
730 | (defn- write-instant [^java.time.Instant x ^Appendable out options]
731 | (let [formatter ^java.time.format.DateTimeFormatter (:date-formatter options)]
732 | (.append out \")
733 | (.append out (.format formatter x))
734 | (.append out \")))
735 |
736 | (defn- write-date [^java.util.Date x ^Appendable out options]
737 | (write-instant (.toInstant x) out options))
738 |
739 | (defn- default-sql-date->instant-fn [^java.sql.Date d]
740 | (.toInstant (.atStartOfDay (.toLocalDate d) (java.time.ZoneId/systemDefault))))
741 |
742 | (defn- write-sql-date [^java.sql.Date x ^Appendable out options]
743 | (let [->instant (:sql-date-converter options)]
744 | (write-instant (->instant x) out options)))
745 |
746 | (defn- write-null [x ^Appendable out options]
747 | (.append out "null"))
748 |
749 | (defn- write-named [x out options]
750 | (write-string (name x) out options))
751 |
752 | (defn- write-generic [x out options]
753 | (if (.isArray (class x))
754 | (-write (seq x) out options)
755 | ((:default-write-fn options) x out options)))
756 |
757 | (defn- write-ratio [x out options]
758 | (-write (double x) out options))
759 |
760 | ;; nil, true, false
761 | (extend nil JSONWriter {:-write write-null})
762 | (extend java.lang.Boolean JSONWriter {:-write write-plain})
763 |
764 | ;; Numbers
765 | (extend java.lang.Byte JSONWriter {:-write write-plain})
766 | (extend java.lang.Short JSONWriter {:-write write-plain})
767 | (extend java.lang.Integer JSONWriter {:-write write-plain})
768 | (extend java.lang.Long JSONWriter {:-write write-plain})
769 | (extend java.lang.Float JSONWriter {:-write write-float})
770 | (extend java.lang.Double JSONWriter {:-write write-double})
771 | (extend clojure.lang.Ratio JSONWriter {:-write write-ratio})
772 | (extend java.math.BigInteger JSONWriter {:-write write-bignum})
773 | (extend java.math.BigDecimal JSONWriter {:-write write-bignum})
774 | (extend java.util.concurrent.atomic.AtomicInteger JSONWriter {:-write write-plain})
775 | (extend java.util.concurrent.atomic.AtomicLong JSONWriter {:-write write-plain})
776 | (extend java.util.UUID JSONWriter {:-write write-uuid})
777 | (extend java.time.Instant JSONWriter {:-write write-instant})
778 | (extend java.util.Date JSONWriter {:-write write-date})
779 | (extend java.sql.Date JSONWriter {:-write write-sql-date})
780 | (extend clojure.lang.BigInt JSONWriter {:-write write-bignum})
781 |
782 | ;; Symbols, Keywords, and Strings
783 | (extend clojure.lang.Named JSONWriter {:-write write-named})
784 | (extend java.lang.CharSequence JSONWriter {:-write write-string})
785 |
786 | ;; Collections
787 | (extend java.util.Map JSONWriter {:-write write-object})
788 | (extend java.util.Collection JSONWriter {:-write write-array})
789 |
790 | ;; Maybe a Java array, otherwise fail
791 | (extend java.lang.Object JSONWriter {:-write write-generic})
792 |
793 | (defn- default-write-fn [x out options]
794 | (throw (Exception. (str "Don't know how to write JSON of " (class x)))))
795 |
796 | (def default-write-options {:escape-unicode true
797 | :escape-js-separators true
798 | :escape-slash true
799 | :sql-date-converter default-sql-date->instant-fn
800 | :date-formatter java.time.format.DateTimeFormatter/ISO_INSTANT
801 | :key-fn default-write-key-fn
802 | :value-fn default-value-fn
803 | :default-write-fn default-write-fn
804 | :indent false
805 | :indent-depth 0 ;; internal, to track nesting depth
806 | })
807 | (defn write
808 | "Write JSON-formatted output to a java.io.Writer. Options are
809 | key-value pairs, valid options are:
810 |
811 | :escape-unicode boolean
812 |
813 | If true (default) non-ASCII characters are escaped as \\uXXXX
814 |
815 | :escape-js-separators boolean
816 |
817 | If true (default) the Unicode characters U+2028 and U+2029 will
818 | be escaped as \\u2028 and \\u2029 even if :escape-unicode is
819 | false. (These two characters are valid in pure JSON but are not
820 | valid in JavaScript strings.)
821 |
822 | :escape-slash boolean
823 |
824 | If true (default) the slash / is escaped as \\/
825 |
826 | :sql-date-converter function
827 |
828 | Single-argument function used to convert a java.sql.Date to
829 | a java.time.Instant. As java.sql.Date does not have a
830 | time-component (which is required by java.time.Instant), it needs
831 | to be computed. The default implementation, `default-sql-date->instant-fn`
832 | uses
833 | ```
834 | (.toInstant (.atStartOfDay (.toLocalDate sql-date) (java.time.ZoneId/systemDefault)))
835 | ```
836 |
837 | :date-formatter
838 |
839 | A java.time.DateTimeFormatter instance, defaults to DateTimeFormatter/ISO_INSTANT
840 |
841 | :key-fn function
842 |
843 | Single-argument function called on map keys; return value will
844 | replace the property names in the output. Must return a
845 | string. Default calls clojure.core/name on symbols and
846 | keywords and clojure.core/str on everything else.
847 |
848 | :value-fn function
849 |
850 | Function to transform values in maps before writing. For each
851 | key-value pair in an input map, called with two arguments: the
852 | key (BEFORE transformation by key-fn) and the value. The
853 | return value of value-fn will replace the value in the output.
854 | If the return value is a number, boolean, string, or nil it
855 | will be included literally in the output. If the return value
856 | is a non-map collection, it will be processed recursively. If
857 | the return value is a map, it will be processed recursively,
858 | calling value-fn again on its key-value pairs. If value-fn
859 | returns itself, the key-value pair will be omitted from the
860 | output. This option does not apply to non-map collections.
861 |
862 | :default-write-fn function
863 |
864 | Function to handle types which are unknown to data.json. Defaults
865 | to a function which throws an exception. Expects to be called with
866 | three args, the value to be serialized, the output stream, and the
867 | options map.
868 |
869 | :indent boolean
870 |
871 | If true, indent json while writing (default = false)."
872 | [x ^Writer writer & {:as options}]
873 | (-write x writer (merge default-write-options options)))
874 |
875 | (defn write-str
876 | "Converts x to a JSON-formatted string. Options are the same as
877 | write."
878 | ^String [x & {:as options}]
879 | (let [sw (StringWriter.)]
880 | (-write x sw (merge default-write-options options))
881 | (.toString sw)))
882 |
883 | ;;; JSON PRETTY-PRINTER
884 |
885 | ;; Based on code by Tom Faulhaber
886 |
887 | (defn- pprint-array [s]
888 | ((pprint/formatter-out "~<[~;~@{~w~^, ~:_~}~;]~:>") s))
889 |
890 | (defn- pprint-object [m options]
891 | (let [key-fn (:key-fn options)]
892 | ((pprint/formatter-out "~<{~;~@{~<~w:~_~w~:>~^, ~_~}~;}~:>")
893 | (for [[k v] m] [(key-fn k) v]))))
894 |
895 | (defn- pprint-generic [x options]
896 | (if (.isArray (class x))
897 | (pprint-array (seq x))
898 | ;; pprint proxies Writer, so we can't just wrap it
899 | (print (with-out-str (-write x (PrintWriter. *out*) options)))))
900 |
901 | (defn- pprint-dispatch [x options]
902 | (cond (nil? x) (print "null")
903 | (instance? java.util.Map x) (pprint-object x options)
904 | (instance? java.util.Collection x) (pprint-array x)
905 | (instance? clojure.lang.ISeq x) (pprint-array x)
906 | :else (pprint-generic x options)))
907 |
908 | (defn pprint
909 | "Pretty-prints JSON representation of x to *out*. Options are the same
910 | as for write except :value-fn and :indent, which are not supported."
911 | [x & {:as options}]
912 | (let [opts (merge default-write-options options)]
913 | (pprint/with-pprint-dispatch #(pprint-dispatch % opts)
914 | (pprint/pprint x))))
915 |
916 | ;; DEPRECATED APIs from 0.1.x
917 |
918 | (defn read-json
919 | "DEPRECATED; replaced by read-str.
920 |
921 | Reads one JSON value from input String or Reader. If keywordize? is
922 | true (default), object keys will be converted to keywords. If
923 | eof-error? is true (default), empty input will throw an
924 | EOFException; if false EOF will return eof-value."
925 | ([input]
926 | (read-json input true true nil))
927 | ([input keywordize?]
928 | (read-json input keywordize? true nil))
929 | ([input keywordize? eof-error? eof-value]
930 | (let [key-fn (if keywordize? keyword identity)]
931 | (condp instance? input
932 | String
933 | (read-str input
934 | :key-fn key-fn
935 | :eof-error? eof-error?
936 | :eof-value eof-value)
937 | java.io.Reader
938 | (read input
939 | :key-fn key-fn
940 | :eof-error? eof-error?
941 | :eof-value eof-value)))))
942 |
943 | (defn write-json
944 | "DEPRECATED; replaced by 'write'.
945 |
946 | Print object to PrintWriter out as JSON"
947 | [x out escape-unicode?]
948 | (write x out :escape-unicode escape-unicode?))
949 |
950 | (defn json-str
951 | "DEPRECATED; replaced by 'write-str'.
952 |
953 | Converts x to a JSON-formatted string.
954 |
955 | Valid options are:
956 | :escape-unicode false
957 | to turn of \\uXXXX escapes of Unicode characters."
958 | [x & options]
959 | (apply write-str x options))
960 |
961 | (defn print-json
962 | "DEPRECATED; replaced by 'write' to *out*.
963 |
964 | Write JSON-formatted output to *out*.
965 |
966 | Valid options are:
967 | :escape-unicode false
968 | to turn off \\uXXXX escapes of Unicode characters."
969 | [x & options]
970 | (apply write x *out* options))
971 |
972 | (defn pprint-json
973 | "DEPRECATED; replaced by 'pprint'.
974 |
975 | Pretty-prints JSON representation of x to *out*.
976 |
977 | Valid options are:
978 | :escape-unicode false
979 | to turn off \\uXXXX escapes of Unicode characters."
980 | [x & options]
981 | (apply pprint x options))
982 |
--------------------------------------------------------------------------------