serialization and deserialization problem

  • Thread starter Thread starter AnkitAsDeveloper [Ankit]
  • Start date Start date
A

AnkitAsDeveloper [Ankit]

Hi i am serializing a 'ref struct' object as follows :



private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}

private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;

}

// now i am serializing it in follwing way...

WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;

Seri( "a.waypoint", waypoint1 );

//Serialization works fine.... but following deserialization throws me
exception...

WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;

// Exception :
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.

// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}

WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}

Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.


[Ankit Jain]
http://www.ankitjain.info/ankit/
 
DateTime is a value type, remove the ^ from...

DateTime Arrival;
DateTime Departure;

WayPoint2 ( LatLog ^latlog, String ^name, String ^description, DateTime
arrival, DateTime departure)

Willy.
 
Thankx for replying.

The problem is solved. The deserialization works for value type
DateTime object. And when we create reference type object of DateTime
it thows SerializationException.

But still a question is roaming in my mind is why it allows to create
reference type objects even though it is defined as 'public sealed
struct DateTime', ie value type ?

[Ankit Jain]
http://www.ankitjain.info/ankit/

DateTime is a value type, remove the ^ from...

DateTime Arrival;
DateTime Departure;

WayPoint2 ( LatLog ^latlog, String ^name, String ^description, DateTime
arrival, DateTime departure)

Willy.


AnkitAsDeveloper said:
Hi i am serializing a 'ref struct' object as follows :



private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}

private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;

}

// now i am serializing it in follwing way...

WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;

Seri( "a.waypoint", waypoint1 );

//Serialization works fine.... but following deserialization throws me
exception...

WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;

// Exception :
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.

// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}

WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}

Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.


[Ankit Jain]
http://www.ankitjain.info/ankit/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top