Good afternoon everyone,
before almost 20 days or so I had post a problem I was having when I was trying to parse JSON. Here is the forum thread:
After @Daestrum advice I have changed slightly the code and now it returns a byte array and does not accept a ResultBuffer byte array in the arguments but it creates it in the sub and returns this instead as a sub result. With this modification I have managed to read different values of a JSON - at least that is what I though.
This is because after reading each new value it trashes slightly the previous read values. I am posting the code and the logs in order for you to have a visual first look of the problem but I also attach the project at the end. I run this code in a WeMos D1 Mini v3.0.0.
And here is what this code prints in logs:
I do not have the slightest clue on what is going wrong here. If someone of you can see the mistake please go on and share it with me. That is if there is a mistake in the code and it is not a minor bug of B4R so please @Erel have a look too now that it is public.
before almost 20 days or so I had post a problem I was having when I was trying to parse JSON. Here is the forum thread:
What is going on here when parsing this JSON?
Good afternoon everyone, I have created a test project which demonstrates a problem I am facing parsing some JSON. I cannot figure out what is going on here, why it is changing the SSID Byte Array and further more how to fix that. If anyone can help it would be highly appreciated. Thanks in...
www.b4x.com
After @Daestrum advice I have changed slightly the code and now it returns a byte array and does not accept a ResultBuffer byte array in the arguments but it creates it in the sub and returns this instead as a sub result. With this modification I have managed to read different values of a JSON - at least that is what I though.
This is because after reading each new value it trashes slightly the previous read values. I am posting the code and the logs in order for you to have a visual first look of the problem but I also attach the project at the end. I run this code in a WeMos D1 Mini v3.0.0.
Module Main:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private bc As ByteConverter
End Sub
Private Sub AppStart
Serial1.Initialize(9600)
Log("AppStart")
'example of connecting to a local network
' If wifi.Connect2(""SSID"", ""PASSWORD"") Then
' Log(""Connected to network"")
' Else
' Log(""Failed to connect to network"")
' End If
GlobalStore.Put (3, "{""aa"":""23"",""id"":""1332278""}")
GlobalStore.Put(4, "{""ssid"":""SSID_NAME"",""pass"":""SSID_PASS"",""lat"":""38.478711"",""long"":""22.663213"",""usr"":""hatzisn"",""usps"":""1234684658""}")
Trial(0)
End Sub
Sub Trial(tag As Byte)
Log("* * * We set blat from globalstore.slot4 * * *")
Dim bLat() As Byte
bLat = JSON.GetTextValueFromKey(GlobalStore.Slot4, "lat", Null, 0)
Dim iBLAT As UInt = bLat.Length
Log("iBLAT:" , iBLAT)
Log("* * * We copy bLat to bCpLat and log the result of bCpLat to check if it destroys the original array only * * *")
Dim bCpLat(iBLAT) As Byte
bc.ArrayCopy(bLat, bCpLat)
Log(bCpLat)
Log(bLat.Length)
Log("-", bLat, "-")
Log("---")
Log("* * * We set bLon from globalstore.slot4 * * *")
Dim bLon() As Byte
bLon = JSON.GetTextValueFromKey(GlobalStore.Slot4, "long", Null, 0)
Log(bLon)
Dim iBLON As UInt = bLon.Length
Log("iBLON:", iBLON)
Log("* * * We copy bLon to bCpLon and log the result of bCpLon to check if it destroys the original array only * * *")
Dim bCpLon(iBLON) As Byte
bc.ArrayCopy(bLon, bCpLon)
Log(bCpLon)
Log("* * * Now that we have created bCpLon we log bCpLat and see that it keeps last 4 digits correct and changes * * *")
Log("* * * the first five digits to the last five of bCpLon * * *")
Log("=================")
Log(bCpLat)
Log("=================")
Log("----")
Log("* * * We set bID from globalstore.slot3 * * *")
Log("* * * We copy bID to bCpID and log the result of bCpID to check if it destroys the original array only * * *")
Dim bID() As Byte
bID = JSON.GetTextValueFromKey(GlobalStore.Slot3, "id", Null, 0)
Log(bID)
Log("------")
Dim iBID As UInt = bID.Length
Log("iBID:", iBID)
Dim bCpID(iBID) As Byte
bc.ArrayCopy(bID, bCpID)
Log(bCpID)
Log("* * * Now that we have created bCpID we log bCpLat and bCpLon and see that it has trashed completely bCpLat * * *")
Log("* * * and in bCpLon has kept the .6 part and then* * *")
Log("=================")
Log(bCpLat)
Log("=================")
Log(" ")
Log("=================")
Log(bCpLon)
Log("=================")
Dim bLink() As Byte = "https://www.mysite.com/wthr/wthrcrnt.aspx?lat=".GetBytes
Log(bLink)
Dim iBLINK As UInt = bLink.Length
Log("iBLINK:" , iBLINK)
Dim bKaiLon() As Byte = "&lon=".GetBytes
Log(bKaiLon)
Dim iBKAILON As UInt = bKaiLon.Length
Log("iBKAILON:", iBKAILON)
Dim bKaiID() As Byte = "&id=".GetBytes
Log(bKaiID)
Dim iBKAIID As UInt = bKaiID.Length
Log("iBKAIID:", iBKAIID)
Log(StackBufferUsage)
'*************************************************************************
'SHOULD BE GIVING THE FOLLOWING LINE:
'38.778711-22.763213-1332272
'*************************************************************************
'INSTEAD IT GIVES
'13322781�-.613322-1332278
'*************************************************************************
Log(bCpLat, "-", bCpLon, "-", bCpID)
Dim iLen As ULong = iBLINK + iBLAT + iBKAILON + iBLON + iBKAIID + iBID
Log("iLEN: ", iLen)
Dim bAll(iLen) As Byte
Log("bAll.Length: ", bAll.Length)
bc.ArrayCopy2(bLink, 0, bAll, 0, iBLINK)
bc.ArrayCopy2(bCpLat, 0, bAll, iBLINK, iBLAT)
bc.ArrayCopy2(bKaiLon, 0, bAll, iBLINK + iBLAT, iBKAILON)
bc.ArrayCopy2(bCpLon, 0, bAll, iBLINK + iBLAT + iBKAILON, iBLON)
bc.ArrayCopy2(bKaiID, 0, bAll, iBLINK + iBLAT + iBKAILON + iBLON, iBKAIID)
bc.ArrayCopy2(bCpID, 0, bAll, iBLINK + iBLAT + iBKAILON + iBLON + iBKAIID, iBID)
'*************************************************************************
'SHOULD BE GIVING:
'https://www.mysite.com/wthr/wthrcrnt.aspx?lat=38.778711&lon=22.763213&id=1332278
'*************************************************************************
'INSTEAD IT GIVES:
'https://www.mysite.com/wthr/wthrcrnt.aspx?lat=13322781�&lon=.613322&id=1332278
'*************************************************************************
Log(bAll)
'*************************************************************************
'THIS LINE IN THE ORGINAL PROJECT BREAKS BUT HERE IT GIVES THE SAME RESULTS:
'https://www.mysite.com/wthr/wthrcrnt.aspx?lat=13322781�&lon=.613322&id=1332278
'*************************************************************************
' Dim bAll() As Byte = JoinBytes(Array(bLink, bCpLat, bKaiLon, bCpLon, bKaiID, bCpID))
' Log(bAll)
End Sub
And here is what this code prints in logs:
********************* PROGRAM STARTING ****************
�����l,�:�tO��4��`�AppStart
* * * We set blat from globalstore.slot4 * * *
iBLAT:9
* * * We copy bLat to bCpLat and log the result of bCpLat to check if it destroys the original array only * * *
38.478711
9
-38.478711-
---
* * * We set bLon from globalstore.slot4 * * *
22.663213
iBLON:9
* * * We copy bLon to bCpLon and log the result of bCpLon to check if it destroys the original array only * * *
22.663213
* * * Now that we have created bCpLon we log bCpLat and see that it keeps last 4 digits correct and changes * * *
* * * the first five digits to the last five of bCpLon * * *
=================
632138711
=================
----
* * * We set bID from globalstore.slot3 * * *
* * * We copy bID to bCpID and log the result of bCpID to check if it destroys the original array only * * *
1332278
------
iBID:7
1332278
* * * Now that we have created bCpID we log bCpLat and bCpLon and see that it has trashed completely bCpLat * * *
* * * and in bCpLon has kept the .6 part and then it has added the first five digits of the bID * * *
=================
13322781�
=================
=================
.613322
=================
iBLINK:46
&lon=
iBKAILON:5
&id=
iBKAIID:4
12648
13322781�-.613322-1332278
iLEN: 80
bAll.Length: 80
�����l,�:�tO��4��`�AppStart
* * * We set blat from globalstore.slot4 * * *
iBLAT:9
* * * We copy bLat to bCpLat and log the result of bCpLat to check if it destroys the original array only * * *
38.478711
9
-38.478711-
---
* * * We set bLon from globalstore.slot4 * * *
22.663213
iBLON:9
* * * We copy bLon to bCpLon and log the result of bCpLon to check if it destroys the original array only * * *
22.663213
* * * Now that we have created bCpLon we log bCpLat and see that it keeps last 4 digits correct and changes * * *
* * * the first five digits to the last five of bCpLon * * *
=================
632138711
=================
----
* * * We set bID from globalstore.slot3 * * *
* * * We copy bID to bCpID and log the result of bCpID to check if it destroys the original array only * * *
1332278
------
iBID:7
1332278
* * * Now that we have created bCpID we log bCpLat and bCpLon and see that it has trashed completely bCpLat * * *
* * * and in bCpLon has kept the .6 part and then it has added the first five digits of the bID * * *
=================
13322781�
=================
=================
.613322
=================
iBLINK:46
&lon=
iBKAILON:5
&id=
iBKAIID:4
12648
13322781�-.613322-1332278
iLEN: 80
bAll.Length: 80
I do not have the slightest clue on what is going wrong here. If someone of you can see the mistake please go on and share it with me. That is if there is a mistake in the code and it is not a minor bug of B4R so please @Erel have a look too now that it is public.
Attachments
Last edited: