Author Topic: JSON Challenge  (Read 17238 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #15 on: November 25, 2018, 01:45:29 AM »
My comments are based on what you've stated before about wanting to create a json module for SB.

As a module, your current code won't hold up.

I could have done the same as you and written something that would meet the criteria in just a few lines of code.  But I'm looking to actually re-use what I've coded, and doing it like you've done would not have allowed me to do that without having to change it for each use.

At any rate, you've met the challenge requirements.  If that's enough for you, then that's fine.  But as long as you've known me, I've always tried to push you past the point of "it's good enough".

AIR.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #16 on: November 25, 2018, 02:08:06 AM »
PYTHON VERSION

Code: Python
  1. import json
  2.  
  3. datafile = open("commits.json").read()
  4. for item in json.loads(datafile):
  5.     print "-"*40
  6.     print item["commit"]["author"]["name"]
  7.     print item["commit"]["author"]["date"]
  8.     print item["sha"]
  9. print "-"*40
  10.  

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #17 on: November 25, 2018, 02:28:41 AM »
I'll submit another entry using the jsmn library in SB.

Offline jalih

  • Advocate
  • Posts: 111
Re: JSON Challenge
« Reply #18 on: November 25, 2018, 02:51:14 AM »
I'm waiting on jalih to chime in.   ;D ;D

Following 8th code should do the trick:

Code: [Select]
"commits.json" f:slurp json> var, data

data @ ( nip "commit" m:@ "author" m:@ nip "name" m:@ . cr "date" m:@ nip . cr "sha" m:@ nip . cr "-" 50 s:* . cr ) a:each drop

Offline jalih

  • Advocate
  • Posts: 111
Re: JSON Challenge
« Reply #19 on: November 25, 2018, 08:48:02 AM »
Here is a little bit fancier version using simple GUI for displaying data.

Code: [Select]
true app:isgui !

"commits.json" f:slurp json> var, data
a:new var, items
var gui

{
  "kind" : "win",
  "buttons" : 5,
  "title" : "JSON Challenge",
  "wide" : 640,
  "high" : 480,
  "center" : true,
  "children" :
  [
    {
      "kind" : "table",
      "name" : "table",
      "bg" : "lightgreen",
      "hl" : "lightgray",
      "hlc" : "gray",
      "bounds" : "10,10,parent.width-10, top+460",
      "header" : [
                   { "name" : "Name of committer", "width":160 },
                   { "name" : "Date of commit", "width":160 },
                   { "name" : "The SHA HASH for commit", "width":280 }
                 ]
    }
  ]
} var, gui-desc


: app:main
  data @ ( nip "commit" m:@ "author" m:@ nip "name" m:@ swap "date" m:@ nip rot "sha" m:@ nip 3 a:close items @ swap a:push drop ) a:each drop
  gui-desc @ "children" m:@ nip 0 a:@ nip "items" items @ m:! drop
  gui-desc @ g:new gui ! ;
« Last Edit: November 25, 2018, 08:58:32 AM by jalih »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #20 on: November 25, 2018, 11:52:54 AM »
Very Nice, Jali!!!!!

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #21 on: November 25, 2018, 12:02:51 PM »
Why these challenges are so beneficial to all.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #22 on: November 25, 2018, 02:16:25 PM »
Quote from: AIR
My comments are based on what you've stated before about wanting to create a json module for SB.

My almost instantaneous submission was based on my comment about writing an XML parser in 10 lines.

I feel it would be more beneficial to release your json (jsmn) extension module in a Linux and Windows version.


Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #23 on: November 25, 2018, 04:27:47 PM »
That's your call, it's not quite finished yet but the core is there.  You could put it out to get feedback, at least.

I actually prefer the way Parson implements the api for json parsing.  It's much easier to port and use, as the HB submission here shows.

Had I found it earlier, I would have based the SB JSON module on it.

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #24 on: November 25, 2018, 05:25:14 PM »
Thanks for the update on your current SB json efforts. I'll give Parson a peek before generating a json extension module.

I might use DLLC's FFI (DYC on steroids)  under Windows to prototype the libparson SB interface before doing it in C BASIC as a json extension module.

While on the FFI topic, that would be a nice code challenge. I would love to turn Charles's DLLC FFI into a C extension module that is cross platform.
« Last Edit: November 25, 2018, 06:02:46 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #25 on: November 25, 2018, 06:58:54 PM »
Code: ScriptBasic
  1. INCLUDE json.bas
  2.  
  3. jarray = json::load("commits.json")
  4. for i = 0 to json::count(jarray)-1
  5.     obj = json::object(jarray,i)
  6.     print string(40,"-"),"\n"
  7.     print json::text(obj,"commit.author.name"),"\n"
  8.     print json::text(obj,"commit.author.date"),"\n"
  9.     print json::text(obj,"sha"),"\n"
  10. next
  11. print string(40,"-"),"\n"
  12.  

[riveraa@MacDev ~/tmp/sb/tests] $ scriba parse.bas
----------------------------------------
Linus Torvalds
2018-11-24T20:58:47Z
e195ca6cb6f21633e56322d5aa11ed59cdb22fb2
----------------------------------------
Linus Torvalds
2018-11-24T17:42:32Z
d146194f31c96f9b260c5a1cf1592d2e7f82a2e2
----------------------------------------
Linus Torvalds
2018-11-24T17:19:38Z
857fa628bbe93017c72ddd0d5304962a2608db07
----------------------------------------
Linus Torvalds
2018-11-24T17:11:52Z
abe72ff4134028ff2189d29629c40a40bee0a989
----------------------------------------
Andreas Fiedler
2018-11-23T23:16:34Z
07093b76476903f820d83d56c3040e656fb4d9e3
----------------------------------------
Quentin Schulz
2018-11-23T18:01:51Z
3fa528b7682e73e906266bcd43728b8f923bf9b2
----------------------------------------
Fabio Estevam
2018-11-23T17:46:50Z
e7b9fb4f545b1f7885e7c642643828f93d3d79c9
----------------------------------------
Lorenzo Bianconi
2018-11-23T17:28:01Z
ef2a7cf1d8831535b8991459567b385661eb4a36
----------------------------------------
Yangtao Li
2018-11-22T12:34:41Z
c44c749d3b6fdfca39002e7e48e03fe9f9fe37a3
----------------------------------------
Hangbin Liu
2018-11-22T08:15:28Z
5ed9dc99107144f83b6c1bb52a69b58875baf540
----------------------------------------
Jason Wang
2018-11-22T06:36:31Z
18ba58e1c234ea1a2d9835ac8c1735d965ce4640
----------------------------------------
Jason Wang
2018-11-22T06:36:30Z
e59ff2c49ae16e1d179de679aca81405829aee6c
----------------------------------------
Linus Torvalds
2018-11-23T19:24:55Z
7c98a42618271210c60b79128b220107d35938d9
----------------------------------------
Linus Torvalds
2018-11-23T19:20:14Z
3381918fec9278d14f776d1dabd68da85fd6822e
----------------------------------------
Davide Caratti
2018-11-21T17:23:53Z
484afd1bd3fc6f9f5347289fc8b285aa65f67054
----------------------------------------
Paolo Abeni
2018-11-21T17:21:35Z
605108acfe6233b72e2f803aa1cb59a2af3001ca
----------------------------------------
Hangbin Liu
2018-11-21T13:52:33Z
896585d48e8e9ba44cd1754fbce8537feffcc1a5
----------------------------------------
Linus Torvalds
2018-11-23T19:15:27Z
d88783b9c8849d88c3a75b7b9071cba072b47eba
----------------------------------------
Willem de Bruijn
2018-11-20T18:00:18Z
5cd8d46ea1562be80063f53c7c6a5f40224de623
----------------------------------------
Linus Torvalds
2018-11-23T18:56:16Z
a03bac580ae743d5900af626ac63f7f8cd85def9
----------------------------------------
Linus Torvalds
2018-11-23T18:52:57Z
b88af994872418f0a98db6f4a9bae849315a99b0
----------------------------------------
Will Deacon
2018-11-21T15:07:00Z
4f9f49646a5733c0c2bd49940673dde89a9c5add
----------------------------------------
Linus Torvalds
2018-11-23T18:40:19Z
e6005d3c42336074de3745718ac85807dd6e1e6a
----------------------------------------
Linus Torvalds
2018-11-23T18:36:02Z
dcd3aa31dcdd6d8eae8d4771c44aeb3b1fec995a
----------------------------------------
Linus Torvalds
2018-11-23T18:03:08Z
9b7c880c834c0a1c80a1dc6b8a0b19155361321f
----------------------------------------
Sergey Matyukevich
2018-11-16T18:21:30Z
b5d9a07ef7736b2456b9d3c90568de25e43d8ec3
----------------------------------------
Rafael J. Wysocki
2018-11-23T09:32:49Z
1d50088ca3956e5dcd2751a658e7869b9af10bb4
----------------------------------------
Rafael J. Wysocki
2018-11-23T09:32:22Z
bec00cb5e97c19d2c8bd10db15d334cb40760000
----------------------------------------
Dave Airlie
2018-11-23T01:03:20Z
98c9cdfd34fbb62886e4c5a07e33661aa3352ef5
----------------------------------------
David S. Miller
2018-11-22T19:53:26Z
039e70a70c8417b5bf5878a60612ebd2c95f731e
----------------------------------------


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #26 on: November 25, 2018, 07:04:10 PM »
This is why they pay you the BIG bucks.

Thanks for your json extension module contribution!

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #27 on: November 25, 2018, 07:10:31 PM »
I threw this together quickly to help get you started....it's in my repository, try it against a few json files before releasing to testing....

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #28 on: November 25, 2018, 07:16:50 PM »
Okay.

I can't stop staring at your SB json example.

Sweet!
« Last Edit: November 25, 2018, 07:57:33 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #29 on: November 25, 2018, 09:11:05 PM »
Thanks.

What I like about the Parson code is that it's free as in free.  MIT license, just need to include attribution and license.  I've been focused on finding stuff like this this is either a single header or a single header/single C file combo.

At some point, you might want to take a look at CLIB, which is an index/repository for stuff like this.  Some of the string stuff there could flesh out your C-BASIC project....I've been trying to find a good hash-table implementation, which C sorely lacks...

AIR.