1

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.

6
  • Can you give some code samples? Why isn't it serializing at present? Commented Feb 3, 2015 at 13:34
  • 1
    Um and how do you expect JavaScript to read this? Commented Feb 3, 2015 at 13:34
  • To elaborate on what @epascarello said, javascript is not able to reach the MemoryStream because that exists on the server's memory. Why do you not want to use JSON or XML? That's gonna be about the only way to do it. Even if you could output the memory stream, the way C# represents that object in memory is different than how JS does, so JS wouldn't know how to deserialize it Commented Feb 3, 2015 at 13:38
  • 1
    @dcastro: The serialized object comes from c#. The way it serializes it would seem very important to me! I can imagine an equivalent Java object would be serialised to a very different end result. Commented Feb 3, 2015 at 13:44
  • 2
    If you could clarify why it is important that you use your current serialisation that might help narrow down an appropriate answer. Currently as it stands I want to say "You're doing it wrong" since using JSON is the appropriate serialisation to pass things to JS but there may well be reasons that you've left out that add factors not fully explained. @filtered's answer is an example of a solution that may or may not work depending on your requirements (since it basically says deserialize. Also clarifying how you serialize it might help us know how to deserialise it... Commented Feb 3, 2015 at 13:50

2 Answers 2

4

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

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

3 Comments

I'm torn between downvoting and not downvoting. In the question, the asker specifically said no JSON, however, since that's a bit unrealistic, this is probably the best way
I had the feeling the asker was looking for a solution without JSON, because he did not know, how to achieve this.Telling him "you have to use JSON, but is not that hard" seemed to be the right answer for me :)
Example requirements: For a multiplayer game we need to serialize some object of data (an instance of a custom written class) into binary, and then we pass this binary data through websocket message and then want to read some values from it. We use custom class and not JSON because it serializes into less bytes.
0

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!

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.