-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.py
More file actions
48 lines (31 loc) · 1.04 KB
/
Copy pathjson.py
File metadata and controls
48 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""A method for serializing values to JSON."""
from pathlib import Path
import typing
from typeclasses import typeclass
T = typing.TypeVar('T') # pylint: disable=invalid-name
@typeclass(T)
def to_json(value: T) -> str: # type: ignore[empty-body] # pylint: disable=unused-argument
"""Serialize a value to JSON."""
@to_json.instance(type(None))
def _to_json_none(_):
return 'null'
@to_json.instance(bool)
def _to_json_bool(value):
return 'true' if value else 'false'
@to_json.instance(str)
@to_json.instance(bytes)
@to_json.instance(Path)
def _to_json_string(string):
return f'"{str(string)}"'
@to_json.instance(int)
@to_json.instance(float)
def _to_json_number(number):
return str(number)
@to_json.instance(typing.Mapping, protocol=True)
def _to_json_mapping(mapping):
members = ','.join(f'"{k}":{to_json(v)}' for k, v in mapping.items())
return f'{{{members}}}'
@to_json.instance(typing.Iterable, protocol=True)
def _to_json_iterable(iterable):
items = ','.join(to_json(x) for x in iterable)
return f'[{items}]'