Repository navigation
Use Ruby's json gem instead of Oj - #563
Open
erikrozendaal wants to merge 1 commit into
Open
erikrozendaal wants to merge 1 commit into
erikrozendaal wants to merge 1 commit into
Conversation
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.
There was a problem hiding this comment.
🟡 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::Jsonand 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), |
| @@ -0,0 +1,17 @@ | |||
| # frozen_string_literal: true | |||
|
|
|||
| require 'json' | |||
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Ruby's own
jsongem 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 forjsonbcolumns, so Sequent and ActiveRecord now load JSON the same way, and Sequent has one native dependency less.Sequent::Core::Ojstays as a deprecated alias of the newSequent::Core::Json, so applications can move at their own pace. Times are now written withSequent.configuration.time_precision(#562); the CHANGELOG lists the other differences with the JSON Oj wrote and loaded. Based on #562.