All Projects → foca → Granola

foca / Granola

Licence: mit
Simple JSON serializers (Cereal-izers. GET IT?)

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Granola

JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (-33.33%)
Mutual labels:  json-serialization
Pyjfuzz
PyJFuzz - Python JSON Fuzzer
Stars: ✭ 342 (+776.92%)
Mutual labels:  json-serialization
Json
JSON for Modern C++
Stars: ✭ 27,824 (+71243.59%)
Mutual labels:  json-serialization
kson
A Java serialization/deserialization library to convert Java Objects into json and back, faster and powerful then Gson.
Stars: ✭ 25 (-35.9%)
Mutual labels:  json-serialization
Easyjson
Fast JSON serializer for golang.
Stars: ✭ 3,512 (+8905.13%)
Mutual labels:  json-serialization
Jsoncons
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
Stars: ✭ 400 (+925.64%)
Mutual labels:  json-serialization
libstud-json
JSON pull-parser/push-serializer library for C++
Stars: ✭ 20 (-48.72%)
Mutual labels:  json-serialization
Jsondoc
JSON object for Delphi based on IUnknown and Variant
Stars: ✭ 20 (-48.72%)
Mutual labels:  json-serialization
Metastuff
Code I use in my game for all serialization/deserialization/introspection stuff
Stars: ✭ 337 (+764.1%)
Mutual labels:  json-serialization
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+61430.77%)
Mutual labels:  json-serialization
Panko serializer
High Performance JSON Serialization for ActiveRecord & Ruby Objects
Stars: ✭ 266 (+582.05%)
Mutual labels:  json-serialization
Surging
Surging is a micro-service engine that provides a lightweight, high-performance, modular RPC request pipeline. The service engine supports http, TCP, WS,Grpc, Thrift,Mqtt, UDP, and DNS protocols. It uses ZooKeeper and Consul as a registry, and integrates it. Hash, random, polling, Fair Polling as a load balancing algorithm, built-in service gove…
Stars: ✭ 3,088 (+7817.95%)
Mutual labels:  json-serialization
Python Rapidjson
Python wrapper around rapidjson
Stars: ✭ 417 (+969.23%)
Mutual labels:  json-serialization
JSON
Simple and easy JSON parser, JSON generator, and data holder based on JSONArray and JSONObject for android
Stars: ✭ 78 (+100%)
Mutual labels:  json-serialization
Fasteasymapping
A tool for fast serializing & deserializing of JSON
Stars: ✭ 556 (+1325.64%)
Mutual labels:  json-serialization
Coronavirus-COVID-19-Tracker
Mapping 2019-nCoV (Official source): https://systems.jhu.edu/research/public-health/ncov/
Stars: ✭ 25 (-35.9%)
Mutual labels:  json-serialization
Restc Cpp
Modern C++ REST Client library
Stars: ✭ 371 (+851.28%)
Mutual labels:  json-serialization
Eminim
JSON serialization framework for Nim, works from a Stream directly to any type and back. Depends only on stdlib.
Stars: ✭ 32 (-17.95%)
Mutual labels:  json-serialization
Spray Json
A lightweight, clean and simple JSON implementation in Scala
Stars: ✭ 917 (+2251.28%)
Mutual labels:  json-serialization
Element Api
Create a JSON API/Feed for your elements in Craft.
Stars: ✭ 493 (+1164.1%)
Mutual labels:  json-serialization

Granola, a JSON serializer Build Status RubyGem

A tasty bowl of Granola

Granola aims to provide a simple interface to generate JSON responses based on your application's domain models. It doesn't make assumptions about anything and gets out of your way. You just write plain ruby.

Example

class PersonSerializer < Granola::Serializer
  def data
    {
      "name" => object.name,
      "email" => object.email,
      "age" => object.age
    }
  end
end

PersonSerializer.new(person).to_json #=> '{"name":"John Doe",...}'

Install

gem install granola

JSON serialization

Granola doesn't make assumptions about your code, so it shouldn't depend on a specific JSON backend. It defaults to the native JSON backend, but you're free to change it. For example, if you were using Oj:

Granola.render :json, via: Oj.method(:dump),
                      content_type: "application/json"

Handling lists of entities

A Granola serializer can handle a list of entities of the same type by using the Serializer.list method (instead of Serializer.new). For example:

serializer = PersonSerializer.list(Person.all)
serializer.to_json #=> '[{"name":"John Doe",...},{...}]'

Rack Helpers

If your application is based on Rack, you can require "granola/rack" instead of require "granola", and then simply include Granola::Rack to get access to the following interface:

granola(person) #=> This will infer PersonSerializer from a Person instance
granola(person, with: AnotherSerializer)

This method returns a Rack response tuple that you can use like so (this example uses Cuba, but similar code will work for other frameworks):

require "granola/rack"

Cuba.plugin Granola::Rack

Cuba.define do
  on get, "users/:id" do |id|
    user = User[id]
    halt granola(user)
  end
end

Rails Support

The companion Granola::Rails gem takes care of support for Rails.

HTTP Caching

Granola::Serializer gives you two methods that you can implement in your serializers: last_modified and cache_key.

When using the Granola::Rack module, you should return a Time object from your serializer's last_modified. Granola will use this to generate the appropriate Last-Modified HTTP header. Likewise, the result of cache_key will be MD5d and set as the response's ETag header.

If you do this, you should also make sure that the Rack::ConditionalGet middleware is in your Rack stack, as it will use these headers to avoid generating the JSON response altogether. For example, using Cuba:

class UserSerializer < Granola::Serializer
  def data
    { "id" => object.id, "name" => object.name, "email" => object.email }
  end

  def last_modified
    object.updated_at
  end

  def cache_key
    "user:#{object.id}:#{object.updated_at.to_i}"
  end
end

Cuba.plugin Granola::Rack
Cuba.use Rack::ConditionalGet

Cuba.define do
  on get, "users/:id" do |id|
    halt granola(User[id])
  end
end

This will avoid generating the JSON response altogether if the user sends the appropriate If-Modified-Since or If-None-Match headers.

Caching of serialized bodies

If you are generating responses that are particularly expensive to serialize, you can use the Granola::Cache gem to store their representations once generated in an external cache.

Different Formats

Although Granola out of the box only ships with JSON serialization support, it's easy to extend and add support for different types of serialization in case your API needs to provide multiple formats. For example, in order to add MsgPack support (via the msgpack-ruby library), you'd do this:

require "msgpack"

Granola.render :msgpack, via: MessagePack.method(:pack),
                         content_type: "application/x-msgpack"

Now all serializers can be serialized into MsgPack using a to_msgpack method. In order to use this from our Rack helpers, you'd do:

granola(object, as: :msgpack)

This will set the correct MIME type.

If you don't explicitly set a format when rendering via the rack helper, Granola will use the request's Accept header to choose the best format for rendering.

For example, given a request with the following header:

Accept: text/x-yaml;q=0.5,application/x-msgpack;q=0.8,*/*;q=0.2

Granola will check first if you have a renderer registered for the application/x-msgpack MIME type, and then check for a renderer for the text/x-yaml MIME type. If none of these are registered, it will default to rendering JSON.

License

This project is shared under the MIT license. See the attached LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].