Class: Praxis::Handlers::JSON
- Inherits:
-
Object
- Object
- Praxis::Handlers::JSON
- Defined in:
- lib/praxis/handlers/json.rb
Instance Method Summary collapse
-
#generate(structured_data) ⇒ String
Generate a pretty-printed JSON document from structured data.
-
#initialize ⇒ JSON
constructor
Construct a JSON handler and initialize any related libraries.
-
#parse(document) ⇒ Hash, Array
Parse a JSON document into structured data.
Constructor Details
#initialize ⇒ JSON
Construct a JSON handler and initialize any related libraries.
7 8 9 10 11 12 13 |
# File 'lib/praxis/handlers/json.rb', line 7 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 |
Instance Method Details
#generate(structured_data) ⇒ String
Generate a pretty-printed JSON document from structured data.
29 30 31 |
# File 'lib/praxis/handlers/json.rb', line 29 def generate(structured_data) ::JSON.pretty_generate(structured_data) end |
#parse(document) ⇒ Hash, Array
Parse a JSON document into structured data.
19 20 21 22 23 |
# File 'lib/praxis/handlers/json.rb', line 19 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 |