JSON to class and vice versa

Is there any easy way to convert JSON data into a class and to convert the elements of a class into JSON data? I ask because I find it much easier working with classes rather than JSONitems within my code, but I’d like to save the class data using JSON. dotNet has a serialization class which does this kind of thing, but I haven’t seen any easy way to doing it in Xojo.

Kevin Clark

This is definitely possible but you need to write all of your save and restore from JSONItem code manually, or maybe with some clever introspection.

I really need to post that class…

Kem, do you mean that you currently have a class which will convert from JSON to a class and vice versa?

For this situation I use a sub class of a JSONItem, with computed properties as a means of interacting with the properties of the class, but all the data is stored within the JSONItem.

I just put together a quick example using introspection in about 5 minutes. It won’t handle object properties, but it should give you an idea of how to do this. Maybe you can use this as an idea until Kem posts his class :wink:

Tease :stuck_out_tongue:

I don’t think Kem has time for us. Maybe he spends to much time on http://regexcrossword.com. Just joking of course!

Introspection can certainly be used to serialize and deserialize your classes to/from JSON, and yes, Kem has a class for that. He did a session on Serialization at XDC this year about it.

For simple classes, using Introspection isn’t that hard. When they get to be more complex, it’ll take quite a bit more work but once you have the framework written, it’ll be a huge time saver. I did a webinar on Introspection that went over the basics Introspection and one of the examples we went through was serializing and deserializing your objects via JSON if you wanna get started with something, keeping in mind it’s for simple classes.


I’ve posed the class. See the conversation and link here: