add jsonencode(), 0.2.1
This commit is contained in:
parent
82b688fe86
commit
7802ac2367
3 changed files with 26 additions and 4 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 0.2.1
|
||||||
|
|
||||||
|
- Add `codecs.jsonencode`
|
||||||
|
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .iding import Siq, SiqCache, SiqType, SiqGen
|
from .iding import Siq, SiqCache, SiqType, SiqGen
|
||||||
from .codecs import StringCase, cb32encode, cb32decode
|
from .codecs import StringCase, cb32encode, cb32decode, jsonencode
|
||||||
from .bits import count_ones, mask_shift
|
from .bits import count_ones, mask_shift
|
||||||
from .configparse import MissingConfigError, MissingConfigWarning, ConfigOptions, ConfigParserConfigSource, ConfigSource, DictConfigSource, ConfigValue, EnvConfigSource
|
from .configparse import MissingConfigError, MissingConfigWarning, ConfigOptions, ConfigParserConfigSource, ConfigSource, DictConfigSource, ConfigValue, EnvConfigSource
|
||||||
from .functools import deprecated, not_implemented
|
from .functools import deprecated, not_implemented
|
||||||
|
|
@ -25,11 +25,11 @@ from .classtools import Wanted, Incomplete
|
||||||
from .itertools import makelist, kwargs_prefix
|
from .itertools import makelist, kwargs_prefix
|
||||||
from .i18n import I18n, JsonI18n, TomlI18n
|
from .i18n import I18n, JsonI18n, TomlI18n
|
||||||
|
|
||||||
__version__ = "0.2.0"
|
__version__ = "0.2.1"
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase',
|
'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase',
|
||||||
'MissingConfigError', 'MissingConfigWarning', 'ConfigOptions', 'ConfigParserConfigSource', 'ConfigSource', 'ConfigValue', 'EnvConfigSource', 'DictConfigSource',
|
'MissingConfigError', 'MissingConfigWarning', 'ConfigOptions', 'ConfigParserConfigSource', 'ConfigSource', 'ConfigValue', 'EnvConfigSource', 'DictConfigSource',
|
||||||
'deprecated', 'not_implemented', 'Wanted', 'Incomplete',
|
'deprecated', 'not_implemented', 'Wanted', 'Incomplete', 'jsonencode'
|
||||||
'makelist', 'kwargs_prefix', 'I18n', 'JsonI18n', 'TomlI18n', 'cb32encode', 'cb32decode', 'count_ones', 'mask_shift'
|
'makelist', 'kwargs_prefix', 'I18n', 'JsonI18n', 'TomlI18n', 'cb32encode', 'cb32decode', 'count_ones', 'mask_shift'
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
import datetime
|
||||||
import enum
|
import enum
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
|
from typing import Any, Callable
|
||||||
|
|
||||||
from suou.functools import not_implemented
|
from suou.functools import not_implemented
|
||||||
|
|
||||||
|
|
@ -74,6 +77,22 @@ def b64decode(val: bytes | str) -> bytes:
|
||||||
val = val.encode('ascii')
|
val = val.encode('ascii')
|
||||||
return base64.urlsafe_b64decode(val.replace(b'/', b'_').replace(b'+', b'-') + b'=' * ((4 - len(val) % 4) % 4))
|
return base64.urlsafe_b64decode(val.replace(b'/', b'_').replace(b'+', b'-') + b'=' * ((4 - len(val) % 4) % 4))
|
||||||
|
|
||||||
|
def _json_default(func = None) -> Callable[Any, str | list | dict]:
|
||||||
|
def default_converter(obj: Any) -> str | list | dict:
|
||||||
|
if isinstance(obj, (datetime.datetime, datetime.date)):
|
||||||
|
return obj.isoformat()
|
||||||
|
elif callable(func):
|
||||||
|
return func(obj)
|
||||||
|
else:
|
||||||
|
raise TypeError
|
||||||
|
return default_converter
|
||||||
|
|
||||||
|
def jsonencode(obj: dict, *, skipkeys: bool = True, separators: tuple[str, str] = (',', ':'), default: Callable | None = None, **kwargs) -> str:
|
||||||
|
'''
|
||||||
|
JSON encoder with stricter and smarter defaults.
|
||||||
|
'''
|
||||||
|
return json.dumps(obj, skipkeys=skipkeys, separators=separators, default=_json_default(default), **kwargs)
|
||||||
|
|
||||||
class StringCase(enum.Enum):
|
class StringCase(enum.Enum):
|
||||||
"""
|
"""
|
||||||
Enum values used by regex validators and storage converters.
|
Enum values used by regex validators and storage converters.
|
||||||
|
|
@ -108,6 +127,6 @@ class StringCase(enum.Enum):
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'cb32encode', 'cb32decode', 'b32lencode', 'b32ldecode', 'b64encode', 'b64decode',
|
'cb32encode', 'cb32decode', 'b32lencode', 'b32ldecode', 'b64encode', 'b64decode', 'jsonencode'
|
||||||
'StringCase'
|
'StringCase'
|
||||||
)
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue