Skip to content

Commit fc6f479

Browse files
committed
Improve doc
1 parent a51f855 commit fc6f479

1 file changed

Lines changed: 96 additions & 58 deletions

File tree

README.md

Lines changed: 96 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,24 @@ repository and issue tracker is
4141
[github.com/ijl/orjson](https://github.com/ijl/orjson), and patches may be
4242
submitted there.
4343

44-
1. [Usage](#usage)
44+
1. [Usage](#usage)
4545
1. [Install](#install)
4646
2. [Serialize](#serialize)
47+
1. [default](#default)
48+
2. [option](#option)
4749
3. [Deserialize](#deserialize)
48-
2. [Types](#types)
50+
2. [Types](#types)
4951
1. [dataclass](#dataclass)
5052
2. [datetime](#datetime)
5153
3. [float](#float)
5254
4. [int](#int)
5355
5. [str](#str)
54-
3. [Testing](#testing)
55-
4. [Performance](#performance)
56+
3. [Testing](#testing)
57+
4. [Performance](#performance)
5658
1. [Latency](#latency)
5759
2. [Memory](#memory)
5860
3. [Reproducing](#reproducing)
61+
5. [License](#license)
5962

6063
## Usage
6164

@@ -99,6 +102,28 @@ arbitrary types through `default`. It does not serialize subclasses of
99102
supported types natively, with the exception of `dataclasses.dataclass`
100103
subclasses.
101104

105+
It raises `JSONEncodeError` on an unsupported type. This exception message
106+
describes the invalid object.
107+
108+
It raises `JSONEncodeError` on a `str` that contains invalid UTF-8.
109+
110+
It raises `JSONEncodeError` on an integer that exceeds 64 bits by default or,
111+
with `OPT_STRICT_INTEGER`, 53 bits.
112+
113+
It raises `JSONEncodeError` if a `dict` has a key of a type other than `str`.
114+
115+
It raises `JSONEncodeError` if the output of `default` recurses to handling by
116+
`default` more than 254 levels deep.
117+
118+
It raises `JSONEncodeError` on circular references.
119+
120+
It raises `JSONEncodeError` if a `tzinfo` on a datetime object is incorrect.
121+
122+
`JSONEncodeError` is a subclass of `TypeError`. This is for compatibility
123+
with the standard library.
124+
125+
#### default
126+
102127
To serialize a subclass or arbitrary types, specify `default` as a
103128
callable that returns a supported type. `default` may be a function,
104129
lambda, or callable class instance.
@@ -123,42 +148,75 @@ The `default` callable may return an object that itself
123148
must be handled by `default` up to 254 times before an exception
124149
is raised.
125150

126-
`dumps()` accepts options via an `option` keyword argument. These include:
127-
128-
- `orjson.OPT_NAIVE_UTC` for assuming `datetime.datetime` objects without a
129-
`tzinfo` are UTC.
130-
- `orjson.OPT_OMIT_MICROSECONDS` to not serialize the `microseconds` field
131-
on `datetime.datetime` and `datetime.time` instances.
132-
- `orjson.OPT_SERIALIZE_DATACLASS` to serialize `dataclasses.dataclass`
133-
instances.
134-
- `orjson.OPT_STRICT_INTEGER` for enforcing a 53-bit limit on integers. The
135-
limit is otherwise 64 bits, the same as the Python standard library.
136-
- `orjson.OPT_UTC_Z` to serialize a UTC timezone on `datetime.datetime`
137-
instances as `Z` instead of `+00:00`.
151+
#### option
138152

139-
To specify multiple options, mask them together, e.g.,
153+
To modify how data is serialized, specify `option`. Each `option` is an integer
154+
constant in `orjson`. To specify multiple options, mask them together, e.g.,
140155
`option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC`.
141156

142-
It raises `JSONEncodeError` on an unsupported type. This exception message
143-
describes the invalid object.
157+
##### OPT_NAIVE_UTC
144158

145-
It raises `JSONEncodeError` on a `str` that contains invalid UTF-8.
159+
Serialize `datetime.datetime` objects without a `tzinfo` as UTC. This
160+
has no effect on `datetime.datetime` objects that have `tzinfo` set.
146161

147-
It raises `JSONEncodeError` on an integer that exceeds 64 bits by default or,
148-
with `OPT_STRICT_INTEGER`, 53 bits.
162+
```python
163+
>>> import orjson, datetime
164+
>>> orjson.dumps(
165+
datetime.datetime(1970, 1, 1, 0, 0, 0),
166+
)
167+
b'"1970-01-01T00:00:00"'
168+
>>> orjson.dumps(
169+
datetime.datetime(1970, 1, 1, 0, 0, 0),
170+
option=orjson.OPT_NAIVE_UTC,
171+
)
172+
b'"1970-01-01T00:00:00+00:00"'
173+
```
149174

150-
It raises `JSONEncodeError` if a `dict` has a key of a type other than `str`.
175+
##### OPT_OMIT_MICROSECONDS
151176

152-
It raises `JSONEncodeError` if the output of `default` recurses to handling by
153-
`default` more than 254 levels deep.
177+
Do not serialize the `microsecond` field on `datetime.datetime` and
178+
`datetime.time` instances.
154179

155-
It raises `JSONEncodeError` on circular references.
180+
```python
181+
>>> import orjson, datetime
182+
>>> orjson.dumps(
183+
datetime.datetime(1970, 1, 1, 0, 0, 0, 1),
184+
)
185+
b'"1970-01-01T00:00:00.000001"'
186+
>>> orjson.dumps(
187+
datetime.datetime(1970, 1, 1, 0, 0, 0, 1),
188+
option=orjson.OPT_OMIT_MICROSECONDS,
189+
)
190+
b'"1970-01-01T00:00:00"'
191+
```
156192

157-
It raises `JSONEncodeError` if a `tzinfo` on a datetime object is incorrect.
193+
##### OPT_SERIALIZE_DATACLASS
158194

159-
`JSONEncodeError` is a subclass of `TypeError`. This is for compatibility
160-
with the standard library.
195+
Serialize `dataclasses.dataclass` instances. For more, see
196+
[dataclass](#dataclass).
197+
198+
##### OPT_STRICT_INTEGER
199+
200+
Enforce 53-bit limit on integers. The limit is otherwise 64 bits, the same as
201+
the Python standard library. For more, see [int](#int).
161202

203+
##### OPT_UTC_Z
204+
205+
Serialize a UTC timezone on `datetime.datetime` instances as `Z` instead
206+
of `+00:00`.
207+
208+
```python
209+
>>> import orjson, datetime
210+
>>> orjson.dumps(
211+
datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
212+
)
213+
b'"1970-01-01T00:00:00+00:00"'
214+
>>> orjson.dumps(
215+
datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
216+
option=orjson.OPT_UTC_Z
217+
)
218+
b'"1970-01-01T00:00:00Z"'
219+
```
162220

163221
### Deserialize
164222

@@ -247,16 +305,6 @@ orjson serializes `datetime.datetime` objects to
247305
e.g., "1970-01-01T00:00:00+00:00". This is a subset of ISO 8601 and
248306
compatible with `isoformat()` in the standard library.
249307

250-
`datetime.datetime` objects serialize with or without a `tzinfo`. For a full
251-
RFC 3339 representation, `tzinfo` must be present or `orjson.OPT_NAIVE_UTC`
252-
must be specified (e.g., for timestamps stored in a database in UTC and
253-
deserialized by the database adapter without a `tzinfo`). If a
254-
`tzinfo` is not present, a timezone offset is not serialized.
255-
256-
`tzinfo`, if specified, must be a timezone object that is either
257-
`datetime.timezone.utc` or from the `pendulum`, `pytz`, or
258-
`dateutil`/`arrow` libraries.
259-
260308
```python
261309
>>> import orjson, datetime, pendulum
262310
>>> orjson.dumps(
@@ -273,22 +321,9 @@ b'"2100-09-01T21:55:02+00:00"'
273321
b'"2100-09-01T21:55:02"'
274322
```
275323

276-
`orjson.OPT_NAIVE_UTC`, if specified, only applies to objects that do not have
277-
a `tzinfo`.
278-
279-
```python
280-
>>> import orjson, datetime, pendulum
281-
>>> orjson.dumps(
282-
datetime.datetime.fromtimestamp(4123518902),
283-
option=orjson.OPT_NAIVE_UTC
284-
)
285-
b'"2100-09-01T21:55:02+00:00"'
286-
>>> orjson.dumps(
287-
datetime.datetime(2018, 12, 1, 2, 3, 4, 9, tzinfo=pendulum.timezone('Australia/Adelaide')),
288-
option=orjson.OPT_NAIVE_UTC
289-
)
290-
b'"2018-12-01T02:03:04.000009+10:30"'
291-
```
324+
`datetime.datetime` supports instances with a `tzinfo` that is `None`,
325+
`datetime.timezone.utc` or a timezone instance from
326+
the `pendulum`, `pytz`, or `dateutil`/`arrow` libraries.
292327

293328
`datetime.time` objects must not have a `tzinfo`.
294329

@@ -393,8 +428,7 @@ The library has comprehensive tests. There are tests against fixtures in the
393428
[nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark)
394429
repositories. It is tested to not crash against the
395430
[Big List of Naughty Strings](https://github.com/minimaxir/big-list-of-naughty-strings).
396-
It is tested to not leak memory. It is tested to be correct against
397-
input from the PyJFuzz JSON fuzzer. It is tested to not crash
431+
It is tested to not leak memory. It is tested to not crash
398432
against and not accept invalid UTF-8. There are integration tests
399433
exercising the library's use in web servers (gunicorn using multiprocess/forked
400434
workers) and when
@@ -509,7 +543,6 @@ format, containing floats and arrays, indented.
509543
| simplejson | 42.99 | 23.2 | 1.93 |
510544
| json | 44.69 | 21.4 | 2.01 |
511545

512-
513546
If a row is blank, the library did not serialize and deserialize the fixture without
514547
modifying it, e.g., returning different values for floating point numbers.
515548

@@ -569,3 +602,8 @@ ujson 1.35, python-rapidson 0.8.0, and simplejson 3.16.0.
569602

570603
The latency results can be reproduced using the `pybench` and `graph`
571604
scripts. The memory results can be reproduced using the `pymem` script.
605+
606+
## License
607+
608+
orjson was written by ijl <ijl@mailbox.org>, copyright 2018 - 2020, licensed
609+
under either the Apache 2 or MIT licenses.

0 commit comments

Comments
 (0)