class MotionRecord::Serialization::JSONSerializer
Public Class Methods
generate_json(obj)
click to toggle source
JSON generate/parse code is hoisted from BubbleWrap::JSON
# File lib/motion_record/serialization/json_serializer.rb, line 23 def self.generate_json(obj) NSJSONSerialization.dataWithJSONObject(obj, options:0, error:nil).to_str end
parse_json(str_data)
click to toggle source
# File lib/motion_record/serialization/json_serializer.rb, line 27 def self.parse_json(str_data) return nil unless str_data data = str_data.respond_to?('dataUsingEncoding:') ? str_data.dataUsingEncoding(NSUTF8StringEncoding) : str_data opts = NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves | NSJSONReadingAllowFragments error = Pointer.new(:id) obj = NSJSONSerialization.JSONObjectWithData(data, options:opts, error:error) raise JSONParserError, error[0].description if error[0] obj end
Public Instance Methods
deserialize(value)
click to toggle source
# File lib/motion_record/serialization/json_serializer.rb, line 14 def deserialize(value) unless @column.type == :text raise "JSON can only be deserialized from TEXT columns" end self.class.parse_json(value) end
serialize(value)
click to toggle source
# File lib/motion_record/serialization/json_serializer.rb, line 7 def serialize(value) unless @column.type == :text raise "JSON can only be serialized to TEXT columns" end self.class.generate_json(value) end