Currently I’m creating a String from listbox rows and columns, where I add column-delimiters and row-delimiters.
To re-populate the listbox, I split rows and columns into arrays, using those delimiters.
I wonder if it would be more speedy to save and reload from JSONItem strings?
I saw this contribution of Kem and now I’m stuck getting the JSONitem back into the listbox:
[quote=172827:@Kem Tekinay]Do you mean, is there some built-in function for this? No, you’d have to write the code yourself.
The easiest common format is JSON, so I’d recommend that. Maybe something like this:
Function ToJSONItem (Extends lb As ListBox) As JSONItem
dim root as new JSONItem
dim lastRow as integer = lb.ListCount - 1
dim lastCol as integer = lb.ColumnCount - 1
for row as integer = 0 to lastRow
dim rowJSON as new JSONItem
for col as integer = 0 to lastCol
rowJSON.Append lb.Cell( row, col )
next col
root.Append rowJSON
next row
return root
End Function
(This is off the top of my head and not tested.)[/quote]
This works fine so far.
I’m scratching my head on how to get this back into a listbox. The documentation is not helpful on this.
root.count is giving me the number of listbox rows then, but how do I know the number of ‘columns’ of each row and how do I access those?
Can anyone give me a hint on how to go back from JSON to listbox?
I have a situation where listbox contents gets temporarily saved in a database (all contents in one field) and at the same time it is possible that the number of columns may be altered in the meantime. So when reading back in, I have to validate the columns.
Also I have checkbox columns and I wonder if this would automatically work with your suggestion.
I’ll hit the road now and will give it a try later today.
Tim is right, that’s the fastest. If you want more control, you can use something like JSON to store other properties, like column widths, for example.
(I am going to be doing a session on data serialization and JSON at XDC.)
To answer your question, a JSONItem can work either like an array of variant, or like a Dictionary. In either case, the values it holds can be one of: nil, a number, a string, a boolean, or another JSONItem.
In the example, I gave, the “root” JSON worked as an array, and every element of the array was another JSON that also worked like an array. To pull the values out, you’d navigate it exactly as you would an array of arrays (only possible in Xojo with a variant array where each value is another array).
So, where “root” is the JSON that holds the data:
dim lastRow as integer = root.Count - 1
for row as integer = 0 to lastRow
dim rowJSON as JSONItem = root( row )
dim lastCol as integer = rowJSON.Count - 1
for col as integer = 0 to lastCol
lb.Cell( row, col ) = rowJSON( col )
next col
next row
BTW, because any string can be stored in JSON, there is nothing stopping you from putting the entire Listbox contents into a single value. For example:
dim j as new JSONItem
j.Value( "Name" ) = "MyListBox"
j.Value( "ColumnCount" ) = 10
j.Value( "RowCount" ) = 20
j.Value( "Contents" ) = lb.Cell( -1, -1 )
BTW, because any string can be stored in JSON, there is nothing stopping you from putting the entire Listbox contents into a single value. For example:
<…> You get the idea.[/quote]
Right. I was using JSON already from dictionaries, but I just felt insecure on how to use like an array. Seeing an example has helped, once again.