Skip to content

Use Ruby's json gem instead of Oj - #563

Open
erikrozendaal wants to merge 1 commit into
improvement/time-precision-defaultfrom
improvement/json-gem
Open

erikrozendaal wants to merge 1 commit into
improvement/time-precision-defaultfrom
improvement/json-gem

Conversation

@erikrozendaal

Copy link
Copy Markdown
Member

Ruby's own json gem is now faster than Oj in rails mode: dumping an event takes about 16.5 µs instead of 20 µs, and loading it 2.0 µs instead of 2.15 µs. ActiveRecord already uses it for jsonb columns, so Sequent and ActiveRecord now load JSON the same way, and Sequent has one native dependency less. Sequent::Core::Oj stays as a deprecated alias of the new Sequent::Core::Json, so applications can move at their own pace. Times are now written with Sequent.configuration.time_precision (#562); the CHANGELOG lists the other differences with the JSON Oj wrote and loaded. Based on #562.

The json gem that ships with Ruby is now faster than Oj in rails mode:
dumping an event takes about 16.5 µs instead of 20 µs, and loading it
2.0 µs instead of 2.15 µs. ActiveRecord already uses it for jsonb
columns, so Sequent and ActiveRecord now load JSON the same way, and
Sequent has one native dependency less.

Times are written by AttributeSupport#as_json, using Sequent's
time_precision, which until now did not apply to the JSON of events.
@erikrozendaal
erikrozendaal requested a balanced review from Copilot November 4, 2026 16:46
@erikrozendaal
erikrozendaal marked this pull request as ready for review November 4, 2026 16:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The JSON changes break standalone dumping, leave timestamp precision loss in persistence and test helpers, and introduce nesting limits that can block replay of previously supported events.

5 open issues
What changed in this PR

Replaces Oj with Ruby’s json gem throughout Sequent’s serialization paths, while retaining a deprecated compatibility wrapper. Builds on #562 by applying Sequent’s configured time precision during attribute serialization.

Changes:

  • Adds Sequent::Core::Json and migrates persistence and test helpers to it.
  • Adds coverage for numeric handling, time precision, and the deprecated Oj wrapper.
  • Updates dependencies and documents serialization differences.
File Description
spec/​lib/​sequent/​core/​serializes_event_spec.rb Uses the new JSON loader in event tests.
spec/​lib/​sequent/​core/​serializes_command_spec.rb Uses the new JSON loader in command tests.
spec/​lib/​sequent/​core/​sequent_oj_spec.rb Removes the superseded Oj-specific spec.
spec/​lib/​sequent/​core/​json_spec.rb Tests serialization behavior and Oj compatibility.
spec/​lib/​sequent/​core/​event_store_spec.rb Updates event-store assertions to use Json.
spec/​lib/​sequent/​core/​event_spec.rb Migrates event round-trip tests to Json.
sequent.gemspec Replaces the Oj dependency with json.
lib/​sequent/​test/​workflow_helpers.rb Uses Json for payload comparisons.
lib/​sequent/​test/​command_handler_helpers.rb Uses Json for test-event serialization.
lib/​sequent/​core/​snapshot_store.rb Uses Json when storing snapshots.
lib/​sequent/​core/​snapshot_record.rb Requires the new serializer.
lib/​sequent/​core/​sequent_oj.rb Delegates to Json with a deprecation warning.
lib/​sequent/​core/​json.rb Adds the JSON serialization wrapper.
lib/​sequent/​core/​helpers/​attribute_support.rb Recursively applies configured time formatting.
lib/​sequent/​core/​event_store.rb Migrates event-store serialization to Json.
lib/​sequent/​core/​event_record.rb Uses Json for event record serialization.
lib/​sequent/​core/​core.rb Loads Json alongside the compatibility wrapper.
lib/​sequent/​core/​command_record.rb Uses Json for command record serialization.
gemfiles/​ar_8_1.gemfile.lock Updates dependencies for ActiveRecord 8.1.
gemfiles/​ar_8_0.gemfile.lock Updates dependencies for ActiveRecord 8.0.
gemfiles/​ar_7_2.gemfile.lock Updates dependencies for ActiveRecord 7.2.
CHANGELOG.md Documents migration and serialization differences.

Review effort: Balanced

self.user_id = command.user_id if command.respond_to? :user_id
self.command_type = command.class.name
self.command_json = serialize_json? ? Sequent::Core::Oj.dump(command.attributes) : command.attributes
self.command_json = serialize_json? ? Sequent::Core::Json.dump(command.attributes) : command.attributes
Comment on lines +386 to +387
Sequent::Core::Json.dump(command_record),
Sequent::Core::Json.dump(events),
Comment thread lib/sequent/core/json.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'json'
Comment thread lib/sequent/core/json.rb
Comment on lines +9 to +13
::JSON.generate(obj.as_json)
end

def self.strict_load(json)
::JSON.parse(json)
Comment on lines +71 to +72
Sequent::Core::Json.strict_load(Sequent::Core::Json.dump(actual.payload)),
).to eq(Sequent::Core::Json.strict_load(Sequent::Core::Json.dump(expected.payload)))

This branch has not been deployed

No deployments
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.

2 participants