Skip to content

ModelDumper output buffer persists across calls #59

Description

@yarikoptic

Reusing a single ModelDumper instance across two `dumps()calls returns the **concatenation** of both inputs on the second call. This makes the model API unusable for any program that processes more than one document, and likely explains whytests/test_roundtrip.py`` is
fully commented out upstream.

Reproduce, just run

from json5.dumper import ModelDumper, dumps
from json5.loader import ModelLoader, loads


dumper = ModelDumper()  # SINGLE instance, reused below

a = loads('{"a": 1}', loader=ModelLoader())
b = loads('{"b": 2}', loader=ModelLoader())

out_a = dumps(a, dumper=dumper)
out_b = dumps(b, dumper=dumper)

print(f"first  dumps -> {out_a!r}")
print(f"second dumps -> {out_b!r}")

and get

    first  dumps -> '{"a": 1}'
    second dumps -> '{"a": 1}{"b": 2}'

I thought that may be it is just by design to be cumulative across records... but unlikely since

a = loads('{"a": 1, "c": 4}', loader=ModelLoader())
b = loads('{"a": 2, "b": 2}', loader=ModelLoader())

out_a = dumps(a, dumper=dumper)
out_b = dumps(b, dumper=dumper)

print(f"second dumps -> {out_b!r}")

leading to not really sensible

second dumps -> '{"a": 1, "c": 4}{"a": 2, "b": 2}'

It was discovered while claude code round-tripping 2,151 sidecars from the https://github.com/bids-standard/bids-examples/ which are quite a wild west of formattings of jsons there. Good news is that after working around this issue, it reported that all round trips succeeded.

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