Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion elasticsearch-api/lib/elasticsearch/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ module Elasticsearch
module API
include Elasticsearch::API::Actions

DEFAULT_SERIALIZER = MultiJson
# Default JSON serializer. multi_json >= 1.21 deprecates the MultiJson constant and
# dump/load in favor of MultiJSON, generate/parse; use the new API when available.
module DefaultSerializer
def self.dump(object, options = {})
defined?(::MultiJSON) ? ::MultiJSON.generate(object, options) : ::MultiJson.dump(object, options)
end

def self.load(string, options = {})
defined?(::MultiJSON) ? ::MultiJSON.parse(string, options) : ::MultiJson.load(string, options)
end
end

DEFAULT_SERIALIZER = DefaultSerializer

HTTP_GET = 'GET'.freeze
HTTP_HEAD = 'HEAD'.freeze
Expand Down
11 changes: 10 additions & 1 deletion elasticsearch-api/spec/unit/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
end

it 'has a default serializer' do
expect(Elasticsearch::API.serializer).to eq(MultiJson)
expect(Elasticsearch::API.serializer).to respond_to(:dump, :load)
end

it 'serializes and deserializes with the default serializer' do
serializer = Elasticsearch::API::DEFAULT_SERIALIZER
expect(serializer.load(serializer.dump({ 'a' => 1 }))).to eq({ 'a' => 1 })
end

it 'does not emit deprecation warnings' do
expect { Elasticsearch::API::DEFAULT_SERIALIZER.dump({ a: 1 }) }.not_to output.to_stderr
end

context 'when settings are changed' do
Expand Down