Discussion:
create ISerializer interface and extensions
(too old to reply)
mp
2016-12-15 18:33:53 UTC
Permalink
trying this
interface iSerializer
{
void Serialize(object objectToSerialize, string filename);
object DeSerialize(string filename);
}


interface ISerializerJson : ISerializer
{
new void Serialize(object objectToSerialize, string filename);
new object DeSerialize(string filename);
}

interface ISerializerXml : ISerializer
{
new void Serialize(object objectToSerialize, string filename);
new object DeSerialize(string filename);
}
interface ISerializerBinary : ISerializer
{
new void Serialize(object objectToSerialize, string filename);
new object DeSerialize(string filename);
}

does this look like the right direction?

it compiled but when i made a class that implemented the interface
i get a runtime error as shown below

public class SerializerJson : ISerializerJson
{
public void Serialize(object objectToSerialize, string fileName)
{
WriteToFile(fileName, Json1.Serialize(objectToSerialize));
}
public object DeSerialize<T>(string fileName)
{
return Json1.Deserialize<T>(ReadFromFile(fileName));
}
private void WriteToFile(string fileName, string encodingString)
{
System.IO.File.WriteAllText(@fileName, encodingString);
}
private string ReadFromFile(string fileName)
{
return System.IO.File.ReadAllText(fileName);
}

}

static void Main()
{
Console.WriteLine("testing BuildingComponents Application");
// First write something so that there is something to read
...
var FirstBuilding = new Building("First Building");
var SecondBuilding = new Building("Second Building");
var filename = "C:/temp/testSerialize.txt";
var serializerJSON = new SerializerJson();

Console.WriteLine("First Building name starts " +
FirstBuilding.Name);
Console.WriteLine("try to write to file");

//bombs on this line now
serializerJSON.Serialize(FirstBuilding, filename);
Console.WriteLine("try to read from file");
SecondBuilding = (Building)
serializerJSON.DeSerialize<Building>(filename);


Console.WriteLine("After DeSerialization Second Building
name " + SecondBuilding.Name);
}
Exception thrown: 'System.Runtime.Serialization.SerializationException'
in System.Runtime.Serialization.dll

the same basic code worked when hard coded in main but when i put in
object it bombs, do you see anything obvious i'm missing?

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-15 18:49:03 UTC
Permalink
On 12/15/2016 12:33 PM, mp wrote:
tried replacing
System.IO.File.WriteAllText(@fileName, encodingString);
with
System.IO.File.WriteAllText(fileName, encodingString);

don't know what the @ symbol is for except that i've seen it in examples
so thought maybe it was an escape symbol for a string in quotes but it
still bombs

i'll have to figure out how to handle exceptions to see if i can get
more detail?
Post by mp
private void WriteToFile(string fileName, string encodingString)
{
}
Exception thrown: 'System.Runtime.Serialization.SerializationException'
in System.Runtime.Serialization.dll
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-15 20:18:11 UTC
Permalink
Post by mp
tried replacing
with
System.IO.File.WriteAllText(fileName, encodingString);
so thought maybe it was an escape symbol for a string in quotes but it
still bombs
i'll have to figure out how to handle exceptions to see if i can get
more detail?
Post by mp
private void WriteToFile(string fileName, string encodingString)
{
}
Exception thrown: 'System.Runtime.Serialization.SerializationException'
in System.Runtime.Serialization.dll
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
i see it is an escape or eliminates need for escape rather
but don't know if i need to use it on a variable containing a string as
i would on the literal string


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-16 01:30:55 UTC
Permalink
Post by mp
trying this
interface iSerializer
{
void Serialize(object objectToSerialize, string filename);
object DeSerialize(string filename);
}
interface ISerializerJson : ISerializer
{
new void Serialize(object objectToSerialize, string filename);
new object DeSerialize(string filename);
}
interface ISerializerXml : ISerializer
{
new void Serialize(object objectToSerialize, string filename);
new object DeSerialize(string filename);
}
interface ISerializerBinary : ISerializer
{
new void Serialize(object objectToSerialize, string filename);
new object DeSerialize(string filename);
}
does this look like the right direction?
I don't think the last 3 interfaces provide much value.

ISerializer interface
SerializerXxxx classes would do in my opinion
Post by mp
it compiled but when i made a class that implemented the interface
i get a runtime error as shown below
Exception thrown: 'System.Runtime.Serialization.SerializationException'
in System.Runtime.Serialization.dll
the same basic code worked when hard coded in main but when i put in
object it bombs, do you see anything obvious i'm missing?
There must be more information than that.

Arne
mp
2016-12-16 13:58:58 UTC
Permalink
Post by Arne Vajhøj
Post by mp
trying this
interface iSerializer
{
void Serialize(object objectToSerialize, string filename);
object DeSerialize(string filename);
}
<snip>
Post by Arne Vajhøj
Post by mp
does this look like the right direction?
I don't think the last 3 interfaces provide much value.
ISerializer interface
SerializerXxxx classes would do in my opinion
<>
Post by Arne Vajhøj
Arne
Thank you

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Loading...