ruby - Deserialize JSON Strings Directly into Models? -
i have ruby models populated responses of api calls in following way:
json.parse
converts responsehash
- the
hash
passedinitialize
method of class - the
initialize
method converts camelcase hash keys , assigns underscore_case instance variables
this works fine, of these response objects large. others arrays of large objects.
profiling shows process consumes lot of memory -- makes sense, create hashes in order create objects, , i'm sure camelcase underscore_case conversion expensive -- libraries or techniques have come across solve problem?
(update) here oversimplified example:
json response third party api (unlikely change):
"{\"abcdef\": 123, \"ghijkl\": 456, \"mnopqr\": 789}"
class definition (unlikely change):
class data attr_accessor :abc_def, :ghi_jkl, :mno_pqr def initialize(attributes = {}) attributes.each |key, val| send "#{key.underscore}=".to_sym, val end end end
creating object:
data.new json.parse(api.get)
Comments
Post a Comment