Tutorial

Using mdTypes [EN]

While working with collections you may have to serialize its content. All collections can be casted to a normal String. You can use Cast, Str or the toString() method. While creating String representations is really easy, deserializing these Strings back to lists and maps may be harder. For this purpose we have a helper class:
- md/helper/mdCollectionsHelper

mdCollectionsHelper contains only a few static methods:

createList(String) As mdList(String)
createMap(String) As mdMap(String, String)
encode(String) As String
decode(String) As String

So, createList will generate a list out of the String, createMap a map. Because the representation of these Strings, it's important not to conflict with some special characters. To avoid problems, encode all Strings in you map containing these signs !"[]{}=,\r\n". On the other side you can use decode to get the correct value back.

Let's check out an example of how it might work:

'Include mdCollectionsHelper (it will include both mdList and mdMap for us)#IncludeOnce"md/helper/mdCollectionsHelper.bi"

'Prepare mdMap - mdCollectionsHelper will use this
mdMapDeclare(String,String)

'This constructor will get a map and fill the values of this class with the content of the mapConstructor MyClass(ByRef m As mdMap(String,String))This.label = mdCollectionsHelper.decode(m.get("label"))'if this data may be encoded, decode it again (see cast operator)This.value =ValInt(m.get("value"))This.weight =Val(m.get("weight"))EndConstructor

'Let's see the output for our created list - at this point we could send the data via internet or save it into a file or whatever...DimAsString stringRepresentation = listOfMyClassPrint"List as string: "& stringRepresentationPrint

'Get a list back of the string and prepare a new arrayDimAs mdList(String) listOfMaps = mdCollectionsHelper.createList(stringRepresentation)DimAs MyClass newArray(0To listOfMaps.size() - 1)

'Fill the array with the data of our string representationFor i AsInteger=LBound(newArray)ToUBound(newArray)
newArray(i)= Type<MyClass>(mdCollectionsHelper.createMap(listOfMaps.get(i)))Print i, newArray(i)Next