|
| 1 | +# |
| 2 | +# Copyright 2021 IBM Corp. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +"JSON file loaders." |
| 18 | + |
| 19 | + |
| 20 | +from typing import cast, Dict, Union, Any |
| 21 | +import json |
| 22 | + |
| 23 | +from .. import typing as typing_ |
| 24 | +from ..schema import SchemaDict |
| 25 | +from ._base import Loader |
| 26 | + |
| 27 | + |
| 28 | +class JSONLoader(Loader): |
| 29 | + """Loads a JSON file to an object representing the data.""" |
| 30 | + |
| 31 | + def load(self, path: Union[typing_.PathLike, Dict[str, str]], options: SchemaDict) -> Any: |
| 32 | + """ |
| 33 | + :param path: The path to the JSON file. |
| 34 | + :param options: None for JSON loader. |
| 35 | + :raises TypeError: ``path`` is not a path-like object. |
| 36 | + :return: An object representing loaded data. See :meth:`json.load` for details. |
| 37 | + """ |
| 38 | + |
| 39 | + super().load(path, options) |
| 40 | + |
| 41 | + # We can remove usage of cast once Dict[str, str] handling is added |
| 42 | + path = cast(typing_.PathLike, path) |
| 43 | + |
| 44 | + with open(path) as json_file: |
| 45 | + return json.load(json_file) |
0 commit comments