diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d686bd2 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,225 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " applehelp to make an Apple Help Book" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " epub3 to make an epub3" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + @echo " dummy to check syntax errors of document sources" + +.PHONY: clean +clean: + rm -rf $(BUILDDIR)/* + +.PHONY: html +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +.PHONY: dirhtml +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +.PHONY: singlehtml +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +.PHONY: pickle +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +.PHONY: json +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +.PHONY: htmlhelp +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +.PHONY: qthelp +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/python-rapidjson.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/python-rapidjson.qhc" + +.PHONY: applehelp +applehelp: + $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp + @echo + @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." + @echo "N.B. You won't be able to view it unless you put it in" \ + "~/Library/Documentation/Help or install it in your application" \ + "bundle." + +.PHONY: devhelp +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/python-rapidjson" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/python-rapidjson" + @echo "# devhelp" + +.PHONY: epub +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +.PHONY: epub3 +epub3: + $(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3 + @echo + @echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3." + +.PHONY: latex +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +.PHONY: latexpdf +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +.PHONY: latexpdfja +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +.PHONY: text +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +.PHONY: man +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +.PHONY: texinfo +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +.PHONY: info +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +.PHONY: gettext +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +.PHONY: changes +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +.PHONY: linkcheck +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +.PHONY: doctest +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +.PHONY: coverage +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +.PHONY: xml +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +.PHONY: pseudoxml +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." + +.PHONY: dummy +dummy: + $(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy + @echo + @echo "Build finished. Dummy builder generates no files." diff --git a/docs/api.rst b/docs/api.rst new file mode 100644 index 0000000..2714a69 --- /dev/null +++ b/docs/api.rst @@ -0,0 +1,379 @@ +=============================== + Exposed functions and symbols +=============================== + +.. default-domain:: py + +.. data:: __version__ + + The version of the module. + +.. data:: __author__ + + The author of the module. + +.. data:: DATETIME_MODE_NONE = 0 + + This is the default ``datetime_mode``: *neither* :class:`datetime` *nor* :class:`date` + instances are recognized by :func:`dumps` and :func:`loads`. + +.. data:: DATETIME_MODE_ISO8601 = 1 + + In this ``datetime_mode`` mode :func:`dumps` and :func:`loads` handle :class:`datetime` + *and* :class:`date` instances representing those values using the `ISO 8601`_ format. + +.. data:: DATETIME_MODE_ISO8601_IGNORE_TZ = 2 + + This is like :data:`DATETIME_MODE_ISO8601` except that the value's timezone is ignored. + +.. data:: DATETIME_MODE_ISO8601_UTC = 3 + + This is like :data:`DATETIME_MODE_ISO8601` except that the times are always *shifted* to + the UTC_ timezone. + +.. data:: UUID_MODE_NONE = 0 + + This is the default ``uuid_mode``: :class:`UUID` instances are *not* recognized by + :func:`dumps` and :func:`loads`. + +.. data:: UUID_MODE_CANONICAL = 1 + + In this ``uuid_mode``, :func:`loads` recognizes string values containing the + ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`` canonical representation as :class:`UUID` + instances; :func:`dumps` emits same kind of representation for :class:`UUID` instances as a + string value. + +.. data:: UUID_MODE_HEX = 2 + + In this ``uuid_mode`` :func:`loads` recognizes string values containing exactly 32 hex + digits *or* the ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`` canonical representation as + :class:`UUID` instances; :func:`dumps` emits the 32 hex digits of :class:`UUID` instances as + a string value. + +.. testsetup:: + + from rapidjson import (dumps, loads, DATETIME_MODE_NONE, DATETIME_MODE_ISO8601, + DATETIME_MODE_ISO8601_IGNORE_TZ, DATETIME_MODE_ISO8601_UTC, + UUID_MODE_NONE, UUID_MODE_CANONICAL, UUID_MODE_HEX) + +.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, allow_nan=True, indent=None, \ + default=None, sort_keys=False, use_decimal=False, \ + max_recursion_depth=2048, datetime_mode=None, uuid_mode=None) + + :param bool skipkeys: whether skip invalid :class:`dict` keys + :param bool ensure_ascii: whether the output should contain only ASCII characters + :param bool allow_nan: whether ``NaN`` values are handled or not + :param int indent: indentation width to produce pretty printed JSON + :param callable default: a function that gets called for objects that can't otherwise be + serialized + :param bool sort_keys: whether dictionary keys should be sorted alphabetically + :param bool use_decimal: whether :class:`Decimal` should be handled + :param int max_recursion_depth: maximum depth for nested structures + :param int datetime_mode: how should :class:`datetime` and :class:`date` instances be + handled + :param int uuid_mode: how should :class:`UUID` instances be handled + :returns: A Python :class:`str` instance. + + Encode given Python `obj` instance into a JSON string. + + If `skipkeys` is true (default: ``False``), then dict keys that are not of a basic type + (:class:`str`, :class:`int`, :class:`float`, :class:`bool`, ``None``) will be skipped + instead of raising a :exc:`TypeError`: + + .. doctest:: + + >>> dumps({(0,): 'empty tuple'}) + Traceback (most recent call last): + File "", line 1, in + TypeError: keys must be a string + >>> dumps({(0,): 'empty tuple'}, skipkeys=True) + '{}' + + If `ensure_ascii` is true (the default), the output is guaranteed to have all incoming + non-ASCII characters escaped. If `ensure_ascii` is false, these characters will be output + as-is: + + .. doctest:: + + >>> dumps('The symbol for the Euro currency is €') + '"The symbol for the Euro currency is \\u20ac"' + >>> dumps('The symbol for the Euro currency is €', + ... ensure_ascii=False) + '"The symbol for the Euro currency is €"' + + If `allow_nan` is false (default: ``True``), then it will be a :exc:`ValueError` to + serialize out of range :class:`float` values (``nan``, ``inf``, ``-inf``) in strict + compliance of the JSON specification. If `allow_nan` is true, their JavaScript equivalents + (``NaN``, ``Infinity``, ``-Infinity``) will be used: + + .. doctest:: + + >>> nan = float('nan') + >>> inf = float('inf') + >>> dumps([nan, inf]) + '[NaN,Infinity]' + >>> dumps([nan, inf], allow_nan=False) + Traceback (most recent call last): + File "", line 1, in + ValueError: Out of range float values are not JSON compliant + + When `indent` is ``None`` (the default), ``python-rapidjson`` produces the most compact JSON + representation. By setting `indent` to 0 each array item and each dictionary value will be + followed by a newline. A positive integer means that each *level* will be indented by that + many spaces: + + .. code-block:: pycon + + >>> dumps([1, 2, {'three': 3, 'four': 4}]) + '[1,2,{"four":4,"three":3}]' + >>> print(dumps([1, 2, {'three': 3, 'four': 4}], indent=0)) + [ + 1, + 2, + { + "four": 4, + "three": 3 + } + ] + >>> print(dumps([1, 2, {'three': 3, 'four': 4}], indent=2)) + [ + 1, + 2, + { + "four": 4, + "three": 3 + } + ] + + The `default` argument may be used to specify a custom serializer for otherwise not handled + objects. If specified, it should be a function that gets called for such objects and returns + a JSON encodable version of the object itself or raise a :exc:`TypeError`: + + .. doctest:: + + >>> class Point(object): + ... def __init__(self, x, y): + ... self.x = x + ... self.y = y + ... + >>> point = Point(1,2) + >>> dumps(point) + Traceback (most recent call last): + File "", line 1, in + TypeError: <__main__.Point object at …> is not JSON serializable + >>> def point_jsonifier(obj): + ... if isinstance(obj, Point): + ... return {'x': obj.x, 'y': obj.y} + ... else: + ... raise ValueError('%r is not JSON serializable' % obj) + ... + >>> dumps(point, default=point_jsonifier) # doctest: +SKIP + '{"y":2,"x":1}' + + When `sort_keys` is true (default: ``False``), the JSON representation of Python + dictionaries is sorted by key: + + .. doctest:: + + >>> dumps(point, default=point_jsonifier, sort_keys=True) + '{"x":1,"y":2}' + + If `use_decimal` is true (default: ``False``), :class:`Decimal` instances will be + serialized as their textual representation like any other float value, instead of raising + an error: + + .. doctest:: + + >>> from decimal import Decimal + >>> pi = Decimal('3.1415926535897932384626433832795028841971') + >>> dumps(pi) + Traceback (most recent call last): + File "", line 1, in + TypeError: Decimal(…) is not JSON serializable + >>> dumps(pi, use_decimal=True) + '3.1415926535897932384626433832795028841971' + + With `max_recursion_depth` you can control the maximum depth that will be reached when + serializing nested structures: + + .. doctest:: + + >>> a = [] + >>> for i in range(10): + ... a = [a] + ... + >>> dumps(a) + '[[[[[[[[[[[]]]]]]]]]]]' + >>> dumps(a, max_recursion_depth=2) + Traceback (most recent call last): + File "", line 1, in + OverflowError: Max recursion depth reached + + By default :class:`date` and :class:`datetime` instances are not serializable. When + `datetime_mode` is set to :data:`DATETIME_MODE_ISO8601` those values are serialized using + the common `ISO 8601`_ format: + + .. doctest:: + + >>> from datetime import date, datetime + >>> today = date.today() + >>> right_now = datetime.now() + >>> dumps({'date': today, 'timestamp': right_now}) + Traceback (most recent call last): + File "", line 1, in + TypeError: datetime(…) is not JSON serializable + >>> dumps({'a date': today, 'a timestamp': right_now}, + ... datetime_mode=DATETIME_MODE_ISO8601) # doctest: +SKIP + '{"timestamp":"2016-08-28T13:14:52.277256","date":"2016-08-28"}' + + Another mode is :data:`DATETIME_MODE_ISO8601_UTC`, that *shifts* all timestamps to the UTC_ + timezone before serializing them: + + .. doctest:: + + >>> from datetime import timedelta, timezone + >>> here = timezone(timedelta(hours=2)) + >>> now = datetime.now(here) + >>> dumps(now) + Traceback (most recent call last): + File "", line 1, in + TypeError: datetime.datetime(…) is not JSON serializable + >>> dumps(now, datetime_mode=DATETIME_MODE_ISO8601) # doctest: +SKIP + '"2016-08-28T20:31:11.084418+02:00"' + >>> dumps(now, datetime_mode=DATETIME_MODE_ISO8601_UTC) # doctest: +SKIP + '"2016-08-28T18:31:11.084418+00:00"' + + With :data:`DATETIME_MODE_ISO8601_IGNORE_TZ` the timezone, if present, is simply omitted: + + .. doctest:: + + >>> dumps(now, datetime_mode=DATETIME_MODE_ISO8601_IGNORE_TZ) # doctest: +SKIP + '"2016-08-28T20:31:11.084418"' + + Likewise, to handle :class:`UUID` instances there are two modes that can be specified with + the `uuid_mode` argument, that will use the string representation of their values: + + .. doctest:: + + >>> from uuid import uuid4 + >>> random_uuid = uuid4() + >>> dumps(random_uuid) + Traceback (most recent call last): + File "", line 1, in + TypeError: UUID(…) is not JSON serializable + >>> dumps(random_uuid, uuid_mode=UUID_MODE_CANONICAL) # doctest: +SKIP + '"be576345-65b5-4fc2-92c5-94e2f82e38fd"' + >>> dumps(random_uuid, uuid_mode=UUID_MODE_HEX) # doctest: +SKIP + '"be57634565b54fc292c594e2f82e38fd"' + +.. function:: loads(s, object_hook=None, use_decimal=False, precise_float=True, \ + allow_nan=True, datetime_mode=None, uuid_mode=None) + + :param str s: The JSON string to parse + :param callable object_hook: an optional function that will be called with the result of + any object literal decoded (a :class:`dict`) and should return + the value to use instead of the :class:`dict` + :param bool use_decimal: whether :class:`Decimal` should be used for float values + :param bool precise_float: use slower-but-more-precise float parser + :param bool allow_nan: whether ``NaN`` values are recognized + :param int datetime_mode: how should :class:`datetime` and :class:`date` instances be + handled + :param int uuid_mode: how should :class:`UUID` instances be handled + :returns: An equivalent Python object. + + Decode the given Python string `s` containing a JSON formatted value into Python object. + + `object_hook` may be used to inject a custom deserializer that can replace any :class:`dict` + instance found in the JSON structure with a *derived* object instance: + + .. doctest:: + + >>> class Point(object): + ... def __init__(self, x, y): + ... self.x = x + ... self.y = y + ... def __repr__(self): + ... return 'Point(%s, %s)' % (self.x, self.y) + ... + >>> def point_dejsonifier(d): + ... if 'x' in d and 'y' in d: + ... return Point(d['x'], d['y']) + ... else: + ... return d + ... + >>> loads('{"x":1,"y":2}', object_hook=point_dejsonifier) + Point(1, 2) + + If `use_decimal` is true (default: ``False``) then all floating point literals present in + the JSON structure will be returned as :class:`Decimal` instances instead of plain + :class:`float`: + + .. doctest:: + + >>> loads('1.2345', use_decimal=True) + Decimal('1.2345') + + If `precise_float` is false (default: ``True``) then a faster but less precise algorithm + will be used to parse floats values inside the JSON structure + + .. doctest:: + + >>> loads('1.234567890123456789') + 1.2345678901234567 + >>> loads('1.234567890123456789', precise_float=False) + 1.234567890123457 + + If `allow_nan` is false (default: ``True``), then the values ``NaN`` and ``Infinity`` won't + be recognized: + + .. doctest:: + + >>> loads('[NaN, Infinity]') + [nan, inf] + >>> loads('[NaN, Infinity]', allow_nan=False) + Traceback (most recent call last): + File "", line 1, in + ValueError: … Out of range float values are not JSON compliant + + With `datetime_mode` you can enable recognition of string literals containing an `ISO 8601`_ + representation as either :class:`date` or :class:`datetime` instances: + + .. doctest:: + + >>> loads('"2016-01-02T01:02:03+01:00"') + '2016-01-02T01:02:03+01:00' + >>> loads('"2016-01-02T01:02:03+01:00"', + ... datetime_mode=DATETIME_MODE_ISO8601) + datetime.datetime(2016, 1, 2, 1, 2, 3, tzinfo=...delta(0, 3600))) + >>> loads('"2016-01-02T01:02:03+01:00"', + ... datetime_mode=DATETIME_MODE_ISO8601_UTC) + datetime.datetime(2016, 1, 2, 0, 2, 3, tzinfo=...utc) + >>> loads('"2016-01-02T01:02:03+01:00"', + ... datetime_mode=DATETIME_MODE_ISO8601_IGNORE_TZ) + datetime.datetime(2016, 1, 2, 1, 2, 3) + >>> loads('"2016-01-02"', datetime_mode=DATETIME_MODE_ISO8601) + datetime.date(2016, 1, 2) + + With `uuid_mode` you can enable recognition of string literals containing two different + representations of :class:`UUID` values: + + .. doctest:: + + >>> loads('"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"') + 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' + >>> loads('"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"', + ... uuid_mode=UUID_MODE_CANONICAL) + UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') + >>> loads('"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"', + ... uuid_mode=UUID_MODE_HEX) + UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') + >>> loads('"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"', + ... uuid_mode=UUID_MODE_CANONICAL) + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + >>> loads('"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"', + ... uuid_mode=UUID_MODE_HEX) + UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') + + +.. _ISO 8601: https://en.wikipedia.org/wiki/ISO_8601 +.. _UTC: https://en.wikipedia.org/wiki/Coordinated_Universal_Time diff --git a/docs/benchmarks.rst b/docs/benchmarks.rst new file mode 100644 index 0000000..47630d8 --- /dev/null +++ b/docs/benchmarks.rst @@ -0,0 +1,50 @@ +============= + Performance +============= + +``python-rapidjson`` tries to be as performant as possible while staying compatible with the +``json`` module. Here are our current benchmarks: + ++-----------------------------------------+--------+------------+------------+-----------+ +| | ujson | simplejson | rapidjson | yajl | ++=========================================+========+============+============+===========+ +|Array with 256 doubles | | | | | ++-----------------------------------------+--------+------------+------------+-----------+ +| Encode | 13.81s | 12.58s | 1.82s | 8.36s | ++-----------------------------------------+--------+------------+------------+-----------+ +| Decode | 2.00s | 4.65s | 2.19s | 2.09s | ++-----------------------------------------+--------+------------+------------+-----------+ +| | | | | | ++-----------------------------------------+--------+------------+------------+-----------+ +| Array with 256 utf-8 strings | | | | | ++-----------------------------------------+--------+------------+------------+-----------+ +| Encode | 1.98s | 2.36s | 5.09s | 1.45s | ++-----------------------------------------+--------+------------+------------+-----------+ +| Decode | 2.77s | 13.58s | 2.42s | 7.19s | ++-----------------------------------------+--------+------------+------------+-----------+ +| | | | | | ++-----------------------------------------+--------+------------+------------+-----------+ +|100 dictionaries of 100 arrays | | | | | ++-----------------------------------------+--------+------------+------------+-----------+ +| Encode | 1.65s | 5.20s | 0.77s | 2.09s | ++-----------------------------------------+--------+------------+------------+-----------+ +| Decode | 2.76s | 3.86s | 2.79s | 3.47 | ++-----------------------------------------+--------+------------+------------+-----------+ +| | | | | | ++-----------------------------------------+--------+------------+------------+-----------+ + +To run these tests yourself, clone the repo and run: + +.. code-block:: shell + + $ tox -- -m benchmark + +.. caution:: + + The benchmarks may require several Gigs of memory! + + You can skip the benchmarks while running the test suite with: + + .. code-block:: shell + + $ tox -- -m '"not benchmark"' diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..83c4c9b --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,339 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# python-rapidjson documentation build configuration file, created by +# sphinx-quickstart on Sun Aug 28 12:31:30 2016. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +# import os +# import sys +# sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +# +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = ['sphinx.ext.doctest'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# +# source_suffix = ['.rst', '.md'] +source_suffix = '.rst' + +# The encoding of source files. +# +# source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = 'python-rapidjson' +copyright = '2016, Ken Robbins' +author = 'Ken Robbins' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '0.1' +# The full version, including alpha/beta/rc tags. +release = '0.1' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +# +# today = '' +# +# Else, today_fmt is used as the format for a strftime call. +# +# today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This patterns also effect to html_static_path and html_extra_path +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +# +# default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +# +# add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +# +# add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +# +# show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +# modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +# keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'alabaster' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +# +# html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +# html_theme_path = [] + +# The name for this set of Sphinx documents. +# " v documentation" by default. +# +# html_title = 'python-rapidjson v0.1' + +# A shorter title for the navigation bar. Default is the same as html_title. +# +# html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +# +# html_logo = None + +# The name of an image file (relative to this directory) to use as a favicon of +# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +# +# html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +# +# html_extra_path = [] + +# If not None, a 'Last updated on:' timestamp is inserted at every page +# bottom, using the given strftime format. +# The empty string is equivalent to '%b %d, %Y'. +# +# html_last_updated_fmt = None + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +# +# html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +# +# html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +# +# html_additional_pages = {} + +# If false, no module index is generated. +# +# html_domain_indices = True + +# If false, no index is generated. +# +# html_use_index = True + +# If true, the index is split into individual pages for each letter. +# +# html_split_index = False + +# If true, links to the reST sources are added to the pages. +# +# html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +# +# html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +# +# html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +# +# html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +# html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr', 'zh' +# +# html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# 'ja' uses this config value. +# 'zh' user can custom change `jieba` dictionary path. +# +# html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +# +# html_search_scorer = 'scorer.js' + +# Output file base name for HTML help builder. +htmlhelp_basename = 'python-rapidjsondoc' + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'python-rapidjson.tex', 'python-rapidjson Documentation', + 'Ken Robbins', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +# +# latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +# +# latex_use_parts = False + +# If true, show page references after internal links. +# +# latex_show_pagerefs = False + +# If true, show URL addresses after external links. +# +# latex_show_urls = False + +# Documents to append as an appendix to all manuals. +# +# latex_appendices = [] + +# It false, will not define \strong, \code, itleref, \crossref ... but only +# \sphinxstrong, ..., \sphinxtitleref, ... To help avoid clash with user added +# packages. +# +# latex_keep_old_macro_names = True + +# If false, no module index is generated. +# +# latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, 'python-rapidjson', 'python-rapidjson Documentation', + [author], 1) +] + +# If true, show URL addresses after external links. +# +# man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'python-rapidjson', 'python-rapidjson Documentation', + author, 'python-rapidjson', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +# +# texinfo_appendices = [] + +# If false, no module index is generated. +# +# texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +# +# texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +# +# texinfo_no_detailmenu = False diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..f173057 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,29 @@ +================== + python-rapidjson +================== + +---------------------------------- +Python 3 wrapper around RapidJSON_ +---------------------------------- + +This module implements a Python 3 wrapper around RapidJSON_, which is an extremely fast C++ +JSON_ serialization library. + +Contents: + +.. toctree:: + :maxdepth: 2 + + quickstart + benchmarks + api + +.. _RapidJSON: https://github.com/miloyip/rapidjson +.. _JSON: http://json.org/ + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`search` diff --git a/docs/quickstart.rst b/docs/quickstart.rst new file mode 100644 index 0000000..7cfe68a --- /dev/null +++ b/docs/quickstart.rst @@ -0,0 +1,129 @@ +============= + Quick start +============= + +This a quick overview of the module. + + +Installation +------------ + +First install ``python-rapidjson``: + +.. code-block:: bash + + $ pip install python-rapidjson + +As the module is written in C++, you most probably will need to install a minimal C++ compiler +toolchain on your system. + + +Basic examples +-------------- + +``python-rapidjson`` tries to be compatible with the standard library ``json.dumps()`` and +``json.loads()`` functions (but see the incompatibilities_). + +Basic usage looks like this: + +.. doctest:: + + >>> from pprint import pprint + >>> from rapidjson import dumps, loads + >>> data = {'foo': 100, 'bar': 'baz'} + >>> dumps(data, sort_keys=True) # for doctest + '{"bar":"baz","foo":100}' + >>> pprint(loads('{"bar":"baz","foo":100}')) + {'bar': 'baz', 'foo': 100} + +All JSON_ data types are supported using their native Python counterparts: + +.. doctest:: + + >>> int_number = 42 + >>> float_number = 1.4142 + >>> string = "√2 ≅ 1.4142" + >>> false = False + >>> true = True + >>> null = None + >>> array = [int_number, float_number, string, false, true, null] + >>> an_object = {'int': int_number, 'float': float_number, + ... 'string': string, + ... 'true': true, 'false': false, + ... 'array': array } + >>> pprint(loads(dumps({'object': an_object}))) + {'object': {'array': [42, 1.4142, '√2 ≅ 1.4142', False, True, None], + 'false': False, + 'float': 1.4142, + 'int': 42, + 'string': '√2 ≅ 1.4142', + 'true': True}} + +``python-rapidjson`` can optionally handle also a few other commonly used data types: + +.. doctest:: + + >>> import datetime, decimal, uuid + >>> from rapidjson import DATETIME_MODE_ISO8601, UUID_MODE_CANONICAL + >>> some_day = datetime.date(2016, 8, 28) + >>> some_timestamp = datetime.datetime(2016, 8, 28, 13, 14, 15) + >>> dumps({'a date': some_day, 'a timestamp': some_timestamp}) + Traceback (most recent call last): + File "", line 1, in + TypeError: datetime.datetime(…) is not JSON serializable + >>> dumps({'a date': some_day, 'a timestamp': some_timestamp}, + ... datetime_mode=DATETIME_MODE_ISO8601, + ... sort_keys=True) # for doctests + '{"a date":"2016-08-28","a timestamp":"2016-08-28T13:14:15"}' + >>> as_json = _ + >>> pprint(loads(as_json)) + {'a date': '2016-08-28', 'a timestamp': '2016-08-28T13:14:15'} + >>> pprint(loads(as_json, datetime_mode=DATETIME_MODE_ISO8601)) + {'a date': datetime.date(2016, 8, 28), + 'a timestamp': datetime.datetime(2016, 8, 28, 13, 14, 15)} + >>> some_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') + >>> dumps(some_uuid) + Traceback (most recent call last): + File "", line 1, in + TypeError: UUID(…) is not JSON serializable + >>> dumps(some_uuid, uuid_mode=UUID_MODE_CANONICAL) + '"886313e1-3b8a-5372-9b90-0c9aee199e5d"' + >>> as_json = _ + >>> loads(as_json) + '886313e1-3b8a-5372-9b90-0c9aee199e5d' + >>> loads(as_json, uuid_mode=UUID_MODE_CANONICAL) + UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') + >>> pi = decimal.Decimal('3.1415926535897932384626433832795028841971') + >>> dumps(pi) + Traceback (most recent call last): + File "", line 1, in + TypeError: Decimal(…) is not JSON serializable + >>> dumps(pi, use_decimal=True) + '3.1415926535897932384626433832795028841971' + >>> as_json = _ + >>> loads(as_json) + 3.141592653589793 + >>> type(loads(as_json)) + + >>> loads(as_json, use_decimal=True) + Decimal('3.1415926535897932384626433832795028841971') + + +Incompatibilities +----------------- + +Here are things in the standard ``json`` library supports that we have decided +not to support: + +* ``separators`` argument. This is mostly used for pretty printing and not + supported by RapidJSON_ so it isn't a high priority. We do support + ``indent`` kwarg that would get you nice looking JSON anyways. + +* Coercing keys when dumping. ``json`` will turn ``True`` into ``'True'`` if you + dump it out but when you load it back in it'll still be a string. We want the + dump and load to return the exact same objects so we have decided not to do + this coercing. + + +.. _JSON: http://json.org/ +.. _RapidJSON: https://github.com/miloyip/rapidjson diff --git a/tox.ini b/tox.ini index 82d82d6..d8d04e0 100644 --- a/tox.ini +++ b/tox.ini @@ -11,3 +11,11 @@ pip_pre=False commands = {[base]commands} py.test {posargs} + +[testenv:docs] +basepython = python3 +changedir = docs +deps = sphinx +usedevelop = True +commands= + sphinx-build -b doctest -d {envtmpdir}/doctrees . {envtmpdir}/doctest