Author Topic: Scriptbasic: JSON module  (Read 8629 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Scriptbasic: JSON module
« on: November 08, 2018, 08:40:04 PM »
I've been playing around with a minimal JSON implementation called Jasmine

Code: ScriptBasic
  1. INCLUDE json.bas
  2.  
  3. JSON_STRING ="""{"user": "johndoe", "admin": false, "uid": 1000,
  4. "groups": ["users", "wheel", "audio", "video"],
  5. "plat_conf": {"ip_addr": "127.0.0.1","port_num": "4444"}}"""
  6.  
  7.  
  8. js = json::parse(JSON_STRING)
  9.  
  10. if js then
  11.     print "User: ",json::find("user"),"\n"
  12.     print "User is Admin: ",json::find("admin"),"\n"
  13.     print "User ID: ",json::find("uid"),"\n"
  14.     print "User Groups: ",json::find("groups"),"\n"
  15.     print "Platform Config: ",json::find("plat_conf"),"\n"
  16. end if
  17.  

[riveraa@MacDev ~/tmp/sb/tests] $ scriba tjson.bas
User: johndoe
User is Admin: false
User ID: 1000
User Groups: ["users", "wheel", "audio", "video"]
Platform Config: {"ip_addr": "127.0.0.1","port_num": "4444"}
[riveraa@MacDev ~/tmp/sb/tests] $


I still need to work out how to pull the arrays out (User Groups and Platform Config above), but for a couple hours of playing it's pretty functional.

AIR.
« Last Edit: November 08, 2018, 08:50:54 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #1 on: November 09, 2018, 12:26:42 AM »
If you are serious about interfacing with web services, JSON support is a must.

Nice job AIR!

Offline jalih

  • Advocate
  • Posts: 109
Re: Scriptbasic: JSON module
« Reply #2 on: November 09, 2018, 05:18:04 AM »
I still need to work out how to pull the arrays out (User Groups and Platform Config above), but for a couple hours of playing it's pretty functional.

8th uses JSON syntax for array and map. I would like to see Basic compiler using the same feature for defining arrays and maps.

Offline AlyssonR

  • Advocate
  • Posts: 126
Re: Scriptbasic: JSON module
« Reply #3 on: November 09, 2018, 03:08:53 PM »
Great stuff!

I was going to try to use a JSON implementation for my DB (it's entirely document based, and human readable), but ended up using my own markup format (in VB6). It's extremely cut down and data specific, but arrays and multiline values are handled.

If I'd seen this, I'd have gone for it.  ::wry grin::

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Scriptbasic: JSON module
« Reply #4 on: November 26, 2018, 02:27:40 PM »
EDIT:  UPDATED TO REFLECT CHANGES IN MODULE

Switched the backend to use PARSON

It's a much simpler api to work with, and integrates well with SB.

Code: ScriptBasic
  1. ' SB JSON Create Entries
  2.  
  3. include json.bas
  4.  
  5. root = json::New()
  6. json::SetText(root,"client1.name","Joe Blow")
  7. json::SetNum(root,"client1.age", 56)
  8. json::SetText(root,"client1.address.city","Tarrytown")
  9. json::SetText(root,"client1.address.state","NY")
  10. json::SetText(root,"client1.address.zip","10891")
  11.  
  12. json::SetText(root,"client2.name","John Smith")
  13. json::SetNum(root,"client2.age",86)
  14. json::SetText(root,"client2.address.city","Cupertino")
  15. json::SetText(root,"client2.address.state","CA")
  16. json::SetText(root,"client2.address.zip","N/A")
  17. json::Save("root.json")
  18.  

Output:
Code: Text
  1. {
  2.     "client1": {
  3.         "name": "Joe Blow",
  4.         "age": 56,
  5.         "address": {
  6.             "city": "Tarrytown",
  7.             "state": "NY",
  8.             "zip": "10891"
  9.         }
  10.     },
  11.     "client2": {
  12.         "name": "John Smith",
  13.         "age": 86,
  14.         "address": {
  15.             "city": "Cupertino",
  16.             "state": "CA",
  17.             "zip": "N\/A"
  18.         }
  19.     }
  20. }
  21.  

Reading it back in:
Code: ScriptBasic
  1. ' SB READ IN CREATED JSON
  2.  
  3. INCLUDE json.bas
  4.  
  5. jObject = json::load("root.json")
  6. for i = 0 to json::count(jObject)-1
  7.     obj = json::object(jObject,i)
  8.     print string(40,"-"),"\n"
  9.     print json::Get(obj,"name"),"\n"
  10.     print json::Get(obj,"age"),"\n"
  11.     print json::Get(obj,"address.city"),"\n"
  12.     print json::Get(obj,"address.state"),"\n"
  13.     print json::Get(obj,"address.zip"),"\n"
  14. next
  15. print string(40,"-"),"\n"
  16.  

Output:
[riveraa@MacDev ~/tmp/sb/tests] $ scriba parse2.bas
----------------------------------------
Joe Blow
56
Tarrytown
NY
10891
----------------------------------------
John Smith
86
Cupertino
CA
N/A
----------------------------------------


You'll have to wait on John to make it available for testing in the main SB repository.

AIR.
« Last Edit: November 26, 2018, 06:46:41 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #5 on: November 26, 2018, 02:35:13 PM »
Is it xmas already?

This will be a valuable extension as most REST web services have switched from XML to JSON.

Great job AIR!


Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #6 on: November 26, 2018, 02:44:42 PM »
Does it handle JSON data beyond strings yet?

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Scriptbasic: JSON module
« Reply #7 on: November 26, 2018, 02:46:12 PM »
Does it handle JSON data beyond strings yet?

No, I'll leave that to you to add.... :P

Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #8 on: November 26, 2018, 02:47:13 PM »
I knew that was coming.  :)

Array mapping from JSON to SB arrays (matrix) structures will be the fun part.
« Last Edit: November 26, 2018, 02:50:12 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Scriptbasic: JSON module
« Reply #9 on: November 26, 2018, 05:40:30 PM »
Quote from: latest commit
Changed 'TEXT' method to 'GET', enabled detection of string or number in method.…

Changed 'TEXT' method to 'GET', enabled detection of string or number in method.  Now returns string OR number where applicable.

Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #10 on: November 26, 2018, 06:23:05 PM »
Nice!

Can one use SB's STRING creation API (uses thread safe myalloc) for buffers with fixed lengths to strings auto allocated by SB?

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Scriptbasic: JSON module
« Reply #11 on: November 26, 2018, 06:27:20 PM »
There's an api call where you can specify custom malloc/free options.  Give it a shot.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Scriptbasic: JSON module
« Reply #12 on: November 26, 2018, 06:48:51 PM »
Quote from: LATEST COMMIT
git commit -m "Added SetText and SetNum methods, removed Add method"

Post announcing change to backend has been updated...

I'm pretty much done with this.  Anything else you want in there, John, is on you.....

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #13 on: November 26, 2018, 06:56:51 PM »
I'll get this compile for Linux and Windows and push it to the public SB-DEV sandbox.

Offline John

  • Forum Support / SB Dev
  • Posts: 3512
    • ScriptBasic Open Source Project
Re: Scriptbasic: JSON module
« Reply #14 on: November 26, 2018, 08:33:28 PM »
AIR,

I used my Linus source directory and was able to build the json module. I'm able to create the root.json file but having a problem with the Get function in your code example saying missing argument(s).


{
    "client1": {
        "name": "Joe Blow",
        "age": 56,
        "address": {
            "city": "Tarrytown",
            "state": "NY",
            "zip": "10891"
        }
    },
    "client2": {
        "name": "John Smith",
        "age": 86,
        "address": {
            "city": "Cupertino",
            "state": "CA",
            "zip": "N\/A"
        }
    }
}



jrs@jrs-laptop:~/sb/abcc/json$ scriba json_get.sb
----------------------------------------
(0): error &H35:Mandatory argument is missing
jrs@jrs-laptop:~/sb/abcc/json$

« Last Edit: November 26, 2018, 08:35:48 PM by John »