[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-29 Thread Terry J. Reedy
Terry J. Reedy added the comment: I am surprised that dumping to a string and to a file give different answers. This strikes me as a bug in any case. I would expect the the main difference would be file.write(chunk) versus temlist.append(chunk). -- nosy: +terry.reedy ___

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread R. David Murray
R. David Murray added the comment: If you break the invariants (in this case: a list has an accurate len) code that expects lists is only going to work by accident. What you really want to do is define your own json encoder for your type. If that isn't possible for a streamed sequence of unde

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread Grigory Statsenko
Grigory Statsenko added the comment: With streaming you never know the real length before you're done iterating. Anyway, the fix really shouldn't be that complicated: In _iterencode_list just yield the '[' instead of saving it to buf -- ___ Python t

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread Grigory Statsenko
Grigory Statsenko added the comment: If __len__ is not defined, then the iterator is considered empty and is always rendered as [] even if it really isn't empty -- ___ Python tracker __

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread SilentGhost
SilentGhost added the comment: The question is why are you defining __len__ if you don't know the size of your final object? Or at least, why are you starting with a potentially wrong initial value? This issue doesn't exist if you either don't define the method or return correct value. --

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread Grigory Statsenko
Grigory Statsenko added the comment: My bad - it doesn't work with non-empty iterators if you set len to 0, so not a solution -- ___ Python tracker ___ _

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread Grigory Statsenko
Grigory Statsenko added the comment: Actually, it does work with len = 0 even if the iterator is not empty. So, I guess that is a solution. But still, I think the more correct way would be to make it work with > 0 -- ___ Python tracker

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread Grigory Statsenko
Grigory Statsenko added the comment: I can't do that if I don't know how many entries there will be ahead of time. In my real-life situation I'm fetching the data from a database not knowing how many entries I'll get before I actually get them (in the iterator). In most cases there are huge am

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread SilentGhost
SilentGhost added the comment: Why does your __len__ method returns 1? Shouldn't it be 0 since this is an empty iterator? Changing it to zero seems to fix the "issue" too. -- nosy: +SilentGhost ___ Python tracker

[issue27613] Empty iterator is rendered as a single bracket ] when using json's iterencode

2016-07-25 Thread Grigory Statsenko
New submission from Grigory Statsenko: JSONEncoder.iterencode doesn't work with empty iterators correctly. Steps: 1. Define an iterator that is recognized by json as a list (inherit from list and define nonzero __len__). 2. Use json.dump with data containing an empty iterator defined as describe