Found an issue in SpiderBasic ? Please report it here !
JCV
 
Posts: 11
Joined: Wed Apr 17, 2019 6:08 am

[Done] 2.21 - InsertJSONList Null Result

by JCV Sun Apr 28, 2019 1:35 pm

Code: Select all
NewList Names.s()
  AddElement(Names()): Names() = "John"
  AddElement(Names()): Names() = "Jane"
  AddElement(Names()): Names() = "Jim"
 
  If CreateJSON(0)
    ObjectValue = SetJSONObject(JSONValue(0))
    mat_req = AddJSONMember(ObjectValue, "Class A")
    InsertJSONList(mat_req, Names())
    Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
  EndIf


The output should be
Code: Select all
{
  "Class A": [
      "John",
      "Jane",
      "Jim"
    ]
}


but I'm getting this result.
Code: Select all
{
   "Class A": null
}
Last edited by JCV on Sun Apr 28, 2019 1:56 pm, edited 2 times in total.
JCV
 
Posts: 11
Joined: Wed Apr 17, 2019 6:08 am

Re: 2.21 - InsertJSONList Null Result

by JCV Sun Apr 28, 2019 1:55 pm

InsertJSONArray is also giving null result.

Code: Select all

Dim Colors.s(3)
  Colors(0) = "red"
  Colors(1) = "yellow"
  Colors(2) = "green"
  Colors(3) = "blue"
If CreateJSON(0)
    ObjectValue = SetJSONObject(JSONValue(0))
    SetJSONInteger(AddJSONMember(ObjectValue, "x"), 10)
    SetJSONInteger(AddJSONMember(ObjectValue, "y"), 20)
    SetJSONInteger(AddJSONMember(ObjectValue, "z"), 30)
    InsertJSONArray(AddJSONMember(ObjectValue, "Colors"), Colors())
   
    Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
  EndIf



Output should be
Code: Select all
{
  "Colors": [
      "red",
      "yellow",
      "green",
      "blue"
    ],
  "x"     : 10,
  "y"     : 20,
  "z"     : 30
}


But I'm getting this result
Code: Select all
{
   "x": 10,
   "y": 20,
   "z": 30,
   "Colors": null
}
User avatar
Paul
 
Posts: 52
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada

Re: 2.21 - InsertJSONList Null Result

by Paul Sun Apr 28, 2019 9:13 pm

