Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johnnyutahh opened this issue Jul 21, 2022 · 2 comments
Open

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

johnnyutahh opened this issue Jul 21, 2022 · 2 comments

Comments

@johnnyutahh
Copy link

johnnyutahh commented Jul 21, 2022

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
Copy link
Owner

Hi @johnnyutahh ,

Try this:

jsons.set_serializer(lambda obj, *_, **__: obj, bytes)

@johnnyutahh
Copy link
Author

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
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants