Sha256: b0d1d6d17bf07e572ff6f8a38886143db909856bc493c6320183942915cd2933

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Praxis
  module Handlers
    class JSON
      # Construct a JSON handler and initialize any related libraries.
      #
      # @raise [Praxis::Exceptions::InvalidConfiguration] if the handler is unsupported
      def initialize
        require 'json'
      rescue LoadError
        # Should never happen since JSON is a default gem; might as well be cautious!
        raise Praxis::Exceptions::InvalidConfiguration,
              'JSON handler depends on json ~> 1.0; please add it to your Gemfile'
      end

      # Parse a JSON document into structured data.
      #
      # @param [String] document
      # @return [Hash,Array] the structured-data representation of the document
      def parse(document)
        # Try to be nice and accept an empty string as an empty payload (seems nice to do for dumb http clients)
        return nil if document.nil? || document == ''

        ::JSON.parse(document)
      end

      # Generate a pretty-printed JSON document from structured data.
      #
      # @param [Hash,Array] structured_data
      # @return [String]
      def generate(structured_data)
        ::JSON.pretty_generate(structured_data)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
praxis-2.0.pre.24 lib/praxis/handlers/json.rb
praxis-2.0.pre.23 lib/praxis/handlers/json.rb
praxis-2.0.pre.22 lib/praxis/handlers/json.rb
praxis-2.0.pre.21 lib/praxis/handlers/json.rb
praxis-2.0.pre.20 lib/praxis/handlers/json.rb
praxis-2.0.pre.19 lib/praxis/handlers/json.rb