Discussion:
create JSONSerializer class
(too old to reply)
mp
2016-12-13 19:38:11 UTC
Permalink
trying to create a class to serialize objects that I could use thusly
static class Program
{
[STAThread]
static void Main()
{
Console.WriteLine("testing BuildingComponents Application");

// First write something so that there is something to read
var FirstBuilding = new cBuilding("FirstBuilding");
//JsonSerializer Serializer = new JsonSerializer();
JsonSerializer.Serialize(FirstBuilding);


Console.WriteLine("Building name " + FirstBuilding.Name);
Console.WriteLine(FirstBuilding.Count + " items in building ");



}


//that compiled and ran without error but where did the file get
created? I would expect to be able to give it a filename to save to??

class JsonSerializer
{
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);
}
}

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());
}
}
}

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-13 20:39:49 UTC
Permalink
Post by mp
trying to create a class to serialize objects that I could use thusly
JsonSerializer.Serialize(BuildingObject);
//that compiled and ran without error but where did the file get
created? I would expect to be able to give it a filename to save to??
obviously i'm totally confused. don't know how to tell it where to save
to and don't know how to read it back, the deserialize method want's a
string parameter input but don't know what that string is supposed to be

if this is the deserialize method what is the (string json) supposed to be?
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);
}
}

in test program trying to use thusly

cBuilding FirstBuilding = New cBuilding(parameters);
JsonSerializer.Serialize(FirstBuilding);

//don't know how to destroy FirstBuilding object
//vb I would set FirstBuilding = nothing...don't know c# equiv
//so creating a second object to test if the First one's data was saved
cBuilding SecondBuilding = JsonSerializer.Deserialise(???);


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
mp
2016-12-13 21:30:56 UTC
Permalink
Post by mp
trying to create a class to serialize objects that I could use thusly
in other words i want to

Serializer.Serialize(myObject, filename)
myObject = Serializer.Deserialize(filename)




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-14 02:35:48 UTC
Permalink
Post by mp
trying to create a class to serialize objects that I could use thusly
static class Program
{
[STAThread]
static void Main()
{
Console.WriteLine("testing BuildingComponents Application");
// First write something so that there is something to read
var FirstBuilding = new cBuilding("FirstBuilding");
//JsonSerializer Serializer = new JsonSerializer();
JsonSerializer.Serialize(FirstBuilding);
Console.WriteLine("Building name " + FirstBuilding.Name);
Console.WriteLine(FirstBuilding.Count + " items in building ");
}
//that compiled and ran without error but where did the file get
created? I would expect to be able to give it a filename to save to??
JsonSerializer.Serialize(FirstBuilding);

->

string jsonresult = JsonSerializer.Serialize(FirstBuilding);

and then you write jsonresult wherever you want.

Arne
mp
2016-12-14 21:45:17 UTC
Permalink
Post by Arne Vajhøj
Post by mp
trying to create a class to serialize objects
string jsonresult = JsonSerializer.Serialize(FirstBuilding);
and then you write jsonresult wherever you want.
Arne
thanks Arne,
Serializing works, Deserializing appears to fail not sure why

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
...
var FirstBuilding = new Building("First Building");
string Filename = "C:/temp/testSerialize.txt";
Console.WriteLine("First Building name starts " +
FirstBuilding.Name);

JsonSerializer Serializer = new JsonSerializer();
Serializer.Serialize (FirstBuilding, Filename);
//change name to indicate that after DeSerialization
variable points to first building
Building SecondBuilding = new Building ("Second Building");
Console.WriteLine("Second Building name starts " +
SecondBuilding.Name);

Serializer.DeSerialize(Filename,SecondBuilding );
//that should hae put FirstBuilding's data into SecondBuilding object
(or so i thought)
Console.WriteLine("After DeSerialization Second Building
name " + SecondBuilding.Name);
//Console.WriteLine(SecondBuilding.Count + " items in
building ");

}

results:
The program '[17120] BuildingComponentTest.vshost.exe' has exited with
code 0 (0x0).
Second Building name starts Second Building
After DeSerialization Second Building name Second Building

here's the serializer class
public class JsonSerializer
{
private string EncodingString;

public void Serialize(object ObjectToSerialize, string Filename)
{
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(ObjectToSerialize.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, ObjectToSerialize);
EncodingString = Encoding.Default.GetString(ms.ToArray());
System.IO.File.WriteAllText(@Filename, EncodingString);
}
}
public void DeSerialize(string Filename, object
ObjectToDeSerialize)
{
EncodingString = System.IO.File.ReadAllText(Filename);
using (var ms = new
MemoryStream(Encoding.Unicode.GetBytes(EncodingString)))
{
Type T = ObjectToDeSerialize.GetType();
var serialiser = new DataContractJsonSerializer(T);
ObjectToDeSerialize = serialiser.ReadObject(ms);
}
}

so i thought the problem might be I didn't pass in the object by ref
so i tried
public void DeSerialize(string Filename, ref object ObjectToDeSerialize)
and
Serializer.DeSerialize(Filename, ref SecondBuilding );
but i get the compile error:
cannot convert from ref building to ref object
so i'm back to brickwall :-)

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-15 01:00:55 UTC
Permalink
Post by mp
Serializing works, Deserializing appears to fail not sure why
Building SecondBuilding = new Building ("Second Building");
Console.WriteLine("Second Building name starts " +
SecondBuilding.Name);
Serializer.DeSerialize(Filename,SecondBuilding );
//that should hae put FirstBuilding's data into SecondBuilding object
(or so i thought)
Console.WriteLine("After DeSerialization Second Building
name " + SecondBuilding.Name);
//Console.WriteLine(SecondBuilding.Count + " items in
building ");
}
public void DeSerialize(string Filename, object
ObjectToDeSerialize)
{
EncodingString = System.IO.File.ReadAllText(Filename);
using (var ms = new
MemoryStream(Encoding.Unicode.GetBytes(EncodingString)))
{
Type T = ObjectToDeSerialize.GetType();
var serialiser = new DataContractJsonSerializer(T);
ObjectToDeSerialize = serialiser.ReadObject(ms);
}
}
so i thought the problem might be I didn't pass in the object by ref
That is indeed a problem.
Post by mp
so i tried
public void DeSerialize(string Filename, ref object ObjectToDeSerialize)
and
Serializer.DeSerialize(Filename, ref SecondBuilding );
cannot convert from ref building to ref object
so i'm back to brickwall :-)
That can probably be resolved by some type casting.

But I believe that the right solution is to have the deserialized
values as return type.

Arne
Arne Vajhøj
2016-12-15 01:02:49 UTC
Permalink
Post by Arne Vajhøj
But I believe that the right solution is to have the deserialized
values as return type.
Example:

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);
}
}

