module Jekyll::Algolia::Utils
def self.jsonify(item)
but will try to stringify other objects, excluding the one that contain
It will keep any string, number, boolean,boolean,array or nested object,
item - The object to convert
JSON, to be stored as a record
Public: Convert an object into an object that can easily be converted to
def self.jsonify(item) simple_types = [ NilClass, TrueClass, FalseClass, Integer, Float, String ] # Integer arrived in Ruby 2.4. Before that it was Fixnum and Bignum if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4.0') # rubocop:disable Lint/UnifiedInteger simple_types += [Fixnum, Bignum] # rubocop:enable Lint/UnifiedInteger end return item if simple_types.member?(item.class) # Recursive types return item.map { |value| jsonify(value) } if item.is_a?(Array) if item.is_a?(Hash) return item.map { |key, value| [key, jsonify(value)] }.to_h end # Can't be stringified, discard it return nil unless item.respond_to?(:to_s) # Discard also if is a serialized version with unique identifier stringified = item.to_s return nil if match?(stringified, /#<[^ ].*@[0-9]* .*>/) stringified end