No description, website, or topics provided.
Latest commit d874fd9 Oct 22, 2015 @chancancode chancancode Merge pull request #5 from agis-/patch-1
Update installation instructions
Failed to load latest commit information.
gemfiles Make it work against 4.1.0 now that it's been released Apr 9, 2014
lib Rails master added back the big decimal options in 6e905e21 Dec 3, 2013
test Rails master added back the big decimal options in 6e905e21 Dec 3, 2013
.gitignore Initial commit Nov 24, 2013
MIT-LICENSE Initial commit Nov 24, 2013 Merge pull request #5 from agis-/patch-1 Oct 22, 2015
Rakefile Initial commit Nov 24, 2013

Active Support JSON Encoder

Build Status

A pure-Ruby ActiveSupport JSON encoder. This was the default encoder used by ActiveSupport prior to Rails 4.1. The main advantage of using this encoder over the new default is for the #encode_json support (see below).


Simply include this gem in your Gemfile:

gem 'activesupport-json_encoder'


By default, ActiveSupport encodes BigDecimal objects as a string:

{ big_number:'12345678901234567890') }.to_json # => "{\"big_number\":\"12345678901234567890.0\"}"

To change this, you can set ActiveSupport.encode_big_decimal_as_string to false:

ActiveSupport.encode_big_decimal_as_string = false
{ big_number:'12345678901234567890') }.to_json # => "{\"big_number\":12345678901234567890.0}"

Beware that you may lose precision on the consuming-end if you do this:

// Parsing this in JavaScript in the browser
JSON.parse("{\"big_number\":12345678901234567890.0}").big_number // => 12345678901234567000

JSON Serialization for Custom Objects

By default, when the encoder encounters a Ruby object that it does not recognize, it will serialize its instance variables:

class MyClass
  def initialize
    @foo = :bar
end # => "{\"foo\":\"bar\"}"

There are two ways to customize this behavior on a per-class basis. Typically, you should override #as_json to return a Ruby-representation of your object. Any options passed to #to_json will be made available to this method:

class MyClass
  def as_json(options = {})
    options[:as_array] ? [:foo, :bar] : {foo: :bar}
end # => "{\"foo\":\"bar\"}" true) # => "[\"foo\",\"bar\"]"

This method is supported by all encoders.

However, sometimes this might not give you enough control. For example, you might want to encode numeric values in a certain format. In this case, you can override the #encoder_json method. This method has access to the Encoder object and is expected to return a String that would be injected to the JSON output directly:

class Money
  def initialize(dollars, cents)
    @dollars = dollars
    @cents = cents

  def as_json(options = nil)
    # Opt-out from the default Object#as_json

  def encode_json(encoder)
    if @cents.to_i < 10

{ price:,10) }.to_json # => "{\"price\":0.10}"

Beware that this function is specific to this gem and is not supported by other encoders. You should also be extra careful to return valid JSON because the return value of this method will be injected into the output with no sanitization whatsoever. Use this method with extreme caution, especially when dealing with user input.


  • activesupport >= 4.1.0