Jaguar-dart / jaguar_serializer

Format (JSON, XML, protobuf, mongodb, etc) and platform (server, client) agnostic serialization framework
172 stars 34 forks source link

how to use jaguar_serializer plugin to convert below csharp code to dart? #175

Open NTMS2017 opened 5 years ago

NTMS2017 commented 5 years ago

I have a csharp code and I am not sure if this plugin serialise the object to byte array.

Below example uses Xml.Serialization. how to use jaguar_serializer in same manner of the below csharp code? The object is a class.

So main question is how to use jaguar_serializer plugin to convert below csharp code to dart?

private static readonly Object locker = new Object();

private static byte[] ObjectToByteArray(Object objectToSerialize)
{            
    MemoryStream ms = new MemoryStream();
    //BinaryFormatter formatter = new BinaryFormatter();            
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
    try
    {
        //Here's the core functionality! One Line!
        //To be thread-safe we lock the object
        lock (locker)
        {
            x.Serialize(ms, objectToSerialize);
            //formatter.Serialize(fs, objectToSerialize);
        }
        //return fs.ToArray();                
        return ms.ToArray();
    }
    catch (SerializationException se)
    {                
        return null;
    }
    finally
    {
        //fs.Close();
        ms.Close();
    }
}