Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Easy ways to load/save D arrays in JSON

Moderators: kris

Posted: 05/01/09 13:52:07 Modified: 05/01/09 13:53:10

Something like this:

int i;
int[100] array; 
int[100][100][10] bigArray;

auto json = new Json!(char);

with (json) {
    value = object (pair("i", value(i)));
    value = object (pair("array", array(array));
    value = object (pair("bigArray", array(bigAray)); 

Thanks :)

Author Message

Posted: 05/01/09 16:15:47

can you suggest a way to implement this?

Posted: 05/03/09 02:07:36

I'm trying to understand what is happening when you create a Json array. Each element gets type checked and then concatenated to the list Lets not do it like that.. that is horribly inefficient when you know the type of the array :)

How about adding an extra type? And two functions? one which only holds the reference and one .dup version.

I don't grasp the code well enough to see what kind of things need implementing, sorry :)

Posted: 05/03/09 03:55:00

this link might be of help

Posted: 05/03/09 04:50:21

I meant the Tango code or are you suggesting to check other implementations to see how they did it? Maybe I am able to read the C or C++ code a bit, but I can read D better. I think that when I understand the tango code, adding a function to save d arrays to a json array shouldn't be too difficult. I still think it needs a new type public enum Type {Null, String, RawString?, Number, Object, Array, Darray True, False}; This would of course only be for internal use. The parser should be able to read these arrays as such as well, which might be a bit of a problem. Maybe take an array to be of a single type by default and switch when finding a conflicting element.

Posted: 05/03/09 16:21:43

The more I understand about the Json implementation the more I see it isn't really made to efficiently save D arrays.
Especially parsing such arrays is a pain.
Does tango support some kind of serialization format which is tailored for D types?

(just learned BR :D)

Posted: 05/03/09 16:36:01

Tango hasn't settled for a serialization framework yet (D 1.0 isn't extremely suited for such excercices), but you can look at blip which do have some serialization stuff on top of Tango.

Posted: 05/03/09 22:21:34 -- Modified: 05/03/09 22:22:03 by

If you don't care about the more complex(deep) serialization like structs and classes, it should be very easy:
Use the the D format.

Posted: 05/04/09 18:40:04

Yes ... but structs and classes are the interesting aspects of serialization.

Posted: 05/04/09 20:06:21

Of course they are interesting, but does that mean there shouldn't be a easy way to save D arrays as long as the interesting problems haven't been solved?

It is probably peanuts for Tango to implement something like the ddata module suggested on D.learn :D

int number = 10;

float [3][2]  simpleArray = [
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6]];

static import ddata;
void main()
char[][] file = read (`file.dat`);
int i;
char c;

ddata.get(file,'number',c); //type fail, thus returns -1 or throws an 

float [3][2] a;

ddata.write(file,'simpleArrayCopy', a);

a[0][0] =3.0;

ddata.write(file,'simpleArray', a);

write('file.dat', file);

resulting data file:

int number = 10;

float [3][2]  simpleArray = [
[3.0, 0.2, 0.3],
[0.4, 0.5, 0.6]];

float [3][2]  simpleArrayCopy = [
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6]];

Posted: 05/08/09 17:16:25

well blip serialization does all this, and you can even change the format by changing the serializer :)


Posted: 05/11/09 06:16:30

I could be wrong, but doesn't the serializer use JSON and isn't JSON inherently inefficient in saving large (1M) single typed arrays?

Also, the documentation is a bit lagging ;)