none
Identifying serialized binary string data type possible? RRS feed

  • Question

  • I am passing serialized objects between applications as binary-formatted, serialized strings. My receiving application can deserialized the string and cast it back into a datatype. All works fine.

    However, I am realizing that if I wish to send different types of objects using the same method, my receiving application needs to know what type of class/object to case the received data into beforehand.

    Given a serialized binary base64string of an object, is it possible to determine from what datatype it originated?

    Wednesday, June 27, 2012 9:52 PM

Answers

  • No, just deserialize and store in a variable of type object.  Then use the GetType() method to determine the type of the object and proceed accordingly.

    Jose R. MCP
    Code Samples

    • Marked as answer by smarrocco Thursday, June 28, 2012 2:18 AM
    Thursday, June 28, 2012 2:06 AM

All replies

  • It depends on the serialization technique you're using.  The framework's serializers all include the type information in the binary (or text) data they produce, so your stream would include the required data to deserialize the type, provided the receiving end understands the type involved.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, June 27, 2012 10:07 PM
    Moderator
  • So, since I *am* using the framework's serializers, would that mean that the receiving application can determine the type of the object that was serialized based only upon the serialized string?

    The key here is that the receiving end must be able to *identify* the object that it is about to deserialize *before* it has been deserialized in order to determine into which type of object to cast the string during deserialization.
    Thursday, June 28, 2012 1:52 AM
  • No, just deserialize and store in a variable of type object.  Then use the GetType() method to determine the type of the object and proceed accordingly.

    Jose R. MCP
    Code Samples

    • Marked as answer by smarrocco Thursday, June 28, 2012 2:18 AM
    Thursday, June 28, 2012 2:06 AM
  • Got it, so the string *must* be deserialized first before determining the type, then deserialized a second time once the type is determined. I was thinking that there might be some way to avoid a double deserialization.

    Thanks!

    Thursday, June 28, 2012 2:18 AM
  • I never said that.  I said:  Deserialize and store in variable of type object.  Then determine type.  Now you can have a series of IF statements or similar to typecast the already-deserialized object.  Don't deserialize again.

    object o = <deserialize here>;
    string t1 = null;
    SomeClass t2 = null;
    if (o.GetType() == typeof(string))
    {
        t1 = (string)o;  //Type-cast instead of deserialization once again.
    }
    else if (o.GetType() == typeof(SomeClass))
    {
        t2 = (SomeClass)o;
    }


    Jose R. MCP
    Code Samples

    Thursday, June 28, 2012 2:25 AM