Please enhance SetJSONArray() and SetJSONObject()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Please enhance SetJSONArray() and SetJSONObject()

Post by Little John »

PB's other SetJSON*() commands assign values to JSON data.
With SetJSONArray() and SetJSONObject() this is not possible. They only change the JSON type, so that there is an empty array or empty object, respectively.

Please enhance SetJSONArray() and SetJSONObject() so that they accept an optional parameter that allows to assign an existing array or object, respectively.

Code: Select all

SetJSONArray(JSONValue [, array])
SetJSONObject(JSONValue [, object])
Currently, we have to write our own code in order to achieve this, see
viewtopic.php?f=13&t=71020
User avatar
the.weavster
Addict
Addict
Posts: 1537
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by the.weavster »

+1
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by NicTheQuick »

+1
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by kenmo »

+1


A related request is a function to copy JSON objects/arrays ("copy" or "merge" or "insert" etc.)

I added functions for these requests to my own JSON_Helper.pbi
https://raw.githubusercontent.com/kenmo ... Helper.pbi

Code: Select all

SetJSONObjectEx(*JSONValue, *Object = #Null)
SetJSONArrayEx(*JSONValue, *Array = #Null)
CopyJSONNode(*Src, *Dest, Key.s = "") overwrites *Dest OR adds as a member if a Key is specified

also added: ComposeJSONNode(*Node, IndentSpaces, NewLine) to Compose a subset of a JSON
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by Little John »

Hi kenmo,

I wasn't aware of your JSON_Helper.pbi. It looks good and useful, thank you!

I see a particular problem, though: Any code that is inside a module currently cannot use code from JSON_Helper.pbi, because that code is not inside of a module. If you'd "wrap" the code in a module, then it could be used even by other code that is also inside of a module. Just a suggestion.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by davido »

+1
DE AA EB
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by RSBasic »

+1
Image
Image
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by kenmo »

I wasn't aware of your JSON_Helper.pbi. It looks good and useful, thank you!

I see a particular problem, though: Any code that is inside a module currently cannot use code from JSON_Helper.pbi, because that code is not inside of a module. If you'd "wrap" the code in a module, then it could be used even by other code that is also inside of a module. Just a suggestion.
I don't think I shared my JSON include on the forum before.

Can a PB Module not call functions from a regular IncludeFile?
I guess I will try that. I don't write PB Modules much, they have some quirks that turned me back to regular IncludeFiles.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by Little John »

kenmo wrote:Can a PB Module not call functions from a regular IncludeFile?
No, it can't. There are requests for this feature, but at least currently this is not possible. Code inside a module can only call code which is inside a module, too.

See this example:

Code: Select all

Procedure CantBeCalledFromAModule ()
   Debug "Hi there!"
EndProcedure

;----------------------
DeclareModule Test
   Declare Demo()   
EndDeclareModule

Module Test
   Procedure Demo ()
      Debug "Hello!"
      ; CantBeCalledFromAModule ()
   EndProcedure   
EndModule
;----------------------

CantBeCalledFromAModule()
Test::Demo()
It works fine. After uncommenting line 13, you'll get an error.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by StarBootics »

Little John wrote:
kenmo wrote:Can a PB Module not call functions from a regular IncludeFile?
No, it can't. There are requests for this feature, but at least currently this is not possible.
It's possible using CallFunctionFast(), in the following code I'm passing the procedure address directly but it's possible to store this address in a global variable inside the module and use it instead.

Code: Select all

Procedure CantBeCalledFromAModule ()
   Debug "Hi there!"
EndProcedure

;----------------------
DeclareModule Test
   Declare Demo(CanBeCalledFromModule.i)   
EndDeclareModule

Module Test

   Procedure Demo(CanBeCalledFromModule.i)
      Debug "Hello!"
      
      CallFunctionFast(CanBeCalledFromModule)
   
   EndProcedure   
EndModule
;----------------------

Test::Demo(@CantBeCalledFromAModule())
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please enhance SetJSONArray() and SetJSONObject()

Post by Little John »

StarBootics wrote:It's possible using CallFunctionFast()
Yes, that's right.

I was meaning normal, direct calling. And CallFunctionFast() is fairly limited.
[u]Documentation[/u] wrote:Note: This function is not very flexible and does not handle string/float/double/quad parameters or string/float/double/quad returns.
Post Reply