Sha256: 73bcaa49f1550b32609df1a0f4473025f4dd4b460a6a7b83d18c7a829d5fb657

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'legion/json/version'
require 'legion/json/parse_error'

module Legion
  # Used to create JSON objects
  module JSON
    class << self
      # Set up the JSON parser. This method must be called before any
      # attempt to use the parser. This method is currently called at
      # the bottom of this file. The appropriate JSON parser will be
      # loaded for the current platform.
      #
      # @return [Object] parser.
      def setup!
        if RUBY_ENGINE == 'jruby'
          require 'jrjackson'
          require 'legion/json/jrjackson'
          @parser = Legion::JSON::JrJackson.new
        else
          require 'oj'
          Oj.default_options = { mode: :compat }
          require 'legion/json/oj'
          @parser = Legion::JSON::Oj.new
        end
      end

      # Load (and parse) a JSON string.
      #
      # @param string [String]
      # @return [Object]
      def load(string)
        @parser.load(string)
      rescue StandardError => error
        raise Legion::JSON::ParseError.build(error, string)
      end

      # Dump (generate) a JSON string from a Ruby object.
      #
      # @param object [Object]
      # @param options [Hash]
      def dump(object, options = {})
        @parser.dump(object, options)
      end
    end
  end
end

Legion::JSON.setup!

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
legion-json-0.1.1 lib/legion/json.rb