Code: Select all
Enumeration
#file_read
#json_result
#button_import
#button_export_xml
EndEnumeration
Structure mazeTile
toRemove.l
start.l
EndStructure
Structure pathsMap
id.l
cash.l
List tileids.l()
EndStructure
Global NewList tiles.mazeTile()
Global NewList paths.pathsMap()
Procedure fileReadCall(Status, Filename$, File, Size)
If Status = #PB_Status_Loaded
Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
Debug "Reading.."
ReadStringFormat(#file_read)
Input$ = ReadString(#file_read,#PB_UTF8 | #PB_File_IgnoreEOL)
Debug Input$
; If ParseJSON(#json_result,Input$)
If ParseJSON(#json_result,Input$)
ClearList(tiles())
ObjectValue = JSONValue(#json_result)
ExtractJSONList(GetJSONMember(JSONValue(#json_result),"Tiles"),tiles())
ClearList(paths())
ExtractJSONList(GetJSONMember(JSONValue(#json_result),"Path"),paths())
Debug ListSize(tiles())
Debug ListSize(paths())
Else
Debug "json read error"
Debug JSONErrorMessage()
Debug JSONErrorLine()
Debug JSONErrorPosition()
EndIf
Debug IsJSON(#json_result)
EndIf
EndProcedure
Procedure fileImporReaded()
If NextSelectedFile()
ReadFile(#file_read, SelectedFileID(), @fileReadCall(), #PB_LocalFile | #PB_UTF8)
EndIf
EndProcedure
Procedure GadgetEvent()
Select EventGadget()
Case #button_import
filename$ = OpenFileRequester(".json",@fileImporReaded(),#PB_Requester_MultiSelection)
Case #button_export_xml
For id = 0 To 10
AddElement(tiles())
tiles()\start = Random(10)
tiles()\toRemove = Random(2)
Next id
For id = 0 To 10
AddElement(paths())
paths()\cash = Random(5)
paths()\id = Random(3)
AddElement(paths()\tileids())
paths()\tileids() = Random(3)
AddElement(paths()\tileids())
paths()\tileids() = Random(3)
AddElement(paths()\tileids())
paths()\tileids() = Random(3)
Next id
fileson = CreateJSON(#PB_Any)
objectValue = SetJSONObject(JSONValue(fileson))
tilesjson = AddJSONMember(objectValue,"Tiles")
InsertJSONList(tilesjson,tiles())
pthsjson = AddJSONMember(objectValue,"Path")
InsertJSONList(pthsjson,paths())
ExportJSON(fileson,"demo.json")
EndSelect
EndProcedure
OpenWindow(0, 0, 0, 300, 150, "Read file example", #PB_Window_ScreenCentered)
ButtonGadget(#button_export_xml, 10, 10, 280, 30, "export json")
ButtonGadget(#button_import, 10, 40, 280, 30, "read json")
BindEvent(#PB_Event_Gadget, @GadgetEvent())