Skip to content

Render JSON sink output with yojson#2

Merged
henrylee97 merged 1 commit into
mainfrom
json-yojson
Jun 2, 2026
Merged

Render JSON sink output with yojson#2
henrylee97 merged 1 commit into
mainfrom
json-yojson

Conversation

@henrylee97

Copy link
Copy Markdown
Member

Summary

Scribe_sinks.Json previously hand-rolled JSON serialization with a Buffer: manual string escaping, value rendering, and byte-by-byte assembly of the {level,message,fields} object. This replaces that logic with yojson, which already implements the same escaping rules correctly.

The sink now builds a Yojson.Safe.t value from the event and serializes it with Yojson.Safe.to_string. The public interface (string_of_event, channel, stderr) and the exact output are unchanged.

Changes

  • lib/sinks/json.ml: build and serialize a yojson value instead of writing to a Buffer (≈44 lines removed).
  • lib/sinks/dune: add yojson to the library's dependencies.
  • dune-project / scribe.opam: declare yojson in the scribe package depends (opam file regenerated by generate_opam_files).

Compatibility

Output is byte-identical to the previous implementation. yojson's compact writer produces spaceless output and uses the same escaping (\", \\, \b, \f, \n, \r, \t, \u00xx for control chars, UTF-8 passed through), and `Assoc preserves field order. The existing test_json_sink, which pins the exact JSON line, passes unchanged.

Verification

dune build, dune runtest, dune build @fmt, and dune build @install all pass.

Replace the hand-rolled Buffer-based JSON serialization in
Scribe_sinks.Json with a yojson value built from the event, serialized
via Yojson.Safe.to_string. The compact output and string escaping are
byte-identical to the previous implementation, so the existing
test_json_sink regression test passes unchanged.

Add yojson as a dependency of the scribe.sinks library and the scribe
package.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@henrylee97 henrylee97 self-assigned this Jun 2, 2026
@henrylee97 henrylee97 added the refactor For tasks related to publishing a new version. label Jun 2, 2026
@henrylee97 henrylee97 enabled auto-merge (squash) June 2, 2026 07:35
@henrylee97 henrylee97 merged commit 9ae958d into main Jun 2, 2026
2 checks passed
@henrylee97 henrylee97 deleted the json-yojson branch June 2, 2026 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor For tasks related to publishing a new version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant