orange icon indicating copy to clipboard operation
orange copied to clipboard

Trying to use custom but failing

Open Aphexus opened this issue 6 years ago • 3 comments
trafficstars

I have a custom class that is virtually identical to the following:

class X { // add toData/fromData}

class Y(T) : X { T Data; this(T d) { Data = d; }

override void toData(Serializer serializer, Serializer.Data key) inout
{		
		serializer.serialize(Data, "Data");
	return;
}

override void fromData(Serializer serializer, Serializer.Data key)
{		
	Data = serializer.deserialize!(T)("Data");
	return;
}

}

But when I run the code, during deserialization I get the error

Orange\serialization\Serializer.d(1274): Failed to unarchive object of type 'X' with key '0'

The reason I have X and Y is because I can't register Y for every possible type, if I just use Y with no X involved then I get errors about registering Y. Ideally I'd want to use Y!T directly because it would make coding much easier(I won't have to manually figure out the type from X and cast).

I figured that I'd use a base class as a common type and just serialize it. Everything seems to work but the deserialization process seems to fail and I can't figure out why.

The serialized data is:

string TESTING

Everything looks right except the runtime type: runtimeType="mX.Y!string.Y"

Seems there is an extra Y appended to the end.

Although remove the .Y in the xml fixes nothing.

If I remove the toData and fromData

"The object of the static type "inout(mX.X)" have a different runtime type (mX.Y!string.Y) and therefore needs to either register its type or register a serializer for its type "mX.Y!string.Y".

I'm not sure if I'm doing something wrong or what. Any ideas what it could be?

Aphexus avatar May 25 '19 20:05 Aphexus

The error seems to happen on the

<object type="inout(mX.cX)" key="0"

line

It's failing to unarchive but I have no idea why ;/

Aphexus avatar May 25 '19 21:05 Aphexus

Here is a reduced test case that seems to also exhibit the same issue:

module Issue1;

import std.stdio;

import orange.serialization.Serializable : nonSerialized; import std.file; import orange.serialization., orange.serialization.archives.;

class C { int x = 53; }

class Y(T) : C { T x;

} int main() {

auto archive = new XmlArchive!(char); // create an XML archive
auto serializer = new Serializer(archive); // create the serializer

serializer.register!(C);
serializer.register!(Y!int);
serializer.register!(Y!string);

C c = new C();

C[] ya;
auto yi = new Y!int;
yi.x = 22;
auto ys = new Y!string;
ys.x = "sfda";
ya ~= yi;
ya ~= ys;



auto slfp = `test.xml`;
if (!false && exists(slfp))
{	

	auto f = std.file.read(slfp);
	archive.beginUnarchiving(cast(immutable(void)[])f);
	auto ud = archive.untypedData;

	ya = serializer.deserialize!(C[])(ud);

} else
{	
	// write xml file
	archive.beginArchiving();
	std.file.write(slfp, cast(byte[])serializer.serialize(ya));
}


return 0;

}

Aphexus avatar May 26 '19 17:05 Aphexus

and specifically the code

Object newInstance (string name) { auto classInfo = ClassInfo.find(name);

if (!classInfo)
    return null;

return newInstance(classInfo);

}

is not able to create the instance of the templated class.

Aphexus avatar May 26 '19 17:05 Aphexus