Module: TorqueBox::Codecs::JSON

Defined in:
lib/torquebox/codecs/json.rb

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/torquebox/codecs/json.rb', line 49

def decode(data)
  require_json
  begin
    ::JSON.parse( data, :symbolize_names => true ) unless data.nil?
  rescue ::JSON::ParserError
    ::JSON.load(data)
  end
end

.encode(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/torquebox/codecs/json.rb', line 37

def encode(data)
  require_json
  begin
    if ( data.respond_to?( :as_json ) )
      data = data.as_json
    end
    ::JSON.fast_generate( data ) unless data.nil?
  rescue ::JSON::GeneratorError
    ::JSON.dump(data)
  end
end

.require_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/torquebox/codecs/json.rb', line 25

def require_json
  # We can't ship our own json, as it may collide with the gem
  # requirement for the app.
  if !defined?( ::JSON )
    begin
      require 'json'
    rescue LoadError => ex
      raise RuntimeError.new( "Unable to load the json gem. Verify that is installed and in your Gemfile (if using Bundler)" )
    end
  end
end