Sha256: 68514c52b7631748039f01d3ef23310bb150c7d6d5a21c57c526f1f4bcb8276b

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

unless RUBY_PLATFORM =~ /java/
  puts "This library is only compatible with a java-based ruby environment like JRuby."
  exit 255
end

require_relative "jars/jrjackson-1.2.3.jar"

require 'com/jrjackson/jr_jackson'

module JrJackson
  module Json
      class << self
      TIME_REGEX = %r(\A(\d{4}-\d\d-\d\d|(\w{3}\s){2}\d\d)\s\d\d:\d\d:\d\d)

      def load(json_string, options = nil)
        if json_string.is_a?(String) && json_string =~ TIME_REGEX
          return JrJackson::Raw.parse_raw("\"#{json_string}\"")
        end

        if options && !options.empty?
          if options.size == 1 && !!options[:raw]
            return JrJackson::Raw.parse_raw(json_string)
          end
          if options.size == 1 && !!options[:symbolize_keys]
            return JrJackson::Raw.parse_sym(json_string)
          end
          JrJackson::Raw.parse(json_string, options)
        else
          JrJackson::Raw.parse_str(json_string)
        end
      end

      def dump(object)
        case object
        when Array, Hash, String
          JrJackson::Raw.generate(object)
        when true, false
          object.to_s
        when nil
          'null'
        else
          if object.respond_to?(:to_json)
            object.to_json
          elsif object.respond_to?(:to_s)
            object.to_s
          else
            object
          end
        end
      end

      alias :parse :load
      alias :generate :dump
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jrjackson-0.2.2 lib/jrjackson/jrjackson.rb
jrjackson-0.2.1 lib/jrjackson/jrjackson.rb