Mercurial > hgrepos > Python > libs > ConfigMix
view configmix/json.py @ 129:df60417d7665
The JSON scanner allows only decimal integers
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Wed, 04 Apr 2018 23:17:37 +0200 |
| parents | 95ad65c69561 |
| children | 2f2e819e8d17 |
line wrap: on
line source
# -*- coding: utf-8 -*- #- # :Copyright: (c) 2018, Franz Glasner. All rights reserved. # :License: 3-clause BSD. See LICENSE.txt for details. #- """Read JSON-style configuration files. """ from __future__ import division, absolute_import, print_function import io import json.decoder try: from collections import OrderedDict as DictImpl except ImportError: try: from ordereddict import OrderedDict as DictImpl except ImportError: DictImpl = dict __all__ = ["load"] # # Determine whether the JSONDecoder has the "object_pairs_hook" # parameter once # try: json.decoder.JSONDecoder(object_pairs_hook=DictImpl) except TypeError: _with_object_pairs_hook = False else: _with_object_pairs_hook = True def load(filename, encoding="utf-8"): """Load a single JSON file with name `filename` and encoding `encoding`. .. todo:: Allow comments in JSON files """ with io.open(filename, mode="rt", encoding=encoding) as jsfp: # # The scanner (not to be changed yet) does only recognize decimal # integers yet. # kwds = { "parse_int": lambda s: int(s, 0), "strict": False } if _with_object_pairs_hook: kwds["object_pairs_hook"] = DictImpl decoder = json.decoder.JSONDecoder(**kwds) return decoder.decode(jsfp.read())
