-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;
}
9
  • 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 Commented Aug 30, 2016 at 20:35
  • it was formatted when I entered it. somehow this happened when I hit submit. Let met try to correct it. Commented Aug 30, 2016 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 Commented Aug 30, 2016 at 20:44
  • 1
    Thank you for the Edit MethodMan Commented Aug 30, 2016 at 20:45
  • stackoverflow.com/questions/2194949/… Commented Aug 30, 2016 at 20:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.