0

For example these json structures need to be converted to the following class:

JSON_str_1 = {"a":"stuff", "b":{"aa": "more_stuff", "bb":"even_more"}}
JSON_str_2 = {"a": "stuff", "aa": "more_stuff","b": {"bb":"even_more"}}
JSON_str_3 = {"b": {"a":"stuff" "aa": "more_stuff", "bb":"even_more"}}

To a class of this format:

class my_class < ActiveRecord
   attr_accessor: :a, :aa, :bb
end

All of the information I need encoded is the same just the structure is different. This would essentially involve flatttening the json structure.
How would one deal with that? Where in the Rails app would one put that conversion functionality? The model? The controller? A PORO somewhere?

I have a lot of this kind of conversion from unoptimized JSON to my class specific formats and it seems like a pretty common issue.

Dr. Chocolate
  • 2,043
  • 4
  • 25
  • 45
  • Where is this json coming from? In a request? – Noman Ur Rehman Mar 12 '16 at 20:09
  • I think this would belong with models. – Cthulhu Mar 12 '16 at 20:10
  • @NomanUrRehman: Yep, from the request. It would be specific to one route. Another route may have a different format of the same information that would also have to be converted to that model also. That's why I'm wondering if I should convert in the controller before calling the model. Or making a conversion class. Just not sure what to do. – Dr. Chocolate Mar 12 '16 at 20:16
  • @Cthulhu: I was thinking that, but there may be a series of different JSON strings encoding the same information that would all have to be converted to that model's format. And that would be like one method per JSON conversion required. – Dr. Chocolate Mar 12 '16 at 20:19
  • My gut instinct is a converter mixin. – Dr. Chocolate Mar 12 '16 at 20:20
  • @Nickthemagicman I would be happy to comment but your question needs more details like different JSON objects that would need to be converted, will all requests require the conversion, and once converted, what are you trying to achieve with the objects. – Noman Ur Rehman Mar 12 '16 at 20:23
  • Hi @NomanUrRehman. Thanks. I'll add some more details. – Dr. Chocolate Mar 12 '16 at 20:25

1 Answers1

0

I would start with a method in the model:

def self.from_json(json)
  new(a: json['a'], aa: json['a']['aa'], bb: json['a']['bb'])
end

What would be called like:

foo = MyClass.from_json(JSON_str)
spickermann
  • 100,941
  • 9
  • 101
  • 131
  • What about if there were different formats to be converted? Would you add a class for each conversion? Or would you factor it out at some point? – Dr. Chocolate Mar 12 '16 at 20:30
  • I would generate a converter class once there are more than two different formats or if that formats way more complex than your example. But I would try to keep everything in one class as long as it makes sense, is maintainable and understandable. – spickermann Mar 12 '16 at 20:36
  • Your updated answer contains more examples now. But all examples follow the same pattern and the structure is pretty simple. A small helper method like the one in this [answer](https://stackoverflow.com/questions/8301566/find-key-value-pairs-deep-inside-a-hash-containing-an-arbitrary-number-of-nested/8301752#8301752) could still solve the problem. Does one class method and one helper method justify a class on its own? I would say that depends on if that class is reusable or if it is only used in one place to generate instances of one specific class. – spickermann Mar 12 '16 at 20:58