Author Topic: JSON Challenge  (Read 17242 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #45 on: January 05, 2019, 01:38:15 AM »
Here's a quick and dirty example using an unreleased version of JADE, along with JUTE:

Code: C++
  1. #include <jade.hpp>
  2. #include "jute.h"
  3.  
  4. MAIN
  5.     using namespace jute;
  6.     STRING str(LOADFILE$("test.json"));
  7.     jValue data = parser::parse(str);
  8.     PRINT( data["Person"]["Name"].to_string() );
  9.     PRINT( data["Person"]["Age"].to_string() );
  10.     PRINT( data["Person"]["Hobbies"][0].to_string() );
  11.     PRINT( data["Person"]["Hobbies"][1].to_string() );
  12.     PRINT( "\n", data.to_string());
  13. END
  14.  

$ jade jtest.cc jute.o -ljade -o jtest
$ ./jtest
"Homer"
39
"Eating"
"Sleeping"

{
  "Person": {
    "Name": "Homer",
    "Age": 39,
    "Hobbies": ["Eating", "Sleeping"]
  }
}

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #46 on: January 05, 2019, 09:40:01 AM »
I'm glad you're still on the JADE path. The JUTE json library looks easier to use than the Parson library.

Can SB use C++ libraries to create extension modules?
« Last Edit: January 05, 2019, 12:21:29 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #47 on: February 27, 2019, 01:11:19 AM »
Since I'm learning GO, here is a GO version of the original challenge:

Code: Go
  1. package main
  2.  
  3. import (
  4.         "encoding/json"
  5.         "fmt"
  6.         "io/ioutil"
  7.         "os"
  8.         "strings"
  9. )
  10.  
  11. func main() {
  12.  
  13.         var jsonData []interface{}
  14.  
  15.         jsonFile, err := os.Open("commits.json")
  16.  
  17.         if err != nil {
  18.                 fmt.Println(err)
  19.                 os.Exit(1)
  20.         }
  21.  
  22.         defer jsonFile.Close()
  23.  
  24.         jsonBytes, _ := ioutil.ReadAll(jsonFile)
  25.  
  26.         if err = json.Unmarshal(jsonBytes, &jsonData); err != nil {
  27.                 fmt.Println(err)
  28.                 os.Exit(1)
  29.         }
  30.  
  31.         for _, item := range jsonData {
  32.                 fmt.Println(strings.Repeat("-", 50))
  33.                 entry := item.(map[string]interface{})
  34.                 commit := entry["commit"].(map[string]interface{})
  35.                 author := commit["author"].(map[string]interface{})
  36.                 fmt.Println("Name:", author["name"])
  37.                 fmt.Println("Commit Date:", author["date"])
  38.                 fmt.Println("SHA Hash:", entry["sha"])
  39.         }
  40.         fmt.Println(strings.Repeat("-", 50))
  41. }
  42.  

GO is a really nice language, though it's primary focus is web / server / commandline oriented.

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #48 on: February 27, 2019, 08:00:38 AM »
Thanks AIR for the GO submission!

I was getting worried you retired again.  :)

Actually MySQL turned out to be a great JSON parser for the project I'm working on.
« Last Edit: February 27, 2019, 08:04:42 AM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #49 on: February 28, 2019, 10:08:33 PM »
Go has the built-in capability of mapping json directly to fields in a structure.  Additionally, you can specify that only specific fields are mapped, ignoring all others.

Here is what using this feature looks like:

Code: Go
  1. package main
  2.  
  3. import (
  4.         "encoding/json"
  5.         "fmt"
  6.         "io/ioutil"
  7.         "os"
  8.         "strings"
  9. )
  10.  
  11. type JsonData []struct {
  12.         Sha    string `json:"sha"`
  13.         Commit struct {
  14.                 Author struct {
  15.                         Name string `json:"name"`
  16.                         Date string `json:"date"`
  17.                 } `json:"author"`
  18.         }
  19. }
  20.  
  21. func main() {
  22.         var jsonData JsonData
  23.  
  24.         jsonFile, err := os.Open("commits.json")
  25.  
  26.         if err != nil {
  27.                 fmt.Println(err)
  28.                 os.Exit(1)
  29.         }
  30.  
  31.         defer jsonFile.Close()
  32.  
  33.         jsonBytes, _ := ioutil.ReadAll(jsonFile)
  34.  
  35.         if err = json.Unmarshal(jsonBytes, &jsonData); err != nil {
  36.                 fmt.Println(err)
  37.                 os.Exit(1)
  38.         }
  39.  
  40.         for _, item := range jsonData {
  41.                 fmt.Println(strings.Repeat("-", 50))
  42.                 fmt.Println("Name:", item.Commit.Author.Name)
  43.                 fmt.Println("Commit Date:", item.Commit.Author.Date)
  44.                 fmt.Println("SHA Hash:", item.Sha)
  45.         }
  46.         fmt.Println(strings.Repeat("-", 50))
  47. }
  48.  

I think that, for the most part, someone not familiar with go can look at the code above and have a decent understanding of what it's doing.

The more I study go, the more I like it...

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #50 on: February 28, 2019, 10:31:01 PM »
GO looks pretty bare bones and to do anything you need to include a bunch of libraries. Am I missing something?

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: JSON Challenge
« Reply #51 on: February 28, 2019, 11:01:14 PM »
It's a small core with a ton of available PACKAGES in the standard distribution.

I like it's modular nature vs the monolithic approach.  I also like the fact that I can develop on my Mac, and then optionally cross-compile to Linux and Windows from within macOS without having to install additional tool-chains to do so.


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: JSON Challenge
« Reply #52 on: February 28, 2019, 11:13:35 PM »
A nice set of extensions.  The cross platform feature is a big plus.