Discussion:
serializable
(too old to reply)
mp
2016-12-12 21:32:46 UTC
Permalink
as I read the help files, if i want all the classes(data) to be stored
from one program run to the next I need to put [Serializable()] in front
of each class definition...is that right? or if i have a master
container class that holds all instances of subobjects can that one
class be serializable and not all the individual classes?

[Serializable()]
class Building {...contains all BuildingComponent objects ...}
...not sure how it will contain them, maybe a List object?...

in any case the help file shows one object writing to one file.
I would want x objects all writing to one file

foreach object in container
object.WriteToMasterfile

however the help files on writing to xml file do not show the book class
with the serializable attribute so i'm confused

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Marcel Mueller
2016-12-12 22:54:31 UTC
Permalink
Post by mp
as I read the help files, if i want all the classes(data) to be stored
from one program run to the next I need to put [Serializable()] in front
of each class definition...is that right?
More or less true.

Strictly speaking there are many methods of serialization, and one of
them requires the SerializableAttribute.
E.g. XmlSerialization AFAIK does not require the SerializableAttribute.
And Database POCOs do not need to be serializable either.
Post by mp
or if i have a master
container class that holds all instances of subobjects can that one
class be serializable and not all the individual classes?
You will get an exception about the content of your container that
cannot be serialized.
Post by mp
[Serializable()]
class Building {...contains all BuildingComponent objects ...}
...not sure how it will contain them, maybe a List object?...
???
Post by mp
in any case the help file shows one object writing to one file.
I would want x objects all writing to one file
foreach object in container
object.WriteToMasterfile
While possible this won't help you much since at the other way around
(deserialization) you do not have the container with the exactly
matching number of objects where you could apply the foreach loop.
=> Simply serialize your container including the content.
Post by mp
however the help files on writing to xml file do not show the book class
with the serializable attribute so i'm confused
Xml Serialization works entirely different. It simply saves the values
of all public properties in XML elements of the same name. Everything
else is lost.
In fact the XmlSerializer internally writes code to do the job and
immediately invokes csc.exe to compile it. This can be quite slow at
program startup or if invoked too often.

Another major difference of theses two serialization methods is the
compatibility. While the XmlSerializer will be able to read serialized
data from an older class version with less properties the binary
serializer will sadly fail if the data should be read with a changed
class layout. So binary serialization is usually a bad idea if the
serialized data should survive a program update. XML or JSON
serialization usually fits better in this case.


Marcel
mp
2016-12-13 18:10:39 UTC
Permalink
Post by Marcel Mueller
Post by mp
I need to put [Serializable()] in front
of each class definition.
More or less true.
And Database POCOs do not need to be serializable either.
just read about POCOs in old post from stackoverflow

"POCO is often considered good because it allows for a strong separation
of concerns. You can define your data objects to have absolutely zero
knowledge of the mechanism that will be used to store them. (So it makes
it easy to switch out the storage mechanism for something different in
the future). It also means you don't have to design your data objects
with any consideration for the database/framework that is used to store
them."
Post by Marcel Mueller
=> Simply serialize your container including the content.
Xml Serialization <snip> saves the values
of all public properties in XML elements of the same name. Everything
else is lost.
so for example if a private variable is set somehow in code other than
thru a public property it would be lost on deserialization? what else
can be lost?
Post by Marcel Mueller
In fact the XmlSerializer <snip> can be quite slow at
program startup
slow program would be one criteria i would like to avoid, therefore
binary serialize appears better, but in your next comment it would break
on program update...since i'm just now creating program many updates
would be expected so what would you recommend to get around the two
negatives of lost data and slow program?
Post by Marcel Mueller
the binary serializer will sadly fail if the data should be read with a
changed
Post by Marcel Mueller
class layout. So binary serialization is usually a bad idea if the
serialized data should survive a program update. XML or JSON
serialization usually fits better in this case.
so would JSON avoid both the above problems?
I know nothing about JSON other than what I've just looked up online...
would this work as a deserialize code in my context?

public static T Deserialise<T>(string json)
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
var serialiser = new DataContractJsonSerializer(typeof(T));
return (T)serialiser.ReadObject(ms);
}
}
...and this for the3 serializer
public static string Serialize<T>(T obj)
{
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
return Encoding.Default.GetString(ms.ToArray());
}
}

except i'm not finding a DataContractJsonSerializer in my ide
Namespace: System.Runtime.Serialization.Json does not exist
looks like i need an assembly reference
Post by Marcel Mueller
Marcel
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-13 18:14:04 UTC
Permalink
Post by mp
}
except i'm not finding a DataContractJsonSerializer in my ide
Namespace: System.Runtime.Serialization.Json does not exist
looks like i need an assembly reference
found the assembly, it now builds


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-14 02:30:46 UTC
Permalink
Post by mp
Post by Marcel Mueller
Xml Serialization <snip> saves the values
of all public properties in XML elements of the same name. Everything
else is lost.
so for example if a private variable is set somehow in code other than
thru a public property it would be lost on deserialization?
Yes.
Post by mp
what else
can be lost?
I only think you have fields that are exposed via properties and
fields that are not.
Post by mp
Post by Marcel Mueller
In fact the XmlSerializer <snip> can be quite slow at
program startup
slow program would be one criteria i would like to avoid,
Slow is a very relative term.

