ruby - Deserialize JSON Strings Directly into Models? -


i have ruby models populated responses of api calls in following way:

  1. json.parse converts response hash
  2. the hash passed initialize method of class
  3. 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

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -