Open johnnyutahh opened 2 years ago
Hi @johnnyutahh ,
Try this:
jsons.set_serializer(lambda obj, *_, **__: obj, bytes)
This works, thanks @ramonhagenaars!
Details:
$ cat jsons-dump-string-type-test.py
#!/usr/bin/env python3
# testing type-hint maneuvers with jsons.
# this is code used for (now solved) following jsons issue:
# https://github.com/ramonhagenaars/jsons/issues/181
# ~Johnny, 2022-07-27
from dataclasses import dataclass
import jsons
# https://github.com/ramonhagenaars/jsons/issues/181#issuecomment-1197305338
jsons.set_serializer(lambda obj, *_, **__: obj, bytes)
@dataclass
class strs_and_bytes:
originally_bytes_type : bytes
originally_str___type : str
sandb1 = strs_and_bytes \
(
originally_bytes_type = '1234' ,
originally_str___type = '1234' ,
)
print('"' + str(jsons.dump(sandb1)) + '"')
$
$
$ python3 !$
python3 jsons-dump-string-type-test.py
"{'originally_bytes_type': '1234', 'originally_str___type': '1234'}"
$
$
$ python3 --version
Python 3.8.10
$ !pip3
pip3 install jsons
Requirement already satisfied: jsons in /usr/local/lib/python3.8/dist-packages (1.6.3)
Requirement already satisfied: typish>=1.9.2 in /usr/local/lib/python3.8/dist-packages (from jsons) (1.9.3)
$ !lsb_
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
$
How can one make
jsons.dump()
treatbytes
the same as Python3str
? Some sort of serializer/class/setting change/override, perhaps?On my system (below)
jsons.dump()
prints mybytes
type variables in a List-like format, even when assigning said variables astr
literal (I think... maybe I don't understand Python3 properly? Quite possible). The pertinent excerpt:"{'originally_bytes_type': ['1', '2', '3', '4'], 'originally_str___type': '1234'}"
All the context: