Class: TypedEAV::Field::Json
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- ApplicationRecord
- Base
- TypedEAV::Field::Json
- Defined in:
- app/models/typed_eav/field/json.rb
Constant Summary
Constants inherited from Base
Constants included from ColumnMapping
ColumnMapping::DEFAULT_OPERATORS_BY_COLUMN, ColumnMapping::FALLBACK_OPERATORS
Instance Method Summary collapse
-
#cast(raw) ⇒ Object
Parse JSON strings into objects/arrays.
Methods inherited from Base
#allowed_option_values, #array_field?, #clear_option_cache!, #default_value, #default_value=, #field_type_name, #optionable?, #validate_typed_value
Instance Method Details
#cast(raw) ⇒ Object
Parse JSON strings into objects/arrays. The JSON input partial posts a string from a textarea; without parsing it would land as a JSON- encoded string in json_value instead of the intended object.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/models/typed_eav/field/json.rb', line 12 def cast(raw) return [nil, false] if raw.nil? return [raw, false] if raw.is_a?(Hash) || raw.is_a?(Array) || raw.is_a?(Numeric) || raw == true || raw == false str = raw.to_s return [nil, false] if str.strip.empty? [::JSON.parse(str), false] rescue ::JSON::ParserError [nil, true] end |