|
| 1 | +import json |
1 | 2 | from typing import Dict
|
2 | 3 |
|
| 4 | +from linkml_runtime.dumpers.dumper_root import Dumper |
3 | 5 | from linkml_runtime.utils.context_utils import CONTEXTS_PARAM_TYPE
|
4 | 6 | from linkml_runtime.utils.yamlutils import YAMLRoot, as_json_object
|
5 |
| -from jsonasobj import as_json |
6 | 7 |
|
7 | 8 |
|
8 |
| -def remove_empty_items(obj: Dict) -> Dict: |
9 |
| - """ |
10 |
| - Remove empty items from obj |
11 |
| - :param obj: |
12 |
| - :return: copy of dictionary with empty lists/dicts and Nones removed |
13 |
| - """ |
14 |
| - return {k: v for k, v in obj.items() if not (v is None or v == [] or v == {})} |
| 9 | +class JSONDumper(Dumper): |
15 | 10 |
|
| 11 | + def dump(self, element: YAMLRoot, to_file: str, contexts: CONTEXTS_PARAM_TYPE = None) -> None: |
| 12 | + """ |
| 13 | + Write element as json to to_file |
| 14 | + :param element: LinkML object to be serialized as YAML |
| 15 | + :param to_file: file to write to |
| 16 | + :param contexts: JSON-LD context(s) in the form of: |
| 17 | + * file name |
| 18 | + * URL |
| 19 | + * JSON String |
| 20 | + * dict |
| 21 | + * JSON Object |
| 22 | + * A list containing elements of any type named above |
| 23 | + """ |
| 24 | + super().dump(element, to_file, contexts=contexts) |
16 | 25 |
|
17 |
| -def dump(element: YAMLRoot, to_file: str, contexts: CONTEXTS_PARAM_TYPE = None) -> None: |
18 |
| - """ |
19 |
| - Write element as json to to_file |
20 |
| - :param element: LinkML object to be serialized as YAML |
21 |
| - :param to_file: file to write to |
22 |
| - :param contexts: JSON-LD context(s) in the form of: |
23 |
| - * file name |
24 |
| - * URL |
25 |
| - * JSON String |
26 |
| - * dict |
27 |
| - * JSON Object |
28 |
| - * A list containing elements of any type named above |
29 |
| - """ |
30 |
| - with open(to_file, 'w') as outf: |
31 |
| - outf.write(dumps(element, contexts)) |
| 26 | + def dumps(self, element: YAMLRoot, contexts: CONTEXTS_PARAM_TYPE = None) -> str: |
| 27 | + """ |
| 28 | + Return element as a JSON or a JSON-LD string |
| 29 | + :param element: LinkML object to be emitted |
| 30 | + :param contexts: JSON-LD context(s) in the form of: |
| 31 | + * file name |
| 32 | + * URL |
| 33 | + * JSON String |
| 34 | + * dict |
| 35 | + * JSON Object |
| 36 | + * A list containing elements of any type named above |
| 37 | + :return: JSON Object representing the element |
| 38 | + """ |
| 39 | + return json.dumps(as_json_object(element, contexts), |
| 40 | + default=lambda o: self.remove_empty_items(o) if isinstance(o, YAMLRoot) else json.JSONDecoder().decode(o), |
| 41 | + indent=' ') |
32 | 42 |
|
33 | 43 |
|
34 |
| -def dumps(element: YAMLRoot, contexts: CONTEXTS_PARAM_TYPE = None) -> str: |
35 |
| - """ |
36 |
| - Return element as a JSON or a JSON-LD string |
37 |
| - :param element: LinkML object to be emitted |
38 |
| - :param contexts: JSON-LD context(s) in the form of: |
39 |
| - * file name |
40 |
| - * URL |
41 |
| - * JSON String |
42 |
| - * dict |
43 |
| - * JSON Object |
44 |
| - * A list containing elements of any type named above |
45 |
| - :return: JSON Object representing the element |
46 |
| - """ |
47 |
| - return as_json(as_json_object(element, contexts), filtr=remove_empty_items, indent=' ') |
| 44 | + @staticmethod |
| 45 | + def remove_empty_items(obj: Dict) -> Dict: |
| 46 | + """ |
| 47 | + Remove empty items from obj |
| 48 | + :param obj: |
| 49 | + :return: copy of dictionary with empty lists/dicts and Nones removed |
| 50 | + """ |
| 51 | + return {k: v for k, v in obj.__dict__.items() if not (v is None or v == [] or v == {})} |
0 commit comments