Can a dictionary value be a picture?

My mobile app creates a dictionary that is an array of dictionaries describing objects. Each object includes a picture (300x260, not huge). When I perform a GenerateJSON on the array of dictionaries I get a crash. If I don’t include the picture in the dictionary for each object everything works fine. Dictionary values are Variants, and in the docs I don’t see any mention of Picture in the description of Variant.

So is the problem I’m having that you can’t add a Picture to a Dictionary? Or is something else going on?

The dictionary can store a picture.
But it can’t package it for an array.
So you may need to do GetData on the picture, pass it through EncodeBase64 and store that in the dictionary to go in the JSON.

1 Like

To clarify the answer, JSON values can only be Numbers, Strings, Booleans, NULLs, JSON objects and arrays of any of these items.

This is a good relevant feature request. <https://xojo.com/issue/62292>

1 Like

could you also hold a reference to a picture? you hold the picture in dictionary because you will save it all into a single file?

Is it an exception or a crash?
Because for a crash, please submit it via Feedback app.

You can store a reference to a picture in a dictionary, but you cannot convert that dictionary to JSON if you do. Greg explained why.

Yes, that will work, but if you tried to turn that into a JSON string either by JSONIten.ToString or GenerateJSON then you’ll get an exception.

1 Like

Good discussion. Thanks to all. I’m creating the JSON to save for when the app is relaunched. Since I’m saving all the properties that I use to draw the picture in the first place, I’ll just redraw the picture when I reload each object.

It’s not difficult to convert pictures to strings and back again, if that helps. RS used to come with examples, not sure if Xojo still does. MBS also has dedicated functions for it if I’m not mistaken.

Yes, see Picture.FromData and Picture.ToData.