1 | # |
||
2 | # Copyright (C) 2011 - 2018 Satoru SATOH <ssato @ redhat.com> |
||
3 | # License: MIT |
||
4 | # |
||
5 | # Ref. python -c "import json; help(json)" |
||
6 | # |
||
7 | # pylint: disable=import-error |
||
8 | r"""JSON backend: |
||
9 | |||
10 | - Format to support: JSON, http://www.json.org |
||
11 | - Requirements: json in python standard library (>= python 2.6) or simplejson |
||
12 | - Development Status :: 5 - Production/Stable |
||
13 | - Limitations: None obvious |
||
14 | - Special options: |
||
15 | |||
16 | - All options of json.load{s,} and json.dump{s,} except object_hook |
||
17 | should work. |
||
18 | |||
19 | - See also: https://docs.python.org/3/library/json.html or |
||
20 | https://docs.python.org/2/library/json.html dependent on the python version |
||
21 | to use. |
||
22 | |||
23 | Changelog: |
||
24 | |||
25 | .. versionchanged:: 0.9.6 |
||
26 | |||
27 | - Add support of loading primitives other than mapping objects. |
||
28 | |||
29 | .. versionadded:: 0.0.1 |
||
30 | """ |
||
31 | from __future__ import absolute_import |
||
32 | |||
33 | try: |
||
34 | import json |
||
35 | except ImportError: |
||
36 | import simplejson as json |
||
37 | |||
38 | import anyconfig.backend.base |
||
39 | import anyconfig.compat |
||
40 | |||
41 | |||
42 | _LOAD_OPTS = ["cls", "object_hook", "parse_float", "parse_int", |
||
43 | "parse_constant"] |
||
44 | _DUMP_OPTS = ["skipkeys", "ensure_ascii", "check_circular", "allow_nan", |
||
45 | "cls", "indent", "separators", "default", "sort_keys"] |
||
46 | _DICT_OPTS = ["object_hook"] |
||
47 | |||
48 | # It seems that 'encoding' argument is not allowed in json.load[s] and |
||
49 | # json.dump[s] in JSON module in python 3.x. |
||
50 | if not anyconfig.compat.IS_PYTHON_3: |
||
51 | _LOAD_OPTS.append("encoding") |
||
52 | _DUMP_OPTS.append("encoding") |
||
53 | |||
54 | if not anyconfig.compat.IS_PYTHON_2_6: |
||
55 | _LOAD_OPTS.append("object_pairs_hook") |
||
56 | _DICT_OPTS.insert(0, "object_pairs_hook") # Higher prio. than object_hook |
||
57 | |||
58 | |||
59 | View Code Duplication | class Parser(anyconfig.backend.base.StringStreamFnParser): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
60 | """ |
||
61 | Parser for JSON files. |
||
62 | """ |
||
63 | _type = "json" |
||
64 | _extensions = ["json", "jsn", "js"] |
||
65 | _load_opts = _LOAD_OPTS |
||
66 | _dump_opts = _DUMP_OPTS |
||
67 | _ordered = not anyconfig.compat.IS_PYTHON_2_6 |
||
68 | _allow_primitives = True |
||
69 | _dict_opts = _DICT_OPTS |
||
70 | |||
71 | _load_from_string_fn = anyconfig.backend.base.to_method(json.loads) |
||
72 | _load_from_stream_fn = anyconfig.backend.base.to_method(json.load) |
||
73 | _dump_to_string_fn = anyconfig.backend.base.to_method(json.dumps) |
||
74 | _dump_to_stream_fn = anyconfig.backend.base.to_method(json.dump) |
||
75 | |||
76 | # vim:sw=4:ts=4:et: |
||
77 |