jsonapi-rb / jsonapi-renderer

Efficiently render JSON API documents.
http://jsonapi-rb.org
MIT License
27 stars 11 forks source link

Unify Simple and Cached resource output – both returns array of hash objects #35

Open ViliusLuneckas opened 5 years ago

ViliusLuneckas commented 5 years ago

This change unifies the output type of SimpleResourcesProcessor and CachedResourcesProcessor.

Before:

# No caching
result = JSONAPI::Serializable::Renderer.new(cache: false).render(collection)
result[:data].first.class # => Hash

# With caching
result = JSONAPI::Serializable::Renderer.new(cache: Rails.cache).render(collection)
result[:data].first.class # => String

# Parsing needed if you want to personalize result
result[:data] = json[:data].map { |json_string| JSON.parse(json_string) }

After:

# No caching
result = JSONAPI::Serializable::Renderer.new(cache: false).render(collection)
result[:data].first.class # => Hash

# With caching
result = JSONAPI::Serializable::Renderer.new(cache: Rails.cache).render(collection)
result[:data].first.class # => Hash

That's very important if you need to amend/personalize output of the JSONAPI::Serializable::Renderer.

beauby commented 5 years ago

The kind of caching implemented here is not attributes caching but fragment caching, as the main bottleneck usually is generating the actual json. Attributes caching is quite easy to implement and could be a distinct feature.