Sha256: db5fb61828fa16d5dae3997a9039ee5a1bf8e602a34f95d7478615d05d97e3dd

Contents?: true

Size: 952 Bytes

Versions: 3

Compression:

Stored size: 952 Bytes

Contents

# encoding: UTF-8

require 'oj'

Oj.default_options = {
  mode: :compat
} if defined?(Oj)

module Spontaneous
  module JSON
    module OjParser
      def parse(json_string)
        ::Oj.load(json_string, symbol_keys: true)
      rescue
        nil
      end

      def encode(object)
        ::Oj.dump(object)
      end
    end

    if defined?(Yajl)
      module YajlParser
        def parser
          Yajl::Parser.new(:symbolize_keys => true)
        end
        def encoder
          Yajl::Encoder.new
        end

        def parse(json_string)
          parser.parse(json_string)
        rescue
          nil
        end

        def encode(object)
          encoder.encode(object)
        end
      end
    end

    extend OjParser

    def parse_json(json_string)
      Spontaneous::JSON.parse(json_string)
    end
    alias_method :decode_json, :parse_json

    def encode_json(object)
      Spontaneous::JSON.encode(object)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha6 lib/spontaneous/json.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/json.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/json.rb