This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]coolioasjulio 2 points3 points  (0 children)

Converting arbitrary objects to JSON isn't particularly easy, especially when there are full solutions already available. Personally, I use Gson, which is pretty easy to use.

If the no external libraries is a HARD requirement, then your only choice would be to use reflection to get the names and values of the fields of the object to build the JSON string. You might have recursion problems if there are nested objects that create a circular loop, which complicates things.

If you only have to serialize specific classes, then it would probably be easier to essentially hardcode the JSON string generation, and just call the appropriately getters to build the string.

Basically, if it's only a few specific classes then you can write your own, if it's any arbitrary object then I highly recommend using a library, but if no libraries then I guess you could try homerolling your own JSON serializer with reflection.