foca / granola Goto Github PK
View Code? Open in Web Editor NEWSimple JSON serializers (Cereal-izers. GET IT?)
License: MIT License
Simple JSON serializers (Cereal-izers. GET IT?)
License: MIT License
It would be nice to have some mechanism for caching the generated JSON responses, instead of only providing ETag
/ Last-Modified
support.
Probably a good candidate for a plugin, as it's somewhat orthogonal to the serialization itself.
If your app relies on MultiJson
, it's pretty easy to plug in with the new API for defining renderers.
However, if your app does not rely on MultiJson, but indirectly requires it at some point by depending on one of the many (roughly ~2000 at the time of writing), we will switch to using it instead of the JSON
library.
This incurs a heavy performance penalty. The best solution IMO is drop the automatic switching, and allowing people to explicitly set they desire to render via MultiJson
where appropriate.
Similar as to how we have a rack helper, it would be nice to have hooks into Rails' rendering so we could do something along the lines of
render json: something, with: SomeSerializer
In the past I've had gem conflicts because of MultiJson, and, in my opinion, it's not worth to have a dependency on something that is merely a strategy definition.
So instead of depending on it and calling MultiJson.dump
(or whatever its API is), you could use something like:
Granola.json = {
parse: -> str { JSON.parse(str) },
dump: -> o { o.to_json }
}
What do you think?
$ ruby -v
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
$ gem -v
2.4.5
$ gem fetch granola
Fetching: granola-0.10.0.gem (100%)
Downloaded granola-0.10.0
$ gem spec granola-0.10.0.gem files
---
- LICENSE
- README.md
- lib/granola.rb
- lib/granola/caching.rb
- lib/granola/rack.rb
- lib/granola/version.rb
Attempt to require the gem result in error:
$ ruby -rgranola -e "puts :ok"
/usr/local/rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- granola/serializer (LoadError)
Seems the file is missing form the gemspec definition.
Thank you!
Right now Utils.serializer_class_for
will only discover serializer classes that are defined at the top-level namespace.
It would be nice to have a hook to allow users to define their own constant lookup, so that users that have serializers defined in namespaces (e.g. API::V2::UserSerializer
) can use the auto discovery.
Something along the lines of:
Granola::Util.constant_lookup #=> Object.method(:const_get)
Granola::Util.constant_lookup = API::V2.method(:const_get)
Should be enough.
When calling the rack helper, if a user doesn't pass an explicit value for as:
we should search through the registered renderers for one whose content_type
matches the request's Accept
header.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.