class Rosetta::Deserializers::JSON

Public Instance Methods

call() click to toggle source
# File lib/rosetta/deserializers/json.rb, line 10
def call
  validate_input!
  input.map { |obj| Element.new(obj) }
end

Private Instance Methods

valid_json(json) click to toggle source

HACK: Feels dirty but there's no JSON soft-parsing in ruby's json lib

# File lib/rosetta/deserializers/json.rb, line 32
def valid_json(json)
  JSON(json)
#NOTE: Rescuing TypeError too in case json is not a String
rescue ::JSON::ParserError, TypeError
  nil
end
validate_input!() click to toggle source
# File lib/rosetta/deserializers/json.rb, line 17
        def validate_input!
          raise DeserializationError, <<-ERROR.strip unless parsed_input = valid_json(@input)
            JSON input is invalid
          ERROR
          raise DeserializationError, <<-ERROR.strip unless parsed_input.is_a? Array
            JSON input must be an array
          ERROR
          raise DeserializationError, <<-ERROR.strip unless parsed_input.all? { |o| o.is_a? Hash }
            JSON input must contain objects
          ERROR

          @input = parsed_input.freeze
        end