JSON Parser in D

For users of dfns, both novice and expert

JSON Parser in D

Postby paulmansour on Fri Apr 05, 2013 1:39 pm

Hi all,

So yesterday I decided I needed a JSON parser.

(If you are not familiar with JSON, I highly suggest you check it out. I think it a much better alternative to XML in many cases, and it has namespaces (objects) and arrays so basic APL structures are easily created, with no need to create tags like you do in XML)

Anyway, as I was coding it up in D, I remembered I had seen some JSON code somewhere in the MiServer probject, and sure enough there is one. I was almost done, so I just finished my D code, and then compared the two. Now the MiServer JSON parser covers a lot more (like escape chars and what not), and my D code is very simple first pass at the problem.

My question is about the overall approach. The MiServer implementation appears to tokenize the entire JSON string, and then step through each token. Not really knowing what the heck I'm doing, I just parse the top level container, and then recursively look at each value until you get down to strings and numbers. Is this a naive approach? Does the tokenization deliver something important that I am missing? Again, I realize that my code is missing a lot of the detail like handling escape chars and error checking, but is the overall approach valid?

Any comments or thoughts will be appreciated.

You can find the MiServer code in the JSON namespace of the MiServer workspace (You need to run the sample browser once before the JSON namespace is loaded.)

Here is my D code:

Code: Select all
Parse←{                           ⍝ Parse JSON
     ⎕IO ⎕ML←0 3
     dlb←{(∨\' '≠⍵)/⍵}
     dtb←{⌽dlb⌽⍵}
     Partition←{
         p←'[]',[0.5]'{}'
         k←∧⌿=⌿+\p∘.=⍵
         (~k∧⍵=',')⊂⍵
     }
     ParseString←{
         q←''''
         q=↑⍵:⍎⍵
         ⍎q,(¯1↓1↓⍵),q
     }
     ParseValue←{                  ⍝ Parse JSON Value
         j←dlb dtb ⍵               ⍝ Delete ltb
         c←↑j                      ⍝ First char
         c='{':ParseObject ⍵       ⍝ Object
         c='[':ParseArray ⍵        ⍝ Array
         c∊'"''':ParseString ⍵     ⍝ String
         ↑1⊃⎕VFI ⍵                 ⍝ Number
     }
     ParseObject←{                 ⍝
         j←¯1↓1↓⍵                  ⍝ Peal off braces
         nv←Partition j            ⍝
         n v←↓⍉⊃{(~<\':'=⍵)⊂⍵}¨nv  ⍝ Split each Name from Value
         v←ParseValue¨v            ⍝ Parse each Values
         p←⎕NS''                   ⍝ Make a space
         p⊣n p.{⍎⍺,'←⍵'}¨v         ⍝ Inject the names
     }
     ParseArray←{                  ⍝ Parse a JSON array
         j←¯1↓1↓⍵                  ⍝ Peal off brackets
         v←Partition j             ⍝
         ParseValue¨v              ⍝ Parse each value
     }
     ParseValue ⍵
 }
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: JSON Parser in D

Postby DanB|Dyalog on Sat Apr 06, 2013 7:32 am

There is a namespace in SALT with utilities to convert XML & APL to/from JSON.
It is mostly written in T-fns and is found in SALT/tools/Inet/json.dyalog
It was taken from MiServer and was written mostly by Brian.

Your approach is certainly valid and the XML <--> JSON section in SALT does that too.
DanB|Dyalog
 


Return to Functional Programming

Who is online

Users browsing this forum: No registered users and 1 guest