0

i have a byte array created in c# from object type and then send it over socket protocol to a Node js app, but unable to deserialize the data to readable object. what is the way to decode this data?

C# serializing code:

private static byte[] ToBytes(object message)
{
    IFormatter br = new BinaryFormatter();
    byte[] keyAsBytes = null;
    using (var ms = new MemoryStream())
    {
        br.Serialize(ms, message);
        keyAsBytes = ms.ToArray();
    }
    return keyAsBytes;
}

object o = 801507;
byte[] AsBytes =  ToBytes(o);
// send AsBytes

And in node js i try to decode the data

function handleMessage(message) {
    console.log(message.toString());
}

And the above code print me {"type":"Buffer","data":[0,1,0,0,0,255,255,255,255,1,0,0,0,0,0,0,0,4,1,0,0,0,12,83,121,115,116,101,109,46,73,110,116,54,52,1,0,0,0,7,109,95,118,97,108,117,101,0,9,227,58,12,0,0,0,0,0,11]}

so what is the right way to get the origin value ?

  • 2
    What makes you think you *can* turn a C# object binary representation into something useful on the Node.js side? A better option might be to use something standard, like serializing and sending JSON, which is readily parsed in nearly all environments, including Node.js. (Though JSON can be verbose, it's not for every project.) – T.J. Crowder Nov 25 '20 at 10:40
  • *"...but unable to deserialize the data to readable object or json."* JSON is a *textual notation* for data exchange. You don't deserialize *to* JSON, you deserialize *from* JSON (but only if you're sending JSON in the first place, which you aren't). [(More here.)] – T.J. Crowder Nov 25 '20 at 10:42
  • 1
    @T.J.Crowder more where? link is missing :) – nilsK Nov 25 '20 at 10:46
  • @nilsK - Doh! Editing error. Thanks, it's: http://stackoverflow.com/a/2904181/157247 – T.J. Crowder Nov 25 '20 at 10:50
  • 1
    @.J.Crowder Thankes for the comment on my text. post edited – Yermiyahu Tesler Nov 25 '20 at 10:50
  • @.J.Crowder this maybe not usual to serialize object type in c#, but this is the given situation for me and cannot be changed. my only concern is how to deal with that data in my node js app – Yermiyahu Tesler Nov 26 '20 at 08:40
  • `BinaryFormatter` generates a **proprietary, undocumented** dump of the **internal** state of .Net objects. Its output can't be used outside of .Net. See: [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182). – dbc Nov 29 '20 at 14:49
  • 1
    As mentioned above JSON would be the obvious platform-independent alternative, but if you require binary serialization you could look into [protocol buffers](https://developers.google.com/protocol-buffers). See: [Getting started with protobuf-net](https://stackoverflow.com/q/14535913/3744182) and [Google Protocol Buffers: JavaScript Example](https://stackoverflow.com/q/6912981/3744182). – dbc Nov 29 '20 at 14:49

1 Answers1

1

You are sending array of bytes (creted from c# object) to Node js. You probablly want send something more useful preferebly a string. To create (serialize) string (json object) from c# object use serializer / converter. In newer version of .net you have built in var jsonString = System.Text.Json.JsonSerializer.Serialize(object) or you can use Newtonsoft.Json nuget package to achieved the same thing. Newtonsoft.Json variant var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(object);

JanH
  • 107
  • 1
  • 9