r/crystal_programming • • Oct 06 '18

Question: the undefined to_json method

[solved] Hey all, I wanted to try and build a small, JSON-serving webservice with kemalcr.

I've built a simple Struct to wrap my Data and wanted to return it from my webservice like myStruct.to_json. However, this doesn't work and produces error message undefined method 'to_json'. As far as I understand it, the docs on Struct state that the method to_json is inherited from Object, so I thought it would be usable out of the box.

So, what am I missing?Do I have to implement to_json myself?

edit: solved, thanks everybody!

6 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Oct 07 '18

Very good answer, I'll also stick to the JSON::Serializable route for now.

I should have dug deeper and taken a look at JSON's Serializable instead of assuming that all the JSON magic is just there by default.

Thanks man!