[issue42791] There is no way to json encode object to str.

2021-08-31 Thread Irit Katriel


Irit Katriel  added the comment:

See also Issue35111.

--
nosy: +iritkatriel

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42791] There is no way to json encode object to str.

2021-08-31 Thread Irit Katriel


Change by Irit Katriel :


--
nosy: +ezio.melotti, rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42791] There is no way to json encode object to str.

2020-12-30 Thread Artyom Kaltovich


New submission from Artyom Kaltovich :

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.

--
components: Library (Lib)
messages: 384085
nosy: kaltovichartyom
priority: normal
severity: normal
status: open
title: There is no way to json encode object to str.

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com