-2

I am writing a class that both serializes and deserializes an Object. I want to be able to use this class with any object I pass to it.

How do I pass the object to the class? I will need to be able to access the class fields for the instance I am sending it and also I will need to access the definition of the class in order to deserialize text back into an object.

ex.

//mainpage.cs code
private void sendObjectToSerialize()
{
    var db = new DatabaseHandler();
    var myCar = new Car();
    myCar.Make = "Chevy";
    string ObjectString = db.SerializeObject(myCar);
}

private void SendStringToBeDeserialized()
{
    String myString = "SomeSerializeString" //this string would have been serialized using above Obect myCar
    var db = new DatabaseHandler();
    var myCar = new Car();
    var myCar = db.DeserializeStringToObject(String,myCar);
}

//Database Handler Code
public string SerializeObject(Object myObject)
{
    var myNewObject = new myObject();//doesn't work - error myObject used as a variable not a type
    //...serialize the object passed to this function
}

public Object DeserializeStringToObject(String ObjectString ,Object myObject)
{
    var myNewObject = new myObject;//error myObject used as a variable not a type
    //convert string to Object of type myObject...
    return mynewObject;
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 3
    please edit this and fix your horrible formatting it's a mess to read.. and you do not need the enter code here tags at all – MethodMan Aug 30 '16 at 20:35
  • it was formatted when I entered it. somehow this happened when I hit submit. Let met try to correct it. – Gary Hengeveld Aug 30 '16 at 20:39
  • you need to debug your code also `var myNewObject = new myObject;` shouldn't that be `var myNewObject = new myObject();` also if you are passing in the object and you have `var my NewObject` why don't you just assign it to the param that's being passed in ..? also when show code please show all relevant code.. for example the class definition of `Car` you may want to look up how to use `Activator.CreateInstance` – MethodMan Aug 30 '16 at 20:44
  • 1
    Thank you for the Edit MethodMan – Gary Hengeveld Aug 30 '16 at 20:45
  • http://stackoverflow.com/questions/2194949/how-to-use-class-name-as-parameter-in-c-sharp – MethodMan Aug 30 '16 at 20:49
  • Car is a simple public class containing one property named 'make' I didn't think it was necessary to add the entire car class or the serialization code because the car class should be interchangeable as I want to be able to send any object. and I already have working serialization code. – Gary Hengeveld Aug 30 '16 at 20:54
  • I'm just trying to figure out how to send in 'any' object to the class function to use in the code. I need to use not only the instance of the object sent to the code, but I also need to access the definition of the object being sent so I can rebuild the object after deserialization. I'm not sure if I'm articulating my need correctly. forgive me if I am not. – Gary Hengeveld Aug 30 '16 at 20:54
  • what does the DatabaseHandler Class look like also show what the Car Class Looks like – MethodMan Aug 30 '16 at 21:01
  • If you've found the answer to your question you should post it as *an answer* rather than as an edit to the question. – Servy Mar 03 '17 at 17:47

1 Answers1

0

Here is my serializer class code:

// serializer.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace QMC1.Handlers
{
    public class Serializer
    {

This will return a string that can be then saved to a database or file. Currently it only seems to work with a single object and not an ObservableCollection<Object>

public String ConvertObjectToJSONString<myObjectType>(myObjectType myObject)
{
    string content = String.Empty;
    var js = new DataContractJsonSerializer(typeof(myObjectType));
    var ms = new MemoryStream();
    js.WriteObject(ms, myObject);
    ms.Position = 0;

    var reader = new StreamReader(ms);
    content = reader.ReadToEnd();
    return content;
}

This will take the string created with the above code and return it back to the caller as a fully functional Object again. It will only convert back to original object type. Currently it only seems to work with a single object and not an ObservableCollection<Object>.

        public myObjectType ConvertStringToObject<myObjectType>(string CarObjectString, string myObjectName)
        {
            myObjectType myObject = (myObjectType)Activator.CreateInstance(Type.GetType(myObjectName));

            var js = new DataContractJsonSerializer(typeof(myObjectType));    
            byte[] byteArray = Encoding.UTF8.GetBytes(CarObjectString);

            var ms = new MemoryStream(byteArray);
            myObject = (myObjectType)js.ReadObject(ms);    
            return myObject;  
        }
    }
}

Calling code from any file - using car model as example. (car model parameters don't matter for this example)

Serializer ser = new Serializer();

Convert your Object to a JSON string

myCarString = ser.ConvertObjectToJSONString<Car>(myCar).ToString();

Save your string to your database or file.

Pull your string from the database or file and assign it's value to myCarString. Convert your string into an object.

myCar = ser.ConvertStringToObject<Car>(myCarString, "NameSpace.Car");

Use your new object as normal.

Servy
  • 202,030
  • 26
  • 332
  • 449