GuillaumeDSL
GuillaumeDSL

Reputation: 21

How to : Deserialize a C# object from javascript

I have searched a bit online and couldn't find an answer to my question.

I have a C# serialization that is made from a MemoryStream's Object. I would like to save that Serialization (which is a thing I already can do) and then deserialize it with JavaScript.

Do anyone know if this is possible ? I haven't seen any API which could do that.

Thank you for your answers. To be more specific I already have an application running in C# which use the MemoryStream's deserialization. This is a pretty big application and I want to do as few modifications as possible. I want to link an other application (which is running in HTML/JavaScript) with the first one by using the same serialization. If I use the JSON Serialization I will have to often modify the code in C# which is a thing I want to try to avoid.

To summarize, I want to be able to "read" the serialization generated by C# in my javascript project.

In order to be clear I don't want to use an other serialization to JSON / XML / ..., I would like to use the one I'm already using and then deserialize it with JavaScript.

Thanks in advance.

Upvotes: 1

Views: 1431

Answers (2)

Anton Scharnowski
Anton Scharnowski

Reputation: 11

You could Either try XML Serialization or JSON. C# has an XML provider class wich is easy to use, but you should parse your data as object Array, so Java and C# can deserialize them in the way like:

public class person
{
  public string name;
  public byte age;
  public person(object[] args)
  {
     name = args[0];
     age = args[1];
  }
}

On Recieve you could deserialize in an objec[] and then create an Class Instance with

Person p = new Person(rec_args);

I dont exactly know how Javascript handels classes but on Serilaziation level, you have to work on basic level (Only Serialize variables into object Arrays and Reverse) to have a clean bugfree execution!

Upvotes: 0

Ole Albers
Ole Albers

Reputation: 9305

When you use Serialization in C# you usually have Binary or XML-Serialization. None of those can be read by JavaScript out of the box. (And it really is not worth the effort to try to implement this).

But switching from object serialization to JSON and (vice versa) is not really hard:

Person p = new Person();
p.name = "John";
p.age = 42;
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream1, p);

(example from MSDN: https://msdn.microsoft.com/en-us/library/bb412179%28v=vs.110%29.aspx)

So. In Short: Rethink your requirements. You won't find an easy solution without Json

Upvotes: 4

Related Questions