Skip to main content

Mobomo webinars-now on demand! | learn more.

JSON has become ubiquitous. From Facebook and Twitter both declaring it to be the preferred (and in some cases only) option for API access to the new OAuth 2.0 draft spec declaring that JSON is the only acceptable response format for OAuth token responses, JSON is here to stay. What isn’t ubiquitous, however, are people’s preferred implementations.

As library authors it is our duty to try to support as large a part of the community as possible and do so in a friendly manner. To that end, today we’re releasing MultiJSON, a simple library that allows you to seamlessly provide multiple JSON backends for your library with intelligent defaulting. Install with a simple gem install multi_json and then get started like so:

require 'multi_json'  # Decode using default engine. MultiJson.decode('{"abc":"def"}) # => {"abc" => "def"}  # Set an engine using a symbol. MultiJson.engine = :active_support  # Encode using ActiveSupport MultiJson.encode({:abc => "def"}) # => '{"abc":"def"}'

This gem is primarily for library authors, allowing you to use the best JSON available on the users’ systems without explicitly requiring one library over another. This way you can be sure that your JSON handling will work across implementations (e.g. JRuby) as well as requiring as little extra code as possible (the gem detects existing libraries before requiring more by default).

Engines supported by default are:

  • yajl-ruby
  • json (gem)
  • active_support
  • json_pure

We hope that this will make development of JSON-relying libraries a little bit less of a headache for library authors and users alike. The code is, as always, available on GitHub.

Categories
Author

If you’ve taken a look at Mustache, the “stupid in a good way” templating engine, you might know that there are also Javascript Mustache renderers such as Mustache.js. Today we’ve released a small library called mustache_json that allows you to compile your Mustache view objects into JSON, allowing them to be interpreted by Javascript Mustache rendering engines.

What this means for your project is that you will finally have a identical client-side and server-side rendering interface, opening wide the opportunities for pushing more of the rendering work onto the client-side, a boon for many real-time and heavy-interaction applications.

To install mustache_json, just get the gem from Gemcutter:

gem install mustache_json

To use it, simply require 'mustache_json' and all of your Mustache objects will automatically be given a #to_json method. For instance:

require 'mustache_json'  class Person < Mustache   def initialize(first_name, last_name)     context[:first_name], context[:last_name] = first_name, last_name   end      def initials     "#{context[:first_name][0..0]}.#{context[:last_name][0..0]}."   end      def listing     "#{context[:last_name]}, #{context[:first_name]}"   end end  bob = Person.new('Bob', 'Bobson') bob.to_json

This will render into a JSON object that looks like this:

{"last_name":"Bobson","initials":"B.B.","listing":"Bobson, Bob","first_name":"Bob"}

Mustache JSON gives you access to all of the public instance methods you declare in your Mustache as well as any context you have set. It is essentially a fully compiled version of the Mustache view, providing everything another renderer needs to create the actual markup. The JSON back-end for this library is swappable, meaning you can use the JSON gem, JSON pure, ActiveSupport, or Yajl by default (and any other class with an encode method if you’ve got a different library).

Documentation is available on RDoc.info and the source is available on GitHub. Stay tuned for posts in the future about utilizing this library to actually perform identical rendering in Ruby and Javascript.

Categories
Author
1
Subscribe to Json