Skip to content

JSON streamer: emit valid, correctly-nested event objects#148

Merged
maureeungaro merged 1 commit into
gemc:mainfrom
zhaozhiwen:fix/json-event-structure
Jun 13, 2026
Merged

JSON streamer: emit valid, correctly-nested event objects#148
maureeungaro merged 1 commit into
gemc:mainfrom
zhaozhiwen:fix/json-event-structure

Conversation

@zhaozhiwen

Copy link
Copy Markdown
Collaborator

Reworks the JSON event serialization so events are valid, correctly-nested JSON. Two coupled defects, fixed together because both live in the shared brace-accounting of endEventImpl:

#116 — header never closed. startEventImpl opens "header": { but nothing closed it, so generated and detectors were emitted inside header instead of as siblings. (Single-detector output still parsed, but with the wrong shape.) Fixed by closing the header right after its fields are written (and in the endEventImpl fallback when the header step is skipped), and dropping the now-redundant header-closing brace from endEventImpl.

#115 — multi-detector digitized produced invalid JSON. Digitized data was written inline and "digitized_by_detector" was closed on every per-detector call. The base class interleaves publishEventTrueInfoData/publishEventDigitizedData per detector (gstreamer.cc:93-111), so a second digitized detector extended an already-closed object — extra braces, misplaced keys. (Note: the per-issue proposed fix of "open once, close in endEvent" does not hold under this interleaving — a later detector's true-info would land inside the still-open digitized object.) Fixed by buffering each detector's digitized array and emitting them together as one well-formed "digitized_by_detector" object in endEventImpl.

The existing schema is preserved: per-detector "digitized": [] placeholder plus the "digitized_by_detector" map.

Validation: ran the gstreamer JSON example extended to two detectors (ctof + ftof) in the Geant4 11.4.1 / Qt6 dev container. Before: json.tool rejects the output (Expecting ',' delimiter). After: all worker files parse as valid JSON, with header/generated/detectors as siblings and both detectors' 10 digitized hits present under digitized_by_detector.

Note: filed as one PR because #115 and #116 both rewrite the same endEventImpl brace logic and cannot be cleanly separated.

Fixes #115
Fixes #116

Two coupled defects in the JSON event serialization, both fixed here by
reworking the shared open/close logic in endEventImpl:

- gemc#116: startEventImpl opened the "header" object but nothing closed it,
  so "generated" and "detectors" were nested inside "header" instead of
  being siblings. Close the header right after its fields are written
  (and in the endEvent fallback when the header step is skipped), and
  drop the now-redundant header-closing brace from endEventImpl.

- gemc#115: digitized data was written inline and the "digitized_by_detector"
  object was closed on every per-detector call. Because the base class
  interleaves publishEventTrueInfoData/publishEventDigitizedData per
  detector, a second digitized detector reopened/extended an already
  closed object, producing invalid JSON (extra braces, misplaced keys).
  Buffer each detector's digitized array and emit them together as one
  well-formed "digitized_by_detector" object in endEventImpl.

The existing schema is preserved (per-detector "digitized": [] placeholder
plus the "digitized_by_detector" map). Validated by running the gstreamer
JSON example extended to two detectors: output now parses as valid JSON
with header/generated/detectors as siblings and both detectors' digitized
hits present (Geant4 11.4.1 / Qt6 dev container).

Fixes gemc#115
Fixes gemc#116

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@maureeungaro maureeungaro merged commit f6d4167 into gemc:main Jun 13, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants