Skip to content

Commit dddb863

Browse files
authored
Drop multi_json dependency and fix Time serialization regression (#404)
Switching from MultiJson.dump to the standard JSON library in a prior release broke Time serialization for apps using ActiveSupport: Time values were no longer encoded as ISO 8601 strings because the standard JSON backend does not invoke ActiveSupport's as_json chain. Replace MultiJson.dump(serializable_hash(...)) with serializable_hash(...).to_json so that ActiveSupport's to_json extension is invoked, restoring ISO 8601 output (e.g. "2012-02-27T00:00:00.000Z") for Time and other ActiveSupport-enhanced types. Remove the multi_json runtime dependency from the gemspec as it is no longer used. Regression specs assert the exact ISO 8601 string rather than delegating the expectation to as_json on the same value, so a joint regression in both methods would still be caught.
1 parent a4e3fcd commit dddb863

4 files changed

Lines changed: 4 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#404](https://github.com/ruby-grape/grape-entity/pull/404): Drop `MultiJson` dependency, use `Hash#to_json` for ActiveSupport-aware serialization - [@numbata](https://github.com/numbata).
1011

1112
### 1.0.4 (2026-04-17)
1213

grape-entity.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Gem::Specification.new do |s|
2525
s.required_ruby_version = '>= 3.0'
2626

2727
s.add_dependency 'activesupport', '>= 3.0.0'
28-
s.add_dependency 'multi_json', '>= 1.0'
2928

3029
s.files = Dir['lib/**/*.rb', 'CHANGELOG.md', 'LICENSE', 'README.md']
3130
s.require_paths = ['lib']

lib/grape_entity/entity.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'multi_json'
4-
53
module Grape
64
# An Entity is a lightweight structure that allows you to easily
75
# represent data from your application in a consistent and abstracted
@@ -594,7 +592,7 @@ def is_defined_in_entity?(attribute)
594592

595593
def to_json(options = {})
596594
options = options.to_h if options&.respond_to?(:to_h)
597-
MultiJson.dump(serializable_hash(options))
595+
serializable_hash(options).to_json
598596
end
599597

600598
def to_xml(options = {})

spec/grape_entity/entity_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ class NoPathCharacterEntity < Grape::Entity
18861886
entity = fresh_class.new(model)
18871887
json = entity.to_json
18881888
parsed = JSON.parse(json)
1889-
expect(parsed['birthday']).to eq(attributes[:birthday].as_json)
1889+
expect(parsed['birthday']).to eq('2012-02-27T00:00:00.000Z')
18901890
end
18911891

18921892
it 'serializes Time values in nested exposures via as_json' do
@@ -1895,7 +1895,7 @@ class NoPathCharacterEntity < Grape::Entity
18951895
end
18961896
entity = fresh_class.new(model)
18971897
parsed = JSON.parse(entity.to_json)
1898-
expect(parsed['details']['birthday']).to eq(attributes[:birthday].as_json)
1898+
expect(parsed['details']['birthday']).to eq('2012-02-27T00:00:00.000Z')
18991899
end
19001900
end
19011901

0 commit comments

Comments
 (0)