Android Question Save Map with custom Type to file

Mike1970

Well-Known Member
Licensed User
Longtime User
Hello everyone.

I've a Map, that will be saved using File.WriteMap
B4X:
    Dim m1 As Map = CreateMap("Version": AppVersion, _
                              "Thing1": -1, _
                              "Thing2": "", _
                              "Thing3": "", _
                              "CustomType": Null)

in the Key "CustomType" I wish to save some data of a custom type, for example this one
B4X:
Type Pulizia (StartTime As Long, EndTime As Long)

I tried to save it by using the .Put method of Maps, saving it to file using .WriteMap, then read the file again with .ReadMap and using .Get("CustomType") to retrive the value and associate to a variable, but i get an error saying that is not castable...


what is the right way to save a custom type to file and retrive it?

Thanks in advance
 
Solution
I've a Map, that will be saved using File.WriteMap
It will be ALL STRINGS after you save it this way.

Method_636.png
File. WriteMap (Dir As String, FileName As String, Map As Map)​

Creates a new file and writes the given map. Each key value pair is written as a single line.
All values are converted to strings. <--- !!!

Use B4XSerializator to write or read such Maps....

DonManfred

Expert
Licensed User
Longtime User
I've a Map, that will be saved using File.WriteMap
It will be ALL STRINGS after you save it this way.

Method_636.png
File. WriteMap (Dir As String, FileName As String, Map As Map)​

Creates a new file and writes the given map. Each key value pair is written as a single line.
All values are converted to strings. <--- !!!

Use B4XSerializator to write or read such Maps....
 
Last edited:
Upvote 1
Solution

Mike1970

Well-Known Member
Licensed User
Longtime User
It will be ALL STRINGS after you save it this way.

Method_636.png
File. WriteMap (Dir As String, FileName As String, Map As Map)​

Creates a new file and writes the given map. Each key value pair is written as a single line.
All values are converted to strings. <--- !!!

Use B4XSerializator to write or read such Maps....
Ouhhh thanks.. i didn't read

I did like so, seems to work (i don't know if is the best way)

B4X:
Dim ser As B4XSerializator

'--- WRITE
Dim p As CustomType
p.Initialize
p.StartTime = 50
Dim mt As Map = CreateMap("K1": "V1", "K2":1, "K3":p)
File.WriteBytes(File.DirInternal, "Test.txt", ser.ConvertObjectToBytes(mt))

'--- READ
Dim mt2 As Map = ser.ConvertBytesToObject(File.ReadBytes(File.DirInternal, "Test.txt"))
Dim p2 As CustomType = mt2.Get("K3").As(CustomType)
Log(p2)
 
Upvote 0
Top