Skip to content

Represented objects via extend are not serializable #3

@dblock

Description

@dblock

If you put present instance, with: Presenter inside a cache block, it cannot be serialized. That is because its implementation looks like this:

def represent(object, options = {})
    object.extend self
    object
end

The objects gets serialized, but not its class definition with includes the extended methods.

A solution is to roll out a presenter that looks like this:

module Grape
  module Roar
    module Representer
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def represent(object, options = {})
          object.extend self
          object.as_json(options) # usually cannot be `to_json` because Grape formatter would re-encode this
        end
      end
    end
  end
end

Maybe this should just be a class called Grape::Roar::Representer::JSON?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions