Skip to content

Read JSON instances from a stream - #631

Merged
fontanf merged 1 commit into
masterfrom
instance-builder-read-stream
Oct 3, 2026
Merged

fontanf merged 1 commit into
masterfrom
instance-builder-read-stream

Conversation

@fontanf

@fontanf fontanf commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Motivation

The instance readers only took a file path. To read an instance from a string, e.g. in the WebAssembly module where the instance comes from JavaScript, you had to write it to a file first.

Change

  • C++: each InstanceBuilder (all six types) gets a read(std::istream&) overload that reads an instance in the JSON format from a stream. read(instance_path) opens the file and calls it, so the format and behaviour are unchanged.
  • Python: InstanceBuilder.read accepts a path, a path-like object (e.g. pathlib.Path) or a text stream with a read() method (e.g. io.StringIO). Same convention as Solution.write.
    • The bindings used to take the address of InstanceBuilder::read, which is ambiguous now that it's overloaded. They go through two helpers in python/src/common.hpp instead.
  • Doc fix: irregular's read(instance_path) comment said "Read item types from a file".

Testing

  • New test_read_stream in python/tests/test_instance.py, for each problem type:
    • the same JSON instance read from a path, an io.StringIO and a pathlib.Path gives the same instance;
    • invalid JSON raises an error;
    • an argument that's neither a path nor a stream raises TypeError.
  • C++ unit tests: 693 passed. Python tests: 362 passed, 13 skipped.

Each 'InstanceBuilder' gets a 'read(std::istream&)' overload reading an
instance in the JSON format from a stream; 'read(instance_path)' opens
the file and calls it. This allows reading an instance from a string
without writing a file, e.g. in the WebAssembly module.

In Python, 'InstanceBuilder.read' also accepts a text stream (e.g.
'io.StringIO') or a path-like object, like 'Solution.write'.
@fontanf
fontanf merged commit 3f7160e into master Oct 3, 2026
11 checks passed
@fontanf
fontanf deleted the instance-builder-read-stream branch October 3, 2026 16:21
@fontanf fontanf mentioned this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant