diff --git a/elasticsearch-api/lib/elasticsearch/api.rb b/elasticsearch-api/lib/elasticsearch/api.rb index 7504667e3..7d5913da7 100644 --- a/elasticsearch-api/lib/elasticsearch/api.rb +++ b/elasticsearch-api/lib/elasticsearch/api.rb @@ -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 diff --git a/elasticsearch-api/spec/unit/api_spec.rb b/elasticsearch-api/spec/unit/api_spec.rb index 06aebc88c..a61f62ac6 100644 --- a/elasticsearch-api/spec/unit/api_spec.rb +++ b/elasticsearch-api/spec/unit/api_spec.rb @@ -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