Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize to vanilla JSON using the same serializer class #59

Closed
abargnesi opened this issue Feb 4, 2015 · 1 comment
Closed

Serialize to vanilla JSON using the same serializer class #59

abargnesi opened this issue Feb 4, 2015 · 1 comment

Comments

@abargnesi
Copy link
Contributor

I am attempting to define single Serializer class to handle resources for application/json and application/hal+json.

Currently I have an Evidence resource defined using two serializers:

class EvidenceCollectionJsonSerializer < BaseSerializer                         
  adapter Oat::Adapters::HAL

  schema do                                                                     
    type :'evidence-collection'                                                 

    properties do |p|                                                           
      collection :evidence, item, EvidenceJsonSerializer
      p.facets   context[:facets]
    end
  end
end

class EvidenceCollectionHALSerializer < BaseSerializer
  adapter Oat::Adapters::HAL

  schema do
    type :'evidence-collection'

    properties do |p|
      collection :evidence, item, EvidenceHALSerializer
      p.facets   context[:facets]
    end

    link :self,       link_self
    link :start,      link_start
    link :next,       link_next
  end
end

I have defined the application/json serializer with Oat::Adapters::HAL without including any links. This works accept the :evidence collection is wrapped in a HAL _embedded property.

Is there a way to represent vanilla JSON when the client does not accept hypermedia formats?

An adapter like this gist does the job.

abargnesi added a commit to OpenBEL/openbel-api that referenced this issue Feb 4, 2015
provides a BasicJson Oat::Serializer within the tree

opened issue with the oat gem:
- ismasan/oat#59

closes #19
@ismasan
Copy link
Owner

ismasan commented Feb 4, 2015

I think your approach works. You define a special adapter with no-op methods so it's compatible with all other adapters you're using.

How do content negotiation and swap adapters is up to you and the web framework you'll be using. After deciding which adapter you want to use you can pass it to the serializers on a per-request basis. See https://github.com/ismasan/oat#switching-adapters-dynamically

Regards

@ismasan ismasan closed this as completed Feb 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants