All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gems.multi_json-1.11.2.lib.multi_json.adapter.rb Maven / Gradle / Ivy

There is a newer version: 3.7.2
Show newest version
require 'singleton'
require 'multi_json/options'

module MultiJson
  class Adapter
    extend Options
    include Singleton
    class << self

      def defaults(action, value)
        metaclass = class << self; self; end

        metaclass.instance_eval do
          define_method("default_#{action}_options"){ value }
        end
      end

      def load(string, options={})
        raise self::ParseError if blank?(string)
        string = string.read if string.respond_to?(:read)
        instance.load(string, load_options(options).merge(MultiJson.load_options(options)).merge!(options))
      end

      def dump(object, options={})
        instance.dump(object, dump_options(options).merge(MultiJson.dump_options(options)).merge!(options))
      end

    private

      def blank?(input)
        input.nil? || /\A\s*\z/ === input
      rescue ArgumentError # invalid byte sequence in UTF-8
        false
      end

    end
  end
end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy