GithubHelp home page GithubHelp logo

jsonify-rails's Introduction

Jsonify-Rails — Jsonify templates for Rails views Build Status

Jsonify is to JSON as Builder is to XML.

Installation

gem install jsonify-rails

Usage

Jsonify-Rails provides a Rails 3 template handler allowing you to create view templates using Jsonify. Any template with a .jsonify will be handled by Rails.

The Jsonify template handler exposes a Jsonify::Builder instance to your template with the json variable:

json.hello do
  json.world "Jsonify is Working!"
end

Just like with any other template, your Jsonify template will have access to any instance variables that are exposed through the controller.

For optimum performance, Jsonify outputs the generated JSON string in plain format -- no newlines, or spaces -- this is best for production. However, if you want "pretty" output, you can configure Jsonify by adding the following configuration line to application.rb.

config.jsonify_format = :pretty

Partials

You can use partials from Jsonify views, and you can create Jsonify partials. How your Jsonify template uses a partial depends on how the information the partial returns is structured. Keep in mind that any paritial, be it a Jsonify template, erb, or anything else, always a returns its result as a string.

Jsonify partials

Any Jsonify partial — that is, the file has a .jsonify extension — will return, by design, a string that is valid JSON. It will represent either a JSON object,wrapped in curly braces ( {} ), or a JSON array, wrapped in square brackets ( [] ).

To incorporate such a value into a Jsonify template, use the ingest! method.

ingest! assumes that the value it receives is valid JSON representation. It parses the JSON into a Jsonify object graph, and then adds it to the current Jsonify builder.

Let's assume this this is your main template, index.jsonify:

json << 1
json.ingest! (render :partial=>'my_partial')

From the first line, you can tell that an array will be created as this line uses the append operator. On the second line, a partial is being added to the builder. Note that you cannot simply place render :partial ... on a line by itself as you can do with other templates like erb and haml. You have to explicitly tell Jsonify to add it to the builder.

Let's say that the partial file, _my_partial.jsonify, is as follows:

json << 3
json << 4

This json variable in this partial is a separate distinct Jsonify::Builder instance from the json variable in the main template.

Note: Figure out if a the json instance can be passed to the Jsonify partial. It would make things easier and we wouldn't have to ingest the result.

This partial results in the following string:

"[3,4]"

The ingest! method will actually parse this string back into a Jsonify-based object, and adds it to the builder's current state. The resulting output will be:

"[1,[3,4]]"
Other partials

You can also use output from non-Jsonify templates (e.g. erb); just remember that the output from a template is always a string and that you have to tell the builder how to include the result of the partial.

For example, suppose you have the partial _today.erb with the following content:

<%= Date.today %>

You can then incorporate this partial into your Jsonify template just as you would any other string value:

json << 1
json << {:date => (render :partial => 'today')}

renders ...

[1,{"date":"2011-07-30"}]

Related Projects

License

This project is released under the MIT license.

Authors

Contributors

jsonify-rails's People

Contributors

arikfr avatar bsiggelkow avatar chaslemley avatar laribee avatar markbates avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jsonify-rails's Issues

No pretty printing?

Hi - I've followed your instructions about getting pretty printing in a Rails 3.1 app working to no avail. I'm including my application.rb here:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module MyApp
  class Application < Rails::Application
    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Pretty-print JSON output
    config.jsonify_format = :pretty

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
  end
end

The output all runs on a single line. Am I missing something obvious, like a 'require' statement?

Render jsonify templates within controller context

similar to haml, my templates have access to certain controller/helper methods instead of solely accessing instance variables.

a great example of this is when integrating with Devise authentication and accessing the current_user method from within my templates.

how difficult is it to expose these same methods within jsonify templates?

Rendering a partial within a loop

Hi,
I'm trying to render a partial multiple times (for the index action) and it's only returning the last item:

Partial _child.json.jsonify

json.name child.name

File index.json.jsonify

@children.each do |child|
  json.ingest!(render partial: child)
end

Actual result:

{"name":"Pedrito"}

Expected result:

[{"name":"Horacio"},{"name":"Pepito"},{"name":"Pedrito"}]

I also tried:

@children.each do |child|
  json << json.ingest!(render partial: child)
end

But the result was:

{"name":"Pedrito","#<Jsonify::JsonObject:0x00000002333ef0>":null,"[#<Jsonify::JsonPair:0x00000002320df0 @key=\"name\", @value=\"Pepito\">]":null,"[#<Jsonify::JsonPair:0x00000002306658 @key=\"name\", @value=\"Pedrito\">]":null}

Stripping out whitespace in strings

I'm having some problems using strings in jsonify. I've been using jsonify fine for a year with no problems. Now I've across one particular method that seems to strip out all the whitespace in strings.

For example this:

json.test 'hello world'

Produces:

{
"test": "helloworld"
}

I eventually narrowed the problem down to sending the json with a HTTP status code of 403. If I send with a 200 status code it works fine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.