ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
289 stars 41 forks source link

How to make `jsons.dump()` treat `bytes` the same as Python3 `str`? #181

Open johnnyutahh opened 2 years ago

johnnyutahh commented 2 years ago

How can one make jsons.dump() treat bytes the same as Python3 str? Some sort of serializer/class/setting change/override, perhaps?

On my system (below) jsons.dump() prints my bytes type variables in a List-like format, even when assigning said variables a str 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:

$ cat jsons-dump-string-type-test.py
#!/usr/bin/env python3

from dataclasses import dataclass
import jsons

@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 jsons-dump-string-type-test.py
"{'originally_bytes_type': ['1', '2', '3', '4'], 'originally_str___type': '1234'}"
$ python3 --version
Python 3.8.10
$ pip3 install josons
^CERROR: Operation cancelled by user
$ 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_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:    20.04
Codename:   focal
$
ramonhagenaars commented 2 years ago

Hi @johnnyutahh ,

Try this:

jsons.set_serializer(lambda obj, *_, **__: obj, bytes)
johnnyutahh commented 2 years ago

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
$