All BASIC

BASIC Developer & Support Resources => Scripting Languages => Topic started by: AIR on November 08, 2018, 08:40:04 PM

Title: Scriptbasic: JSON module
Post by: AIR on November 08, 2018, 08:40:04 PM
I've been playing around with a minimal JSON implementation called Jasmine (https://github.com/zserge/jsmn)

Code: Script BASIC
  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.
Title: Re: Scriptbasic: JSON module
Post by: John 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!
Title: Re: Scriptbasic: JSON module
Post by: jalih 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.
Title: Re: Scriptbasic: JSON module
Post by: AlyssonR 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::
Title: Re: Scriptbasic: JSON module
Post by: AIR on November 26, 2018, 02:27:40 PM
EDIT:  UPDATED TO REFLECT CHANGES IN MODULE

Switched the backend to use PARSON (https://github.com/kgabis/parson)

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

Code: Script BASIC
  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: Script BASIC
  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.
Title: Re: Scriptbasic: JSON module
Post by: John 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!

Title: Re: Scriptbasic: JSON module
Post by: John on November 26, 2018, 02:44:42 PM
Does it handle JSON data beyond strings yet?
Title: Re: Scriptbasic: JSON module
Post by: AIR 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
Title: Re: Scriptbasic: JSON module
Post by: John 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.
Title: Re: Scriptbasic: JSON module
Post by: AIR 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.
Title: Re: Scriptbasic: JSON module
Post by: John 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?
Title: Re: Scriptbasic: JSON module
Post by: AIR 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.
Title: Re: Scriptbasic: JSON module
Post by: AIR 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.
Title: Re: Scriptbasic: JSON module
Post by: John 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.
Title: Re: Scriptbasic: JSON module
Post by: John 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$

Title: Re: Scriptbasic: JSON module
Post by: AIR on November 26, 2018, 08:48:08 PM
Without seeing any code, not much I can tell you.

It works on my 64bit Linux box:

riveraa@nas:~/src/sb_ubuntu-64$ ./AppRun parse2.bas
----------------------------------------
Joe Blow
56
Tarrytown
NY
10891
----------------------------------------
John Smith
86
Cupertino
CA
N/A
----------------------------------------
Title: Re: Scriptbasic: JSON module
Post by: John on November 26, 2018, 08:55:04 PM
I didn't touch any of your source or example code. I just compiled it under my Linux source tree.

I remember a bug in Peter's ARGUMENT macro with certain type combinations. I'm not sure if "pz" was one of them. The bug would return the same error I got. I had to use the old school method to get around it.

Maybe you fixed this in another life on your box.
Title: Re: Scriptbasic: JSON module
Post by: AIR on November 26, 2018, 09:06:33 PM
No, I didn't alter any of the Macro stuff.

It works here on both macOS and Ubuntu 64.
Title: Re: Scriptbasic: JSON module
Post by: John on November 26, 2018, 09:09:54 PM
That's what I'm running. Ubuntu 18.10.

Would you zip up your .so and SB code and post it so I can give it a try?
Title: Re: Scriptbasic: JSON module
Post by: AIR on November 26, 2018, 09:32:31 PM
Attached.