File: README.md

package info (click to toggle)
data-json-clojure 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 172 kB
  • sloc: xml: 110; sh: 24; makefile: 13
file content (237 lines) | stat: -rw-r--r-- 8,182 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
data.json
========================================

JSON parser/generator to/from Clojure data structures.

Follows the specification on http://json.org/



Releases and Dependency Information
----------------------------------------

Latest stable release is [0.2.7]

[Leiningen] dependency information:

    [org.clojure/data.json "0.2.7"]

[Maven] dependency information:

    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>data.json</artifactId>
      <version>0.2.7</version>
    </dependency>

[Gradle] dependency information:

    compile "org.clojure:data.json:0.2.7"

[Leiningen]: http://leiningen.org/
[Maven]: http://maven.apache.org/
[Gradle]: http://www.gradle.org/


Other versions:

* [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22data.json%22)

* [Development Snapshots](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~data.json~~~)

* [Development Snapshot Repositories](http://dev.clojure.org/display/doc/Maven+Settings+and+Repositories)



Usage
----------------------------------------

[API Documentation](http://clojure.github.com/data.json/)

Example usage:

    (ns example
      (:require [clojure.data.json :as json]))

To convert to/from JSON strings, use `json/write-str` and `json/read-str`:

    (json/write-str {:a 1 :b 2})
    ;;=> "{\"a\":1,\"b\":2}"

    (json/read-str "{\"a\":1,\"b\":2}")
    ;;=> {"a" 1, "b" 2}

Note that these operations are not symmetric: converting Clojure data
into JSON is lossy.


### Converting Key/Value Types

You can specify a `:key-fn` to convert map keys on the way in or out:

    (json/read-str "{\"a\":1,\"b\":2}"
                   :key-fn keyword)
    ;;=> {:a 1, :b 2}

    (json/write-str {:a 1 :b 2}
                    :key-fn #(.toUpperCase %))
    ;;=> "{\"A\":1,\"B\":2}"

    (json/read-str "{\"a\":1,\"b\":2}"
                   :key-fn #(keyword "com.example" %))
    ;;=> {:com.example/a 1, :com.example/b 2}

You can specify a `:value-fn` to convert map values on the way in or
out. The value-fn will be called with two arguments, the key and the
value, and it returns the updated value.

    (defn my-value-reader [key value]
      (if (= key :date)
        (java.sql.Date/valueOf value)
        value))

    (json/read-str "{\"number\":42,\"date\":\"2012-06-02\"}"
                   :value-fn my-value-reader
                   :key-fn keyword) 
    ;;=> {:number 42, :date #inst "2012-06-02T04:00:00.000-00:00"}

Be aware that `:value-fn` only works on maps (JSON objects). If your
root data structure is, for example, a vector of dates, you will need
to pre- or post-process it outside of data.json. [clojure.walk] may be
useful for this.

[clojure.walk]: http://clojure.github.io/clojure/clojure.walk-api.html


### Order of key-fn / value-fn

If you specify both a `:key-fn` and a `:value-fn` when **reading**,
the value-fn is called **after** the key has been processed by the
key-fn.

The **reverse** is true when **writing**:

    (defn my-value-writer [key value]
      (if (= key :date)
        (str (java.sql.Date. (.getTime value)))
        value))

    (json/write-str {:number 42, :date (java.util.Date. 112 5 2)}
                    :value-fn my-value-writer
                    :key-fn name) 
    ;;=> "{\"number\":42,\"date\":\"2012-06-02\"}"


### Reading/Writing a Stream

You can also read JSON directly from a java.io.Reader with `json/read`
and write JSON directly to a java.io.Writer with `json/write`.


### More

Other options are available. Refer to the [API Documentation] for details.

[API Documentation]: http://clojure.github.com/data.json/



Developer Information
----------------------------------------

* [GitHub project](https://github.com/clojure/data.json)

* [How to contribute](http://dev.clojure.org/display/community/Contributing)

* [Bug Tracker](http://dev.clojure.org/jira/browse/DJSON)

* [Continuous Integration](http://build.clojure.org/job/data.json/)

* [Compatibility Test Matrix](http://build.clojure.org/job/data.json-test-matrix/)



Change Log
----------------------------------------

* Release [0.2.7] on 2019-Nov-18
  * Fix [DJSON-29]: throw exception on missing object entries (extra commas)
* Release [0.2.6] on 2015-Mar-6
  * Modify build to produce an AOT package with classifier "aot"
* Release [0.2.5] on 2014-Jun-13
  * Fix [DJSON-17]: throw exception on Infinite or NaN floating-point
    values. Old behavior could produce invalid JSON.
* Release [0.2.4] on 2014-Jan-10
  * Small change in behavior: `clojure.data.json/pprint` now adds a
    newline after its output just like `clojure.core/pprint`
  * Fix [DJSON-13]: flush output after pprint
  * Fix [DJSON-14]: handle EOF inside character escape
  * Fix [DJSON-15]: bad syntax in test
* Release [0.2.3] on 2013-Aug-30
  * Enhancement [DJSON-9]: option to escape U+2028 and U+2029
  * Fix [DJSON-11]: printing unnecessary commas with value-fn
* Release [0.2.2] on 2013-Apr-07
  * Fix [DJSON-7]: extra commas when removing key/value pairs)
  * Fix [DJSON-8]: wrong output stream in `write-json`
* Release [0.2.1] on 2012-Oct-26
  * Restores backwards-compatibility with 0.1.x releases. The older
    0.1.x APIs are marked as deprecated in their documentation. They
    will be removed in a future release.
* Release [0.2.0] on 2012-Oct-12
  * **Not recommended for use**: this release introduced breaking API
    changes (renaming core functions) without any path for
    backwards-compatibility. Applications with transitive dependencies
    on both the 0.2.x and 0.1.x APIs cannot use this version.
  * New :key-fn and :value-fn permit flexible transformation
    of values when reading & writing JSON
  * Support for reading large integers as BigInt
  * Optional support for reading decimals as BigDecimal
  * Performance improvements
* Release [0.1.3] on 2012-Mar-09
  * Fix writing strings containing characters outside the BMP
* Release [0.1.2] on 2011-Oct-14
  * Better parsing of hexadecimal character escapes
  * Fix EOF-handling bug
  * Fix [DJSON-1]: reflection warnings
* Release [0.1.1] on 2011-Jul-01
  * Ensure that printing to `*out*` always uses a PrintWriter.
* Release [0.1.0] on 2011-Mar-18
  * Initial release.
  * Source-compatible with clojure.contrib.json, except for the name change.

[DJSON-29]: http://dev.clojure.org/jira/browse/DJSON-29
[DJSON-17]: http://dev.clojure.org/jira/browse/DJSON-17
[DJSON-15]: http://dev.clojure.org/jira/browse/DJSON-15
[DJSON-14]: http://dev.clojure.org/jira/browse/DJSON-14
[DJSON-13]: http://dev.clojure.org/jira/browse/DJSON-13
[DJSON-11]: http://dev.clojure.org/jira/browse/DJSON-11
[DJSON-9]: http://dev.clojure.org/jira/browse/DJSON-9
[DJSON-8]: http://dev.clojure.org/jira/browse/DJSON-8
[DJSON-7]: http://dev.clojure.org/jira/browse/DJSON-7
[DJSON-1]: http://dev.clojure.org/jira/browse/DJSON-1

[0.2.7]: https://github.com/clojure/data.json/tree/data.json-0.2.7
[0.2.6]: https://github.com/clojure/data.json/tree/data.json-0.2.6
[0.2.5]: https://github.com/clojure/data.json/tree/data.json-0.2.5
[0.2.4]: https://github.com/clojure/data.json/tree/data.json-0.2.4
[0.2.3]: https://github.com/clojure/data.json/tree/data.json-0.2.3
[0.2.2]: https://github.com/clojure/data.json/tree/data.json-0.2.2
[0.2.1]: https://github.com/clojure/data.json/tree/data.json-0.2.1
[0.2.0]: https://github.com/clojure/data.json/tree/data.json-0.2.0
[0.1.3]: https://github.com/clojure/data.json/tree/data.json-0.1.3
[0.1.2]: https://github.com/clojure/data.json/tree/data.json-0.1.2
[0.1.1]: https://github.com/clojure/data.json/tree/data.json-0.1.1
[0.1.0]: https://github.com/clojure/data.json/tree/data.json-0.1.0



Copyright and License
----------------------------------------

Copyright (c) Stuart Sierra, 2012. All rights reserved.  The use and
distribution terms for this software are covered by the Eclipse Public
License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
be found in the file epl-v10.html at the root of this distribution.
By using this software in any fashion, you are agreeing to be bound by
the terms of this license.  You must not remove this notice, or any
other, from this software.