C# vs Java Serialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray Mitchell

    #1

    C# vs Java Serialization

    Hello,

    I have two Java applications that exchange serialized
    objects. Although I would like to ultimately convert both
    to C# it's a pretty monumental task, especially for
    someone just learning both langauges. I thought it might
    be more reasonable to start with simply converting the
    simpler one of the two and getting it to work. I have
    used Microsoft's free Java to C# converter to convert most
    of the code, but of course I must complete some of the
    conversions manually. I have this basic concern,
    however: Will the serialization done in C# be the same as
    that done in Java? Also, do all Java compilers serialize
    the same way for the same source code? How do I determine
    exactly what the serialized data looks like, both
    theoretically and emperically? I'd like to be able to
    view it at the byte level just for general debugging
    purposes. (Of course I could use a packet sniffer, but
    that's a little too gross even for a hardware person like
    me!)

    Thanks,
    Ray Mitchell
  • Eric Johannsen

    #2
    Re: C# vs Java Serialization

    You have the choice of several serialization formats in C#. No matter which
    format you decide to use, you can save the serialized data to a file if you
    want to have a look at the structure. The XML serializer creates quite
    straightforward , easy to interpret output. The binary serializer requires a
    hex editor to play around with of course.

    Try something like this:

    using System;
    using System.IO;
    using System.Xml.Seri alization;
    using System.Collecti ons;
    namespace Test
    {
    [Serializable()]
    public class Config
    {
    const string filename = "C:\\Temp\\conf ig.xml";
    private string userID;
    public string UserID
    {
    get { return userID; }
    set { userID = value; }
    }
    public void Save()
    {
    XmlSerializer ser = new XmlSerializer(t ypeof(Config));

    TextWriter writer = new StreamWriter(fi lename);
    ser.Serialize(w riter, this);
    writer.Close();
    }
    public static Config GetConfig()
    {
    Config cfg;
    FileStream fs;
    try
    {
    fs = new FileStream(file name, FileMode.Open);
    /* Use the Deserialize method to restore the object's state
    with
    data from the XML document. */
    XmlSerializer ser = new XmlSerializer(t ypeof(Config));
    cfg = (Config) ser.Deserialize (fs);
    fs.Close();
    }
    catch
    {
    cfg = null;
    }
    return cfg;
    }
    }
    }

    Then use this object from a test program something like this

    Config cfg = new Config();
    cfg.UserID = "MyTestID";
    cfg.Save(); // Look how it is saved in C:\Temp\Config. xml
    // ...
    Config cfgNew = Config.GetConfi g();

    I'm not sure about compatibility between Java and C# serialization formats,
    but hopefully someone else in the newsgroup can answer that.

    Eric

    "Ray Mitchell" <RayMitchell_No SpamPlease_@Mea nOldTeacher.com > wrote in
    message news:072301c3a9 74$97b68e90$a10 1280a@phx.gbl.. .[color=blue]
    > Hello,
    >
    > I have two Java applications that exchange serialized
    > objects. Although I would like to ultimately convert both
    > to C# it's a pretty monumental task, especially for
    > someone just learning both langauges. I thought it might
    > be more reasonable to start with simply converting the
    > simpler one of the two and getting it to work. I have
    > used Microsoft's free Java to C# converter to convert most
    > of the code, but of course I must complete some of the
    > conversions manually. I have this basic concern,
    > however: Will the serialization done in C# be the same as
    > that done in Java? Also, do all Java compilers serialize
    > the same way for the same source code? How do I determine
    > exactly what the serialized data looks like, both
    > theoretically and emperically? I'd like to be able to
    > view it at the byte level just for general debugging
    > purposes. (Of course I could use a packet sniffer, but
    > that's a little too gross even for a hardware person like
    > me!)
    >
    > Thanks,
    > Ray Mitchell[/color]

    Comment

    Working...