Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to use custom but failing #56

Open
Aphexus opened this issue May 25, 2019 · 3 comments
Open

Trying to use custom but failing #56

Aphexus opened this issue May 25, 2019 · 3 comments

Comments

@Aphexus
Copy link

Aphexus commented May 25, 2019

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
Copy link
Author

Aphexus commented May 25, 2019

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
Copy link
Author

Aphexus commented May 26, 2019

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
Copy link
Author

Aphexus commented May 26, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant