diff options
| author | Charlie Sharpsteen | 2011-11-10 12:21:35 -0800 |
|---|---|---|
| committer | Charlie Sharpsteen | 2011-11-13 12:14:55 -0800 |
| commit | 1d1cd374b38ef898d3dd64555052ade8edb74330 (patch) | |
| tree | 891a1f11feb518379a134333a1a00217b0148c9c /Library/Homebrew/vendor/multi_json.rb | |
| parent | cb681ffff68fad666a47ab0aca1338fa39a2e80c (diff) | |
| download | brew-1d1cd374b38ef898d3dd64555052ade8edb74330.tar.bz2 | |
Vendor Library: multi-json 1.0.3
Multi-JSON is a library that provides encode/decode support for casting Ruby
objects to JSON strings and back again. This version of the library has been
tested against ruby versions 1.8.6 and later.
Having a JSON encoder/decoder in the toolbox helps now that the GitHub API only
returns results in JSON format.
Diffstat (limited to 'Library/Homebrew/vendor/multi_json.rb')
| -rw-r--r-- | Library/Homebrew/vendor/multi_json.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Library/Homebrew/vendor/multi_json.rb b/Library/Homebrew/vendor/multi_json.rb new file mode 100644 index 000000000..201265e70 --- /dev/null +++ b/Library/Homebrew/vendor/multi_json.rb @@ -0,0 +1,74 @@ +module MultiJson + class DecodeError < StandardError; end + module_function + + @engine = nil + + # Get the current engine class. + def engine + return @engine if @engine + self.engine = self.default_engine + @engine + end + + REQUIREMENT_MAP = [ + ["yajl", :yajl], + ["json", :json_gem], + ["json/pure", :json_pure] + ] + + # The default engine based on what you currently + # have loaded and installed. First checks to see + # if any engines are already loaded, then checks + # to see which are installed if none are loaded. + def default_engine + return :yajl if defined?(::Yajl) + return :json_gem if defined?(::JSON) + + REQUIREMENT_MAP.each do |(library, engine)| + begin + require library + return engine + rescue LoadError + next + end + end + + :ok_json + end + + # Set the JSON parser utilizing a symbol, string, or class. + # Supported by default are: + # + # * <tt>:json_gem</tt> + # * <tt>:json_pure</tt> + # * <tt>:ok_json</tt> + # * <tt>:yajl</tt> + def engine=(new_engine) + case new_engine + when String, Symbol + require "multi_json/engines/#{new_engine}" + @engine = MultiJson::Engines.const_get("#{new_engine.to_s.split('_').map{|s| s.capitalize}.join('')}") + when Class + @engine = new_engine + else + raise "Did not recognize your engine specification. Please specify either a symbol or a class." + end + end + + # Decode a JSON string into Ruby. + # + # <b>Options</b> + # + # <tt>:symbolize_keys</tt> :: If true, will use symbols instead of strings for the keys. + def decode(string, options = {}) + engine.decode(string, options) + rescue engine::ParseError => exception + raise DecodeError, exception.message, exception.backtrace + end + + # Encodes a Ruby object as JSON. + def encode(object) + engine.encode(object) + end +end |
