Module: Allure::JsonHelper
- Included in:
- FileWriter, TestPlan
- Defined in:
- lib/allure_ruby_commons/_json_helper.rb
Overview
Json helper methods
Constant Summary collapse
- OJ_OPTIONS =
Returns Oj json options.
{ mode: :custom, use_to_hash: true, ascii_only: true }.freeze
Instance Method Summary collapse
-
#dump_json(obj) ⇒ String
Dump object to json using Oj or JSON.
-
#json_parse_error ⇒ Class
Json parse error class.
-
#load_json(json) ⇒ Hash
Load json from file using Oj or JSON.
Instance Method Details
#dump_json(obj) ⇒ String
Dump object to json using Oj or JSON
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/allure_ruby_commons/_json_helper.rb', line 17 module JsonHelper # @return [Hash] Oj json options OJ_OPTIONS = { mode: :custom, use_to_hash: true, ascii_only: true }.freeze begin require "oj" define_method(:dump_json) do |obj| Oj.dump(obj, OJ_OPTIONS) end define_method(:load_json) do |json| Oj.load_file(json, symbol_keys: true) rescue Oj::ParseError Allure.configuration.logger.error("Failed to parse json: #{json}") nil end rescue LoadError require "json" define_method(:dump_json) do |obj| JSON.generate(obj) end define_method(:load_json) do |json| JSON.parse(File.read(json), symbolize_names: true) rescue JSON::ParserError Allure.configuration.logger.error("Failed to parse json: #{json}") nil end end end |
#json_parse_error ⇒ Class
Json parse error class
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/allure_ruby_commons/_json_helper.rb', line 17 module JsonHelper # @return [Hash] Oj json options OJ_OPTIONS = { mode: :custom, use_to_hash: true, ascii_only: true }.freeze begin require "oj" define_method(:dump_json) do |obj| Oj.dump(obj, OJ_OPTIONS) end define_method(:load_json) do |json| Oj.load_file(json, symbol_keys: true) rescue Oj::ParseError Allure.configuration.logger.error("Failed to parse json: #{json}") nil end rescue LoadError require "json" define_method(:dump_json) do |obj| JSON.generate(obj) end define_method(:load_json) do |json| JSON.parse(File.read(json), symbolize_names: true) rescue JSON::ParserError Allure.configuration.logger.error("Failed to parse json: #{json}") nil end end end |
#load_json(json) ⇒ Hash
Load json from file using Oj or JSON
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/allure_ruby_commons/_json_helper.rb', line 17 module JsonHelper # @return [Hash] Oj json options OJ_OPTIONS = { mode: :custom, use_to_hash: true, ascii_only: true }.freeze begin require "oj" define_method(:dump_json) do |obj| Oj.dump(obj, OJ_OPTIONS) end define_method(:load_json) do |json| Oj.load_file(json, symbol_keys: true) rescue Oj::ParseError Allure.configuration.logger.error("Failed to parse json: #{json}") nil end rescue LoadError require "json" define_method(:dump_json) do |obj| JSON.generate(obj) end define_method(:load_json) do |json| JSON.parse(File.read(json), symbolize_names: true) rescue JSON::ParserError Allure.configuration.logger.error("Failed to parse json: #{json}") nil end end end |