0

The output string of the BinarySerializer seems too long. It does not change even if I shorten the property names of the class to single character length.

Here is the class file:

using System;

namespace Models.Accounts
{
    /// <summary>
    /// PasswordResetRequest class
    /// model of a password reset request
    /// </summary>
    [Serializable]
    public class PRR
    {
        /// <summary>
        /// request id
        /// </summary>
        public string I { get; set; }

        /// <summary>
        /// time request received
        /// </summary>
        public DateTime T { get; set; }

        /// <summary>
        /// application to which the password belongs
        /// </summary>
        public string A { get; set; }

        /// <summary>
        /// username whose password needs to be reset
        /// </summary>
        public string U { get; set; }

        /// <summary>
        /// token (guid)
        /// </summary>
        public Guid G { get; set; }

        /// <summary>
        /// token used
        /// </summary>
        public bool D { get; set; }

        /// <summary>
        /// client ip
        /// </summary>
        public string C { get; set; }
    }
}

and I'm using this SO link for serialization. When I use the serializer as

        PRR request = new PRR();
        request.U = "someusername";
        request.G = Guid.NewGuid();

        string searlizedRequest = SingletonCommon.Instance.SerializeObject(request);

I get a very long value for searlizedRequest like AAEAAAD/////AQAAAAAAAAAMAgAAAEFMSVQuTW9kZWxzLCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAF0xJVC5Nb2RlbHMuQWNjb3VudHMuUFJSBwAAABI8ST5rX19CYWNraW5nRmllbGQSPFQ+a19fQmFja2luZ0ZpZWxkEjxBPmtfX0JhY2tpbmdGaWVsZBI8VT5rX19CYWNraW5nRmllbGQSPEc+a19fQmFja2luZ0ZpZWxkEjxEPmtfX0JhY2tpbmdGaWVsZBI8Qz5rX19CYWNraW5nRmllbGQBAAEBAwABDQtTeXN0ZW0uR3VpZAECAAAACgAAAAAAAAAACgYDAAAADHNvbWV1c2VybmFtZQT8////C1N5c3RlbS5HdWlkCwAAAAJfYQJfYgJfYwJfZAJfZQJfZgJfZwJfaAJfaQJfagJfawAAAAAAAAAAAAAACAcHAgICAgICAgL20za6r7D0QbKWb7tG1cjSAAoL

Is there any formatter with shorter output I can use or anything else I can try, since this string will be part of a link sent in an email.

5
  • 2
    Use protobuf-net instead if you want a small result Commented Jun 5, 2014 at 14:37
  • 1
    I'd put this data in a DB and send the user a small key to that information. Commented Jun 5, 2014 at 15:20
  • 1
    If you need just any serialization, then use XmlSerializer. You can control it with attributes and files can be edited/viewed by a human (xml is easy to understand). If you need minimum size, then you have to use binary serialization: BinarySerializer, protobuf (fastests, smallest, not human viewable/editable and required using third-party library, though very well supported). If your aim is to transfer serialized object via ascii-text channel, then why not using json, designed specially? Commented Jun 5, 2014 at 15:33
  • @jgauffin thanks- trying protobuf-net now (though skeptical of continued .net support). usr- good idea - thanks will try that too Sinatr- can't use xml or json since this is a password reset link that is going to user's email. should not be easily decodable Commented Jun 5, 2014 at 15:43
  • Converted properties to fields and used [NonSerializable] attribute for some fields, and it reduced the size by almost 40%. Not bad for right now Commented Jun 5, 2014 at 16:49

1 Answer 1

0

Perhaps you can try GZipStream to compress the output when writing to a file to have minimum size

public static void SetCompressed<T>(T data,string name) 
{
    BinaryFormatter bf;
    using(FileStream fileStream = new FileStream(name+ ".object", FileMode.Create, FileAccess.Write))
    using(GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress))
    {
        bf.Serialize(compressionStream, data);
    }
}

public static T GetCompressed<T>(string name) 
{
    BinaryFormatter bf;
    T Tobject;
    using(FileStream fileStream = new FileStream(name + ".object", FileMode.Open, FileAccess.Read))
    using(GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
    {
        Tobject = (T)bf.Deserialize(compressionStream);
    }

    return Tobject;
}

The code above uses BinaryFormatter to serialize any object alongwith FileStream & GZipStream. The GZipStream will compress the output *.object file to some extent.

HACK: You can open the the *.object file with 7-Zip to see the serialized content inside.

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

2 Comments

ended up implementing @usr comment.
The case against Base64 is it encodes strings as UTF-8 first, and then encodes the resulting byte sequence as Base64. So it will increase by 30 -33%.

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.