Page 1 of 1
How to avoid empty array elements when making JSON out of structures?
Posted: Thu Apr 24, 2025 7:54 pm
by Quin
I have this code. It works as expected, accept the generated JSON has one more item in the array than I'd expect, an empty string. Is this because of how PureBasic's Dim/ReDim keywords work? If so, is there a way around it?
Code: Select all
EnableExplicit
Structure JSON
Array Test$(0)
EndStructure
Global NewList Names$()
AddElement(Names$()) : Names$() = "Quin"
AddElement(Names$()) : Names$() = "Sam"
AddElement(Names$()) : Names$() = "Malia"
Define j.JSON, Size.i
ForEach Names$()
Size = ArraySize(j\Test$()) + 1
ReDim j\Test$(Size)
j\Test$(Size - 1) = Names$()
Next
Define JSONID.i = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(JSONID), @j, JSON)
Debug ComposeJSON(JSONID)
FreeJSON(JSONID)
Re: How to avoid empty array elements when making JSON out of structures?
Posted: Thu Apr 24, 2025 8:58 pm
by Andesdaf
Yes it's because ReDim(Size) creates Size+1 elements. You could ReDim with Size-1 in the end or do the first ReDim only in the second iteration of the ForEach loop:
Code: Select all
Define j.JSON, Size.i
Size = ArraySize(j\Test$())
ForEach Names$()
If ListIndex(Names$()) > 0
Size + 1
ReDim j\Test$(Size)
EndIf
j\Test$(Size) = Names$()
Next
but that is only safe if the array is initially empty.
Re: How to avoid empty array elements when making JSON out of structures?
Posted: Thu Apr 24, 2025 9:50 pm
by Quin
Andesdaf wrote: Thu Apr 24, 2025 8:58 pm
Yes it's because ReDim(Size) creates Size+1 elements. You could ReDim with Size-1 in the end or do the first ReDim only in the second iteration of the ForEach loop:
Code: Select all
Define j.JSON, Size.i
Size = ArraySize(j\Test$())
ForEach Names$()
If ListIndex(Names$()) > 0
Size + 1
ReDim j\Test$(Size)
EndIf
j\Test$(Size) = Names$()
Next
but that is only safe if the array is initially empty.
I know the array will always be empty, I explicitly check for that, so that's okay.
Thanks for your help, this works!
Re: How to avoid empty array elements when making JSON out of structures?
Posted: Fri Apr 25, 2025 11:04 am
by NicTheQuick
It's not a good idea to constantly use ReDim in each iteration. When there are a lot of elements in the LinkedList ReDim is called on each element which means that in the background a lot of copying is be done in the worst case. Don't do that.
Just ReDim once before the loop and use a variable to iterate through the indexes:
Code: Select all
EnableExplicit
Structure JSON
Array Test$(0)
EndStructure
Global NewList Names$()
AddElement(Names$()) : Names$() = "Quin"
AddElement(Names$()) : Names$() = "Sam"
AddElement(Names$()) : Names$() = "Malia"
Define j.JSON, i.i = 0
ReDim j\Test$(ListSize(Names$()) - 1)
ForEach Names$()
j\Test$(i) = Names$()
i + 1
Next
Define JSONID.i = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(JSONID), @j, JSON)
Debug ComposeJSON(JSONID)
FreeJSON(JSONID)
For some reason this also works if there are no elements in the LinkedList although ReDim should throw an error because it is called with a negative size.
Edit: FYI: I created another topic regarding the size of -1 here:
https://www.purebasic.fr/english/viewtopic.php?t=86812