D is one of my favorite programming languages for all the usual reasons. It’s a fast language — comparable with c++, but still provides the conveniences I’ve come to expect having started on languages like java and PHP.

You get great (almost pythonic) list comprehensions, built in facilities for documentation, garbage collection,and (my personal favorite) unit-testing tools right out of the box. On the other side of the spectrum, you get static typing, optional manual memory-management and a powerful template system.

When I first learned about D, I dove into a few huge projects that were way beyond me at the time. I never finished any of them, but I did end up implementing a small JSON tool as part of my efforts. At the time, phobos’s (the D standard library’s) JSON module wasn’t all there.

Let’s look at some D — here’s the unit-test I wrote for my JSON class

unittest{
   writefln("beginning unittest for lazyjson.d\n\n");

   writefln("Test 1.0--parsing");
   string jsonstring = "{\"name\":\"Lee Avital\", \"siblings\":[\"Lori\", \"Abigail\"]}";
   writefln("We are parsing the string: %s", jsonstring);

   JSON j = new JSON(jsonstring);
   if(j) writefln("success!\n");
   else writefln("failure\n");


   writef("Test 1.1: shallow retrieval...");
   string name = j.getString("name");
   if(name == "Lee Avital"){ writef("success!\n"); }
   else{ writef("failure\n"); }


   writef("Test 1.2: mid-level retrieval...");
   string[] siblings = j.getStringArray("siblings");
   if(siblings[0] == "Lori" && siblings[1] == "Abigail"){ writef("success!\n"); }
   else{ writef("failure\n"); }


   writef("Test 1.3: shallow extending...");
   j.extend("mynum", 2);
   if(j.getDouble("mynum") == 2){ writef("success\n"); }
   else{ writef("failure\n"); }

   writef("Test 1.4: deep extending...");
   j.extend("myarr", "[2,3,4]");
   int[] arr = j.getIntArray("myarr");
   if(arr[0] == 2 && arr[1] == 3 && arr[2] == 4){
      writef("success\n\n\n");
   }
   else{
      writef("failure\n\n\n");
   }


   jsonstring = "{\"name\":\"My App\", \"permissions\":[\"google.com\", \"facebook.com\"], \"browser_action\":{\"icon\":\"icon.png\", \"caption\":\"click me!\"}}";
   writefln("Test 2.0: parsing");
   writefln("Parsing the string: %s", jsonstring);
   j = new JSON(jsonstring);
   if(j){ writefln("success!\n"); }
   else{ writefln("failure\n"); }

   writef("Test 2.1: shallow retrival...");
   // name already declared.
   name = j.getString("name");
   if(name == "My App"){ writef("success!\n"); }
   else{ writef("failure\n"); }


   writef("Test 2.2: mid-level retrival...");
   string[] arr2 = j.getStringArray("permissions");
   if(arr2[0] == "google.com" && arr2[1] == "facebook.com"){
      writef("success!\n");
   }
   else{
      writef("failure\n");
   }



   writef("Test 2.3: deep retrival...");
   JSON jLow = j.getJSON("browser_action"); // lol, jay-lo
   if(jLow.getString("icon") == "icon.png" && jLow.getString("caption") == "click me!"){
      writef("success!\n");
   }
   else{
      writef("failure\n");
   }

}