Sha256: f0795ad77944f0e7bfd6ec100d73c2c38168900d88dd71abbf5e97a49c40d839

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

# when testing across Ruby versions, we found that JSON string creation inconsistently ordered keys
# which is a problem because our mock testing service ultimately matches strings to see if requests are mocked
# this fix solves that problem by ensuring all hashes are created with a consistent key order every time
module MultiJson
  self.engine = :ok_json

  class << self
    def encode_with_ordering(object)
      # if it's a hash, recreate it with k/v pairs inserted in sorted-by-key order
      # (for some reason, REE fails if we don't assign the ternary result as a local variable
      # separately from calling encode_original)
      encode_original(sort_object(object))
    end

    alias_method :encode_original, :encode
    alias_method :encode, :encode_with_ordering
  
    def decode_with_ordering(string)
      sort_object(decode_original(string))
    end

    alias_method :decode_original, :decode
    alias_method :decode, :decode_with_ordering
    
    private 
  
    def sort_object(object)
      if object.is_a?(Hash)
        sort_hash(object)
      elsif object.is_a?(Array)
        object.collect {|item| item.is_a?(Hash) ? sort_hash(item) : item}
      else
        object
      end
    end
  
    def sort_hash(unsorted_hash)
      sorted_hash = KoalaTest::OrderedHash.new(sorted_hash)
      unsorted_hash.keys.sort {|a, b| a.to_s <=> b.to_s}.inject(sorted_hash) {|hash, k| hash[k] = unsorted_hash[k]; hash}
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
koala-1.4.0 spec/support/json_testing_fix.rb
koala-1.4.0.rc1 spec/support/json_testing_fix.rb
koala-1.3.0 spec/support/json_testing_fix.rb
koala-1.3.0rc2 spec/support/json_testing_fix.rb
koala-1.3.0rc1 spec/support/json_testing_fix.rb
koala-1.2.1 spec/support/json_testing_fix.rb
koala-1.2.0 spec/support/json_testing_fix.rb
koala-1.2.0beta4 spec/support/json_testing_fix.rb
koala-1.2.0beta3 spec/support/json_testing_fix.rb
koala-1.2.0beta2 spec/support/json_testing_fix.rb
koala-1.2.0beta1 spec/support/json_testing_fix.rb
tyler_koala-1.2.0beta spec/support/json_testing_fix.rb