Shall I use std.json at my own risks ?

jean christophe cybrarian at live.fr
Wed Nov 13 00:08:18 PST 2013


On Wednesday, 13 November 2013 at 06:16:07 UTC, Rob T wrote:

>> I need my Gtkd application to maintain a (possibly big) 
>> archive database of financial records downloaded daily from 
>> the server application. In my case JSON seems to be the most 
>> convenient format. Please let me know if, according to you, 
>> std.json will cast aside as std.xml.

...

> I guess that I'm saying is that while std.json is rock solid 
> and very fast, you may want to consider better alternatives to 
> the json format unless there's a technical reason why json must 
> be used.
>
> Have fun :)
>
> --rt

Well first thank you for sharing your experiences.

You mentioned that a) std.json is solid and fast and b) it's not 
due to be deprecated. You've really helped me to make my choice. 
I'm going to use that module. It'd be easier to implement the 
retrieval of data from the server application side which is 
written in PHP. For example, a simple 
`json_encode($bigDataObject)` would be fair enough to send data 
to the desktop application.

I agree that the API std.json is not sexy. But if it is reputed 
solid and fast, why just not keep it, gently fix possible bugs, 
and for those who need more fancy access to the JSON data, wrap 
it in some kind of std.json.helper or similar extension. Jonathan 
mentioned above that it is not range-based which is a lack as 
ranges are one of the paradigms of D. IMO, it's important to have 
a stable standard library onto which one can build real 
applications programs in D. Too much forking is bad.

BTW I've tested the use of std.json with import std.parallelism 
and it works. It's a pretty good news. The example below is 
borrowed from Ali.

import std.stdio;
import std.json;
import std.conv;
import std.file;
import std.random;
import std.parallelism;
import core.thread;

struct Employee
{
   int id;
   string firstName;
   string lastName;

   void initStuf(JSONValue employeeJson)
   {
     writefln("Start long task for Employee %s", id);
     JSONValue[string] employee = employeeJson.object;		
     firstName = employee["firstName"].str;
     lastName  = employee["lastName"].str;
     Thread.sleep(uniform(1, 3).seconds); // Wait for a while
     writefln("Done long task for Employee %s", id);
   }
}

void main()
{
   auto content =
     `{"employees": [
        { "firstName":"John" ,   "lastName":"Doe"},
        { "firstName":"Anna" ,   "lastName":"Smith"},
        { "firstName":"Peter" ,  "lastName":"Jones"},
        { "firstName":"Kim" ,    "lastName":"Karne"},
        { "firstName":"Yngwee" , "lastName":"Malmsteen"},
        { "firstName":"Pablo" ,  "lastName":"Moses"}		  ]
       }`;

   JSONValue[string] document = parseJSON(content).object;
   JSONValue[] employees = document["employees"].array;
   uint employeeId = 0;

   foreach (employeeJson; parallel(employees))
     {
       auto e = Employee( employeeId++ );
       e.initStuf( employeeJson );
     }
}

Gives :

Start long task for Employee 0
Start long task for Employee 4
Start long task for Employee 5
Start long task for Employee 1
Start long task for Employee 3
Start long task for Employee 2
Done long task for Employee 4
Done long task for Employee 5
Done long task for Employee 1
Done long task for Employee 3
Done long task for Employee 0
Done long task for Employee 2










More information about the Digitalmars-d mailing list