I have Ruby models which are populated from the responses of API calls in the following way:
JSON.parse
converts the response to aHash
- the
Hash
is passed into theinitialize
method of a class - the
initialize
method converts camelCase hash keys and assigns underscore_case instance variables - controller code works with these instances and converts back to json to send to the browser
This works fine, but some of these response objects are large. Others are arrays of large objects.
Profiling shows that this process consumes a lot of CPU (and memory, but that is less of a concern) -- which makes sense given that I create hashes in order to create objects, and the back and forth between camelCase and underscore_case happens A LOT -- so what libraries or techniques have you come across which solve this problem?
Here is an oversimplified example:
JSON response from a third party API (unlikely to change):
"{\"abcDef\": 123, \"ghiJkl\": 456, \"mnoPqr\": 789}"
Class definition (attributes unlikely to change):
class Data
attr_accessor :abc_def, :ghi_jkl, :mno_pqr
def initialize(attributes = {})
attributes.each do |key, val|
send "#{key.underscore}=".to_sym, val
end
end
def as_json
instance_variables.reduce({}) do |hash, iv|
iv_name = iv.to_s[1..-1]
v = send(iv_name) if self.respond_to?(iv_name)
hash[iv_name.camelize(:lower)] = (v.as_json(options) if v.respond_to?(:as_json)) || v
hash
end
end
end
Controller:
get '/' do
d = Data.new JSON.parse(api.get)
# ... do some work ...
content_type 'application/json'
d.to_json
end
2 Answers 2
TheJSON schema and data class attributes are unlikely to change. Consider implementing the class to encapsulate the JSON hash, adapting it to the ruby idioms and whatever other custom operations you've added. This should greatly decrease the cost and complexity of deserialization.
Your approach looks OK. It might consume a bunch of CPU power, but is that actually an issue? It's a little unclear from your question if you just noticed heavier CPU load, or if it's actually causing trouble.
If it is an issue, I guess you might use Data
as a wrapper around the hash instead. The idea would be to avoid whole-sale conversion back and forth, and instead only do it when necessary.
class Data
def initialize(json)
@values = json
end
def method_missing(method, *args, &blk)
# grab base property name, and, if the method is a setter,
# the `=` at the end - it'll be nil for a reader method
(/(?<name>.+?)(?<setter>=)?$/ =~ method.to_s) # thanks to Naklion
property = name.camelize
if @values.has_key?(property)
setter ? @values[property] = args.first : @values[property]
else
super
end
end
def as_json
@values
end
end
Alternatively, if method_missing
is casting the net too wide, you can go the meta-programming route and do something like this, provided you declare a list of methods you want (like you do now with attr_accessor
):
class Data
def initialize(json)
@values = json
end
# define getters/setters for the properties we want
%{abc_def ghi_jkl mno_pqr}.each do |name|
property = name.camelize
define_method(name) do
@values[property]
end
define_method("#{name}=") do |value|
@values[property] = value
end
end
def as_json
@values
end
end
Overall, though, it seems like a lot compared to just using the hash. From your question it's unclear why you even need the Data
class. Yes, there's some syntactic sugar in being able to write data.abc_def
instead of raw hash access, but if the cost is a performance hit, and said hit is too much, it would seem that the simplest solution is to just use the plain hash.
-
1\$\begingroup\$
(/(?<name>.+?)(?<setter>=)?$/ =~ method.to_s)
\$\endgroup\$Nakilon– Nakilon2014年11月16日 05:57:51 +00:00Commented Nov 16, 2014 at 5:57 -
\$\begingroup\$ @Nakilon Neat! I didn't actually know you could do that \$\endgroup\$Flambino– Flambino2014年11月17日 01:27:08 +00:00Commented Nov 17, 2014 at 1:27
-
\$\begingroup\$ stdlib, stdlib, stdlib, stdlib, stdlib, stdlib... \$\endgroup\$Nakilon– Nakilon2014年11月17日 01:51:39 +00:00Commented Nov 17, 2014 at 1:51
-
1\$\begingroup\$ @Nakilon yes thank you, I figured. I apologize for not knowing (or remembering) everything about Ruby. \$\endgroup\$Flambino– Flambino2014年11月17日 01:54:13 +00:00Commented Nov 17, 2014 at 1:54
Explore related questions
See similar questions with these tags.