Yes, same code works fine in Pure Basic but not Spider Basic ;(
JCV
 
Posts: 11
Joined: Wed Apr 17, 2019 6:08 am

Re: 2.21 - InsertJSONList Null Result

by JCV Tue Apr 30, 2019 11:20 am

Also null result for InsertJSONStructure

Code: Select all
Structure car
  color.s
EndStructure
 
mycar.car\color = "Red"
 
If CreateJSON(0)
  ObjectValue = SetJSONObject(JSONValue(0))
  object = AddJSONMember(ObjectValue, "Honda")
  InsertJSONStructure(object, mycar, car)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf


Output should be
Code: Select all
{
  "Honda": {
      "color": "Red"
    }
}


but I get this result
Code: Select all
{
   "Honda": null
}
JCV
 
Posts: 11
Joined: Wed Apr 17, 2019 6:08 am

Re: 2.21 - InsertJSONList Null Result

by JCV Wed May 01, 2019 7:23 am

Does anyone have a workaround for this?
User avatar
Paul
 
Posts: 52
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada

Re: 2.21 - InsertJSONList Null Result

by Paul Wed May 01, 2019 12:55 pm

You could do this...
Code: Select all
Dim Colors.s(3)
  Colors(0) = "red"
  Colors(1) = "yellow"
  Colors(2) = "green"
  Colors(3) = "blue"
If CreateJSON(0)
  ObjectValue = SetJSONObject(JSONValue(0))
  SetJSONInteger(AddJSONMember(ObjectValue, "x"), 10)
  SetJSONInteger(AddJSONMember(ObjectValue, "y"), 20)
  SetJSONInteger(AddJSONMember(ObjectValue, "z"), 30)
 
  color=SetJSONArray(AddJSONMember(ObjectValue, "Colors"))
  For tmp=0 To 3
    SetJSONString(AddJSONElement(color),Colors(tmp))
  Next

  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
JCV
 
Posts: 11
Joined: Wed Apr 17, 2019 6:08 am

Re: 2.21 - InsertJSONList Null Result

by JCV Wed May 01, 2019 1:24 pm

Thanks Paul. I did similar as well while waiting for fix. Its a bit hassle especially for long structure.
If someone has macro replacement for the functions mentioned above it would also be fine.
User avatar
Paul
 
Posts: 52
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada

Re: 2.21 - InsertJSONList Null Result

by Paul Wed May 01, 2019 3:32 pm

Maybe something like this till Fred fixes the problem?

Code: Select all
Macro InsertJSONArray(myparam,myarray)
  tmpval=SetJSONArray(myparam)
  Dim tmparray.s(ArraySize(myarray))
  CopyArray(myarray,tmparray())
  For tmp=0 To ArraySize(tmparray())
    SetJSONString(AddJSONElement(tmpval),tmparray(tmp))
  Next
EndMacro

Macro InsertJSONList(myparam,myarray)
  tmpval=SetJSONArray(myparam)
  ForEach myarray
    SetJSONString(AddJSONElement(tmpval),myarray)
  Next
EndMacro

;-------------------------------------------

Dim Colors.s(3)
  Colors(0) = "red"
  Colors(1) = "yellow"
  Colors(2) = "green"
  Colors(3) = "blue"
 
If CreateJSON(0)
  ObjectValue = SetJSONObject(JSONValue(0))
  SetJSONInteger(AddJSONMember(ObjectValue, "x"), 10)
  SetJSONInteger(AddJSONMember(ObjectValue, "y"), 20)
  SetJSONInteger(AddJSONMember(ObjectValue, "z"), 30)
  InsertJSONArray(AddJSONMember(ObjectValue, "Colors"), Colors())
 
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
 

NewList Names.s()
AddElement(Names()): Names() = "John"
AddElement(Names()): Names() = "Jane"
AddElement(Names()): Names() = "Jim"

If CreateJSON(0)
  ObjectValue = SetJSONObject(JSONValue(0))
  mat_req = AddJSONMember(ObjectValue, "Class A")
  InsertJSONList(mat_req, Names())
 
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
JCV
 
Posts: 11
Joined: Wed Apr 17, 2019 6:08 am

Re: 2.21 - InsertJSONList Null Result

by JCV Sun May 05, 2019 5:38 am

Thanks Paul. InsertJSONStructure is more complicated so i did something like this.
For now I combined several JSON into one.

Code: Select all
  Protected out_session.s
  request.s = "{"
     
  If CreateJSON(0)
    ObjectValue = SetJSONObject(JSONValue(0))
    info = AddJSONMember(ObjectValue, "SessionID")
    SetJSONString(info, g_sessionID)
    out_session = ComposeJSON(0)
    FreeJSON(0)
  EndIf
 
  request + Mid(out_session,2,Len(out_session)-2)   + ","
   
  ;-----
 
  If CreateJSON(0)
    ObjectValue = SetJSONObject(JSONValue(0))
    InsertJSONList(ObjectValue, mat_request_info())
    out_session = ComposeJSON(0)
    FreeJSON(0)
  EndIf
 
  request + ~"\"request_info\":" + out_session +  ","
 
  ;-----
 
  If CreateJSON(0)
    ObjectValue = SetJSONObject(JSONValue(0))
    InsertJSONStructure(ObjectValue, *material_request, db_mat_request)
    out_session = ComposeJSON(0)
    FreeJSON(0)
  EndIf
 
  request + ~"\"mat_request\":{" + Mid(out_session,2,Len(out_session)-2) + "}"
  request + "}"
 
  Header("Content-Type") = "application/json"
  HTTPRequest(#PB_HTTP_Post, "mat_request.php", request, @submit_mat_request_event(), 0, Header())
 
the.weavster
 
Posts: 164
Joined: Sat Mar 01, 2014 3:02 pm

Re: 2.21 - InsertJSONList Null Result

by the.weavster Mon May 06, 2019 2:55 pm

One of the things I really like about SB is that you can drop into JS if you want and when it comes to JSON I think JS does it better:

Code: Select all
s.s = ""
!var obj = {'colors':['red','yellow','green','blue'],'x':10,'y':20,'z':30};
!v_s = JSON.stringify(obj,null,'\t');
Debug s
Return to Bugs Reports

Who is online

Users browsing this forum: No registered users and 2 guests