mp
2016-12-15 22:04:37 UTC
have read about generic types and the use of <T>
but cant' figure out how to use it correctly
virtually the same code works from Main() but when i try to put it into
a class it fails, given the attempts i've made to encapsulate it into an
object(i'm sure its' a stupid simple mistake i'm making)
//error runtime on attempt to serialize an object
"Type 'BuildingComponentTest.Building' with data contract name
'Building:http://schemas.datacontract.org/2004/07/BuildingComponentTest'
is not expected. Consider using a DataContractResolver if you are using
DataContractSerializer or add any types not known statically to the list
of known types - for example, by using the KnownTypeAttribute attribute
or by adding them to the list of known types passed to the serializer."}
//this won't compile
public void Serialize<T>(object objectToSerialize, string fileName)
{
WriteToFile(fileName, Json1.Serialize<T>(objectToSerialize));
}
//cannot convert from 'object' to 'T'
//no good either
public void Serialize<T>(object objectToSerialize, string fileName)
{
Type typ = objectToSerialize.GetType();
WriteToFile(fileName,
Json1.Serialize<typeof(typ)>(objectToSerialize));
}
//typ is a variable but is used like a type
//likewise
public void Serialize<T>(object objectToSerialize, string fileName)
{
Type typ = objectToSerialize.GetType();
WriteToFile(fileName,
Json1.Serialize<typeof(typ)>(objectToSerialize));
}
//typ is a variable but is used like a type
here's the whole class containing the above methods
public class SerializerJson : ISerializerJson
{
public void Serialize<T>(object objectToSerialize, string fileName)
{
WriteToFile(fileName, Json1.Serialize<T>(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);
}
//using Arne's code for Json1.cs thankyou thankyou thankyou!!!
//if necessary here's the interfaces
interface ISerializerJson : ISerializer
{
new void Serialize<T>(object objectToSerialize, string filename);
new object DeSerialize<T>(string filename);
}
//and
interface ISerializer
{
void Serialize<T>(object objectToSerialize, string filename);
object DeSerialize<T>(string filename);
}
//and Arne's Json1.cs
public static class Json1
{
public static string Serialize<T>(T o)
{
DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, o);
return Encoding.UTF8.GetString(ms.ToArray());
}
public static T Deserialize<T>(string json)
{
MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(T));
return (T)ser.ReadObject(ms);
}
}
//and here's the same(virtually) code in Main that works
static void Main()
{
var FirstBuilding = new Building("First Building");
var SecondBuilding = new Building("Second Building");
string filename = "C:/temp/testSerialize.txt";
Console.WriteLine("First Building name starts " +
FirstBuilding.Name);
//write to file
string objectString = Json1.Serialize(FirstBuilding);
System.IO.File.WriteAllText(@filename, objectString);
//read from file
objectString = System.IO.File.ReadAllText(@filename);
SecondBuilding = Json1.Deserialize<Building>(objectString);
Console.WriteLine("After DeSerialization Second Building
name " + SecondBuilding.Name);
}
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
but cant' figure out how to use it correctly
virtually the same code works from Main() but when i try to put it into
a class it fails, given the attempts i've made to encapsulate it into an
object(i'm sure its' a stupid simple mistake i'm making)
//error runtime on attempt to serialize an object
"Type 'BuildingComponentTest.Building' with data contract name
'Building:http://schemas.datacontract.org/2004/07/BuildingComponentTest'
is not expected. Consider using a DataContractResolver if you are using
DataContractSerializer or add any types not known statically to the list
of known types - for example, by using the KnownTypeAttribute attribute
or by adding them to the list of known types passed to the serializer."}
//this won't compile
public void Serialize<T>(object objectToSerialize, string fileName)
{
WriteToFile(fileName, Json1.Serialize<T>(objectToSerialize));
}
//cannot convert from 'object' to 'T'
//no good either
public void Serialize<T>(object objectToSerialize, string fileName)
{
Type typ = objectToSerialize.GetType();
WriteToFile(fileName,
Json1.Serialize<typeof(typ)>(objectToSerialize));
}
//typ is a variable but is used like a type
//likewise
public void Serialize<T>(object objectToSerialize, string fileName)
{
Type typ = objectToSerialize.GetType();
WriteToFile(fileName,
Json1.Serialize<typeof(typ)>(objectToSerialize));
}
//typ is a variable but is used like a type
here's the whole class containing the above methods
public class SerializerJson : ISerializerJson
{
public void Serialize<T>(object objectToSerialize, string fileName)
{
WriteToFile(fileName, Json1.Serialize<T>(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);
}
//using Arne's code for Json1.cs thankyou thankyou thankyou!!!
//if necessary here's the interfaces
interface ISerializerJson : ISerializer
{
new void Serialize<T>(object objectToSerialize, string filename);
new object DeSerialize<T>(string filename);
}
//and
interface ISerializer
{
void Serialize<T>(object objectToSerialize, string filename);
object DeSerialize<T>(string filename);
}
//and Arne's Json1.cs
public static class Json1
{
public static string Serialize<T>(T o)
{
DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, o);
return Encoding.UTF8.GetString(ms.ToArray());
}
public static T Deserialize<T>(string json)
{
MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(T));
return (T)ser.ReadObject(ms);
}
}
//and here's the same(virtually) code in Main that works
static void Main()
{
var FirstBuilding = new Building("First Building");
var SecondBuilding = new Building("Second Building");
string filename = "C:/temp/testSerialize.txt";
Console.WriteLine("First Building name starts " +
FirstBuilding.Name);
//write to file
string objectString = Json1.Serialize(FirstBuilding);
System.IO.File.WriteAllText(@filename, objectString);
//read from file
objectString = System.IO.File.ReadAllText(@filename);
SecondBuilding = Json1.Deserialize<Building>(objectString);
Console.WriteLine("After DeSerialization Second Building
name " + SecondBuilding.Name);
}
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus