C#C
C#2y ago
Esa

BinarySerialization - need some design help

Hi, this is my first foray into lower level programming and I could use some help.

I am writing a DOCSIS-compliant binary serializer for C#. It will take as input a .cfg or .txt file that resembles a JSON, but is a bit different:

Main 
{
    DownstreamFrequency 130000000;
    UpstreamChannelId 123;
    NetworkAccess 1;
    ClassOfService
    {
        ClassID 5;
        MaxRateDown 512000;
        MaxRateUp 64000;
        PriorityUp 3;
        GuaranteedUp 32000;
        MaxBurstUp 54314;
        PrivacyEnable 1;
    }
    MaxCPE 13;
    SwUpgradeServer 10.1.1.1;
    /* CmMic e241803a16fa62269f90d6e1619a59d3; */
    /* CmtsMic 41141948116bcc38f6a20ec485fcd0f2; */
    /*EndOfDataMkr*/
}


This is a barebones, minified example of what these files will contain. Every line is either a property or a collection of properties.
I will refer to these properties as DocsisProperty<T>, since they will be deserialized into a C# type and then encoded into byte[] later on.

Now the issue I am facing is that there are 25 ish encoding methods and 25 decoding methods, but hundreds of different properties. All unique properties are stored in a property map with name, id and a reference to the correct method for encoding or decoding.
This can be seen here: https://github.com/rlaager/docsis/blob/master/src/docsis_symtable.h

My original idea was that when I am parsing a file, I can initialize a new DocsisProperty<T> based on the identifier of the value, then later on when I want to encode it I call docsisProperty.Encode(), and it will look up some static dictionary to figure out which encoding method is the appropriate one. However that gets complicated quickly because of the generic type on DocsisProperty<T>. The dictionary would then have to use object or
dynamic
as it's return type, and I am not a fan of either approach.
Was this page helpful?