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

Object serialize

Moderators: larsivi kris

Posted: 04/22/08 21:24:38

I have an object that contains arrays of objects and I need to save and load these objects from file.

I've been trying to serialize these objects to file but I'm getting errors..

Table.d(185): function tango.io.protocol.model.IReader.IReader.get (bool) does not match parameter types (item[]) Table.d(185): Error: cannot implicitly convert expression (this.order) of type item[] to void delegate(IReader) Table.d(185): function tango.io.protocol.model.IReader.IReader.get (bool) does not match parameter types (item[][]) Table.d(185): Error: cannot implicitly convert expression (this.data) of type item[][] to void delegate(IReader) Table.d(191): function tango.io.protocol.model.IWriter.IWriter.put (bool) does not match parameter types (item[]) Table.d(191): Error: expected 0 arguments, not 1 Table.d(191): function tango.io.protocol.model.IWriter.IWriter.put (bool) does not match parameter types (item[][])

class item
{
	//column name, attribute...
	char[] name;
	//vlaue as int, length as string
	int ival;
	//string values
	char[] sval;
	//is this an int?
	bool isint;

	//..class funcs not important
	
	void read(Reader input)
	{
		input (name) (ival) (sval) (isint);	
	}
	
	void write(Writer output)
	{
		output (name) (ival) (sval) (isint);	
	}
}

/**
* TokenNode
*	Returned for next command
*	contains token string and Type
*/
class Table
{
	private char[] name; //table name
	
        // table data, holds the rows..
	item[][] data;
	// row prototype, in order..
	item[]   order;

	//...class funcs not important
	
	void read(Reader input)
	{
		input (name) (order) (data);	//185
		
	}
	
	void write(Writer output)
	{
		output (name) (order) (data);	// 191
		
	}
}

Any suggestions, commits... I'm not even using these functions yet... just trying to compile them in the classes

-Byron

Author Message

Posted: 04/22/08 21:44:19

Changed table class to this

	void read(Reader input)
	{
		int orderlen, rowlen;
		item tmp;
		
		input (name) (orderlen) (rowlen);
		
		for(int i = 0; i < orderlen; ++i)
		{
			tmp.read(input);
			order[i] = new item(tmp);
		}
		
		for(int r = 0; r < rowlen; ++r)
		{
			for(int c = 0; c < orderlen; ++c)
			{
				tmp.read(input);
				data[r][c] = new item(tmp);
			}
		}
		
	}
	
	void write(Writer output)
	{
		output (name) (order.length) (data.length);
		
		foreach(o; order)
			o.write(output);
			
		foreach(r; data)
			foreach(c;r)
				c.write(output);
		
	}

Compiles... now to see if it doead like it should..

-B

Posted: 04/24/08 17:40:57

just not working.... can't find what I'm missing....

This is my saving function

void savetables()
{
	foreach(t; tables)
	{
		auto fp = new FilePath(t.location);
		fp.native;
		debug Stdout("Saving Table: ")(fp.path~fp.file).newline;
		
		auto fc = new FileConduit (fp.path~fp.file, FileConduit.WriteCreate);
		
		
		auto write = new Writer(fc);
		
	
		t.write(write);
                // write (t);  //gives compile errors.....

		fc.close();
		//write (t);
	}
}

You can see the complete code here http://rockydog.no-ip.org:8080/svn/mydbms/

Posted: 04/24/08 22:41:56

What exactly is not working?

  • Are the files written?
  • Is the content correct? (check with hex editor)
  • Can you reduce the example to write one 'item', then load it, then extend to write/read one Table?

Posted: 04/25/08 01:53:42

First I cant get it to act like its suppose too..

http://www.dsource.org/projects/tango/forums/topic/167

and its not writing anything to file...

Posted: 04/25/08 06:46:27

t.write(write);
// write (t);  //gives compile errors.....

To make the second line compile, t must implement the interface IWritable.

But the first line should work also. To find the problem its best to reduce it to the most trivial case. I copied this from your code above without trying to compile...

auto fc = new FileConduit ("test.dat", FileConduit.WriteCreate);
auto write = new Writer(fc);
int value = 0x44556677;
write(write);
fc.close();

Does this work? Does it produce a file containing the value?

Posted: 04/27/08 14:44:09

        auto fc = new FileConduit ("test.dat", FileConduit.WriteCreate);
	auto write = new Writer(fc);
	int value = 0x44556677;
	write(value);
	fc.close();

All I get is an empty file... Not being flushed somehow...?

Posted: 04/27/08 15:02:07

Ok so it wasn't flushing.. added a writer.flush; maybe this should be better documented somewhere...

        auto fc = new FileConduit ("test.dat", FileConduit.WriteCreate);
	auto write = new Writer(fc);
	int value = 0x44556677;
	write(value);
	write.flush;
	fc.close();

Posted: 04/29/08 16:11:41

Well the code work on windows with dmd but segfaults on Ubuntu x64 with gdc

        auto fc = new FileConduit ("test.dat", FileConduit.WriteCreate);
	//getting a segfault on the next line, fc is okay...
        auto write = new Writer(fc);
	
        int value = 0x44556677;
	write(value);
	write.flush;
	fc.close();