Sha256: b340081601a720d2d2037551c5076e1b2e9e268457631a5b0bcbabd9197ffdf4

Contents?: true

Size: 1.16 KB

Versions: 16

Compression:

Stored size: 1.16 KB

Contents

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

16 entries across 16 versions & 1 rubygems

Version Path
praxis-2.0.0 lib/praxis/handlers/json.rb
praxis-2.0.pre.40 lib/praxis/handlers/json.rb
praxis-2.0.pre.39 lib/praxis/handlers/json.rb
praxis-2.0.pre.18 lib/praxis/handlers/json.rb
praxis-2.0.pre.17 lib/praxis/handlers/json.rb
praxis-2.0.pre.16 lib/praxis/handlers/json.rb
praxis-2.0.pre.15 lib/praxis/handlers/json.rb
praxis-2.0.pre.14 lib/praxis/handlers/json.rb
praxis-2.0.pre.13 lib/praxis/handlers/json.rb
praxis-2.0.pre.12 lib/praxis/handlers/json.rb
praxis-2.0.pre.11 lib/praxis/handlers/json.rb
praxis-2.0.pre.10 lib/praxis/handlers/json.rb
praxis-2.0.pre.9 lib/praxis/handlers/json.rb
praxis-2.0.pre.8 lib/praxis/handlers/json.rb
praxis-2.0.pre.7 lib/praxis/handlers/json.rb
praxis-2.0.pre.6 lib/praxis/handlers/json.rb