Arne
mp
2016-12-15 17:03:07 UTC
Permalink
Post by Arne Vajhøj
Post by Arne Vajhøj
But I believe that the right solution is to have the deserialized
values as return type.
public static class Json1
{
Arne
awesome thank you
this test worked

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
...
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);
}
shows SecondBuilding name indeed changed to "First Building"

so now i'm trying to think of where to put this implementation
i'd think building would have that responsibility?
make an ISerializer interface
ISerializerJson : ISerializer etc
then Building : ISerializer
then Building.SerializeMyself using whichever flavor of serializer???

or would the main or event in a main form continue to do that work?
eg if i had a form with buttons
"NewProject" creates a new building
"OpenProject" browse to file containing saved Building
"SaveProject" allow user to browse to location to serialize Building
object

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Arne Vajhøj
2016-12-16 01:28:41 UTC
Permalink
Post by mp
so now i'm trying to think of where to put this implementation
i'd think building would have that responsibility?
make an ISerializer interface
ISerializerJson : ISerializer etc
then Building : ISerializer
then Building.SerializeMyself using whichever flavor of serializer???
or would the main or event in a main form continue to do that work?
eg if i had a form with buttons
"NewProject" creates a new building
"OpenProject" browse to file containing saved Building
"SaveProject" allow user to browse to location to serialize Building
object
Building class is part of your domain model.

The JSON serializer class is either general utility class or
part of your persistence code.

I don't think you should mix them.

Arne
mp
2016-12-16 14:04:10 UTC
Permalink
Post by Arne Vajhøj
Post by mp
so now i'm trying to think of where to put this implementation
i'd think building would have that responsibility?
make an ISerializer interface
ISerializerJson : ISerializer etc
then Building : ISerializer
then Building.SerializeMyself using whichever flavor of serializer???
or would the main or event in a main form continue to do that work?
eg if i had a form with buttons
"NewProject" creates a new building
"OpenProject" browse to file containing saved Building
"SaveProject" allow user to browse to location to serialize Building
object
Building class is part of your domain model.
The JSON serializer class is either general utility class or
part of your persistence code.
I don't think you should mix them.
Arne
thank you

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

Loading...