Class: EightBall::Marshallers::Json
- Inherits:
-
Object
- Object
- EightBall::Marshallers::Json
- Defined in:
- lib/eight_ball/marshallers/json.rb
Instance Method Summary collapse
-
#marshall(features) ⇒ String
Convert the given Features into a JSON array.
-
#unmarshall(json) ⇒ Array<EightBall::Feature>
Convert the given JSON into a list of Features.
Instance Method Details
#marshall(features) ⇒ String
Convert the given Features into a JSON array.
50 51 52 |
# File 'lib/eight_ball/marshallers/json.rb', line 50 def marshall(features) JSON.generate(features.map { |feature| feature_to_hash(feature).to_camelback_keys }) end |
#unmarshall(json) ⇒ Array<EightBall::Feature>
Convert the given JSON into a list of Features.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/eight_ball/marshallers/json.rb', line 64 def unmarshall(json) parsed = JSON.parse(json, symbolize_names: true) raise ArgumentError, 'JSON input was not an array' unless parsed.is_a? Array parsed.to_snake_keys.filter_map do |feature| build_feature feature end rescue JSON::ParserError => e EightBall.logger.error { "Failed to parse JSON: #{e..inspect}" } [] end |