Method: Sequel::Plugins::JsonSerializer.object_to_json_data

Defined in:
lib/sequel/plugins/json_serializer.rb

.object_to_json_data(obj, *args, &block) ⇒ Object

Convert the given object to a JSON data structure using the given arguments.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sequel/plugins/json_serializer.rb', line 145

def self.object_to_json_data(obj, *args, &block)
  if obj.is_a?(Array)
    obj.map{|x| object_to_json_data(x, *args, &block)}
  else
    if obj.respond_to?(:to_json_data)
      obj.to_json_data(*args, &block)
    else
      begin
        Sequel.parse_json(Sequel.object_to_json(obj, *args, &block))
      # :nocov:
      rescue Sequel.json_parser_error_class
        # Support for old Ruby code that only supports parsing JSON object/array
        Sequel.parse_json(Sequel.object_to_json([obj], *args, &block))[0]
      # :nocov:
      end
    end
  end
end