My recent exposure was mostly to non OOP languages, so wonder any pattern or practice to do the below.
I need to write a function, which takes a json string (json array of objects) and produces list of objects (say Student). I checked the creational patterns, but doesn’t seems to fit. Do you think I should write a helper class just to create these objects.
If you have a kind of
utilclass already, this kind of function would fit nicely in that class. I wouldn’t make a completely separate class just for this. For instance, something like:You could, however, have a constructor in your
Studentclass which accepts a json String as a parameter and constructs theStudentobject from that. That would definitely be taking advantage of OO principles. But the GSON package renders a lot of that kind of thing obsolete and you might not want to bother programming it yourself. It has in itfromJson()andtoJson()methods which will parse the data and construct the objects for you.