Discussion:
trouble with <T>
(too old to reply)
mp
2016-12-15 22:04:37 UTC
Permalink
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
Registered User
2016-12-15 23:12:47 UTC
Permalink
Here is a simple helper type I use for serializing and deserializing objects
using textfiles. You need to add the package Newtonsoft.Json to the project.
From the VS menu Tools/NugetPackageManager/Manage Nuget Packages for Solution.
Click Browse and enter Newtonsoft.Json in the textbox. When the result appears
click on Newtonsoft.Json. Click the project's checkbox and then the Install
button.

public static class JsonHelper
{
public static T Deserialize<T>(string json)
{
T result = default(T);
try
{
result = JsonConvert.DeserializeObject<T>(json);
}
catch { }
return result;
}

public static T DeserializeFromFile<T>(string filePath)
{
string json = File.ReadAllText(filePath);
var result = Deserialize<T>(json);
return result;
}

public static void Serialize<T>(T obj, string filePath)
{
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;

using (StreamWriter sw = new StreamWriter(filePath))
{
using (JsonWriter jw = new JsonTextWriter(sw))
{
serializer.Serialize(jw, obj);
}
}
}
}

usage :
JsonHelper.Serialize<FooBar>(foobar,
@"C:\someDirectory\someFileName.json");

Foobar foobar =
Post by mp
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);
//read from file
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
mp
2016-12-15 23:49:46 UTC
Permalink
that's awesome, thank you
just so happens i had just finished installing newtonsoft.json based on
some posts that mentioned it was easier than the net one

thanks a ton, i'll try this out
Post by Registered User
Here is a simple helper type I use for serializing and deserializing objects
using textfiles. You need to add the package Newtonsoft.Json to the project.
From the VS menu Tools/NugetPackageManager/Manage Nuget Packages for Solution.
Click Browse and enter Newtonsoft.Json in the textbox. When the result appears
click on Newtonsoft.Json. Click the project's checkbox and then the Install
button.
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-16 00:51:14 UTC
Permalink
Post by mp
that's awesome, thank you
just so happens i had just finished installing newtonsoft.json based on
some posts that mentioned it was easier than the net one
do i have to add a reference to the json library? I can't find one


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-16 00:56:33 UTC
Permalink
Post by mp
Post by mp
that's awesome, thank you
just so happens i had just finished installing newtonsoft.json based on
some posts that mentioned it was easier than the net one
do i have to add a reference to the json library? I can't find one
ok found the browse button duh! :-)


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-16 01:49:39 UTC
Permalink
Post by mp
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)
//this won't compile
public void Serialize<T>(object objectToSerialize, string fileName)
public void Serialize<T>(T objectToSerialize, string fileName)
Post by mp
{
WriteToFile(fileName, Json1.Serialize<T>(objectToSerialize));
}
//cannot convert from 'object' to 'T'
Arne
mp
2016-12-16 13:51:49 UTC
Permalink
Post by Arne Vajhøj
Post by mp
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)
//this won't compile
public void Serialize<T>(object objectToSerialize, string fileName)
public void Serialize<T>(T objectToSerialize, string fileName)
Post by mp
{
WriteToFile(fileName, Json1.Serialize<T>(objectToSerialize));
}
//cannot convert from 'object' to 'T'
Arne
argghhh! ;-) I knew it had to be something simple lol
thanks again Arne

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

Continue reading on narkive:
Loading...