Skip to content

json C vs pure-python behavior difference #137888

@ptth222

Description

@ptth222

Bug report

Bug description:

This is a little convoluted, but I will explain how I got here are the end. The short explanation of the problem is that if you create a class that inherits from dict the C encoder doesn't recognize it quite right and won't print the contents. The code example below illustrates the issue, note that the indent parameter to dumps is basically used to toggle between C and pure-python encoders.

Code to illustrate the issue:

import json


class NewDict(dict):
    def __init__(self, internal_dict=None):
        if internal_dict is None:
            internal_dict = {}
        self.internal_dict = internal_dict
    
    def items(self):
        items = []
        for key, value in self.internal_dict.items():
            items.append((key, value))
        return items
        
    def __bool__(self):
        return len(self.internal_dict) > 0


# This will only have '{}' as output even with items() and __bool__ defined.
with open('C:/Users/Sparda/Desktop/New folder (2)/__json_test.json','w') as jsonFile:
    jsonFile.write(json.dumps(NewDict({'key':'value'})))

# This will only have '{}' as output if items() is only defined, must have __bool__ defined as well to get expected output.
with open('C:/Users/Sparda/Desktop/New folder (2)/__json_test2.json','w') as jsonFile:
    jsonFile.write(json.dumps(NewDict({'key':'value'}), indent=2))

"""
{
  "key": "value"
}
"""

#59091 might be related. I always use indents, so this isn't a big deal for me, but I wanted to document the issue. I don't know much about how Python objects transfer to C, so there might be a way to overwrite another method or something and get the C version to work as expected. Let me know if so please.

As to how I got to this goofy situation, it is because of duplicate keys in JSON. I have a project where the read in JSON data has some duplicate keys, so to be able to output the same thing I have to be able to read in and write out duplicate keys. The class in the example doesn't fully do this, I cut it down just to illustrate this issue, but that's how I got here.

CPython versions tested on:

3.10

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions