Module: TorqueBox::Codecs::JSON

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

Class Method Summary (collapse)

Class Method Details

+ (Object) decode(data)



49
50
51
52
53
54
55
56
# File 'core/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

+ (Object) encode(data)



37
38
39
40
41
42
43
44
45
46
47
# File 'core/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

+ (Object) require_json



25
26
27
28
29
30
31
32
33
34
35
# File 'core/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