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

There is no way to json encode object to str. #86957

Open
kaltovichartyom mannequin opened this issue Dec 30, 2020 · 2 comments
Open

There is no way to json encode object to str. #86957

kaltovichartyom mannequin opened this issue Dec 30, 2020 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@kaltovichartyom
Copy link
Mannequin

kaltovichartyom mannequin commented Dec 30, 2020

BPO 42791
Nosy @rhettinger, @ezio-melotti, @iritkatriel

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-12-30.20:22:51.914>
labels = ['library']
title = 'There is no way to json encode object to str.'
updated_at = <Date 2021-08-31.20:34:24.741>
user = 'https://bugs.python.org/kaltovichartyom'

bugs.python.org fields:

activity = <Date 2021-08-31.20:34:24.741>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-12-30.20:22:51.914>
creator = 'kaltovichartyom'
dependencies = []
files = []
hgrepos = []
issue_num = 42791
keywords = []
message_count = 2.0
messages = ['384085', '400776']
nosy_count = 4.0
nosy_names = ['rhettinger', 'ezio.melotti', 'iritkatriel', 'kaltovichartyom']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue42791'
versions = []

@kaltovichartyom
Copy link
Mannequin Author

kaltovichartyom mannequin commented Dec 30, 2020

Hello.

At first I want to say thank you for all your efforts in python development. I really appreciate it. :)

I am trying to convert custom object to json.

But, I've found a problem. JSONEncoder has default method for converting custom objects to some primitives and encode for converting structures.

But what if I want to return completed json string? I can't do it in default, because JSONEncoder will think it is string and encode it accordingly later in iterencode method.

Then I tried redefine encode, but it is called with the dict(array_name=default(o)) so I should convert the dict as a whole and basically reimplement all conversions (for int, float, and lists of course).

Did I missed something or there is no way to do it?

I suggest to introduce another method, e.g. encode_obj and call it there:

https://github.com/python/cpython/blob/master/Lib/json/encoder.py#L438

   o = _default(o)
   yield from _iterencode(o, _current_indent_level)

->

   o = _encode_obj(_default(o))
   yield from _iterencode(o, _current_indent_level)

If you are agree I would be happy to implement it.

Best Regards,
Artsiom.

@kaltovichartyom kaltovichartyom mannequin added the stdlib Python modules in the Lib dir label Dec 30, 2020
@iritkatriel
Copy link
Member

See also bpo-35111.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

2 participants