So what if the startup took a few milliseconds - would it really
be a problem?
Post by mp
therefore
binary serialize appears better, but in your next comment it would break
on program update...since i'm just now creating program many updates
would be expected so what would you recommend to get around the two
negatives of lost data and slow program?
I usually recommend:

disk => XML

net => binary
Post by mp
Post by Marcel Mueller
the binary serializer will sadly fail if the data should be read with a
changed
Post by Marcel Mueller
class layout. So binary serialization is usually a bad idea if the
serialized data should survive a program update. XML or JSON
serialization usually fits better in this case.
so would JSON avoid both the above problems?
JSON is more like XML in this regard.

But:
- JSON uses a lot less network bandwidth than XML
- XML has some advanced capabilities (schemas, XSLT etc.)
Post by mp
public static T Deserialise<T>(string json)
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
var serialiser = new DataContractJsonSerializer(typeof(T));
return (T)serialiser.ReadObject(ms);
}
}
...and this for the3 serializer
public static string Serialize<T>(T obj)
{
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
return Encoding.Default.GetString(ms.ToArray());
}
}
except i'm not finding a DataContractJsonSerializer in my ide
Namespace: System.Runtime.Serialization.Json does not exist
looks like i need an assembly reference
It is in:

System.Runtime.Serialization.dll

Arne

Arne Vajhøj
2016-12-13 01:00:20 UTC
Permalink
Post by mp
as I read the help files, if i want all the classes(data) to be stored
from one program run to the next I need to put [Serializable()] in front
of each class definition...is that right?
All classes to be serialized need to be specified as such.

[Serializable]

is the way to achieve that.

That applies to the entire object tree.
Post by mp
or if i have a master
container class that holds all instances of subobjects can that one
class be serializable and not all the individual classes?
All need to be serializable.
Post by mp
[Serializable()]
class Building {...contains all BuildingComponent objects ...}
...not sure how it will contain them, maybe a List object?...
in any case the help file shows one object writing to one file.
I would want x objects all writing to one file
foreach object in container
object.WriteToMasterfile
An object of type List<> is serializable so you can serialize
and deserializae the entire list.

Or have an object of a wrapper class containing the list.
Post by mp
however the help files on writing to xml file do not show the book class
with the serializable attribute so i'm confused
XML serialization is very different from binary serialization.

You don't need that annotation for XML serialization.

Arne
mp
2016-12-13 12:01:21 UTC
Permalink
Post by Arne Vajhøj
Post by mp
as I read the help files, if i want all the classes(data) to be stored
<>
Post by Arne Vajhøj
All classes to be serialized need to be specified as such.
[Serializable]
is the way to achieve that.
so it doesn't need the () shown in help? [serializable()] ?
Post by Arne Vajhøj
An object of type List<> is serializable so you can serialize
and deserializae the entire list.
Or have an object of a wrapper class containing the list.
Arne
thanks again Arne and Marcel
this is so exciting!! I actually got the serialize to work in a trivial
case by tweaking the help example:
namespace BuildingComponentTest
{
static class Program
{
//private static object FirstBuilding;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
Console.WriteLine("testing BuildingComponents Application");


// First write something so that there is something to read
...
//default constructor
//var FirstBuilding = new cBuilding();
//second constructor provides name
var FirstBuilding = new cBuilding("FirstBuilding");

var writer = new
System.Xml.Serialization.XmlSerializer(typeof(cBuilding));
var wfile = new
System.IO.StreamWriter(@"c:\temp\SerializationOverview.xml");
writer.Serialize(wfile, FirstBuilding);
wfile.Close();

// Now we can read the serialized object ...

System.Xml.Serialization.XmlSerializer reader =
new
System.Xml.Serialization.XmlSerializer(typeof(cBuilding ));
System.IO.StreamReader file = new System.IO.StreamReader(
@"c:\temp\SerializationOverview.xml");
FirstBuilding = (cBuilding)reader.Deserialize(file);
file.Close();
Console.WriteLine("Building name " + FirstBuilding.Name);
Console.WriteLine(FirstBuilding.Count + " items in building ");

}




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-13 23:43:22 UTC
Permalink
Post by mp
Post by Arne Vajhøj
Post by mp
as I read the help files, if i want all the classes(data) to be stored
<>
Post by Arne Vajhøj
All classes to be serialized need to be specified as such.
[Serializable]
is the way to achieve that.
so it doesn't need the () shown in help? [serializable()] ?
Correct.
Post by mp
Post by Arne Vajhøj
An object of type List<> is serializable so you can serialize
and deserializae the entire list.
Or have an object of a wrapper class containing the list.
var FirstBuilding = new cBuilding("FirstBuilding");
var writer = new
System.Xml.Serialization.XmlSerializer(typeof(cBuilding));
var wfile = new
writer.Serialize(wfile, FirstBuilding);
wfile.Close();
// Now we can read the serialized object ...
System.Xml.Serialization.XmlSerializer reader =
new
System.Xml.Serialization.XmlSerializer(typeof(cBuilding ));
System.IO.StreamReader file = new System.IO.StreamReader(
@"c:\temp\SerializationOverview.xml");
FirstBuilding = (cBuilding)reader.Deserialize(file);
file.Close();
Yes. It is not that hard.

Note though that to produce nicer XML you may need to annotate
your data classes.

Arne
Loading...