Skip to content

json.JSONEncoder.iterencode drops entries from dict subclasses with false truthiness #158732

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: json.dumps documentation: "Serialize obj to a JSON formatted str." JSONEncoder.iterencode documentation: "Encode the given object, o, and yield each string representation as available." Both describe serialization of the given object.

Expected: Both APIs return '{"a": 1}'.

Actual: json.dumps returns '{"a": 1}', while joined JSONEncoder.iterencode returns '{}'.

import json

class D(dict):
    def __bool__(self):
        return False

d = D(a=1)
if not all(type(k) is str and type(v) is int for k, v in d.items()):
    print("REFUTATION REJECTED: invalid input")
else:
    actual = json.dumps(d), ''.join(json.JSONEncoder().iterencode(d))
    reference = ''.join(json.JSONEncoder().iterencode(dict(d)))
    expected = reference, reference
    if actual != expected:
        print("REFUTATION CONFIRMED:", d, "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED: both APIs match the dictionary reference")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library json:

REFUTATION CONFIRMED: {'a': 1} actual: ('{"a": 1}', '{}') expected: ('{"a": 1}', '{"a": 1}')

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/json-20261003-130022-c2

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions