indice-co / EDI.Net

EDI Serializer/Deserializer. Supports EDIFact, X12 and TRADACOMS formats
MIT License
446 stars 170 forks source link

Serializing enums / reserializing file not 1:1 #183

Closed shinji144 closed 3 months ago

shinji144 commented 3 years ago

Hi @cleftheris,

I've followed the tutorial to serialize/deserialize, using the provided sample file "tradacoms.utilitybill.edi". Deserializing that file provides me a POCO wherein the VatRateCategoryCode gets set to L. However, serializing the same POCO using same grammar yields me a vastly different .edi file compared to the original- and the enum is serialized to its integer equivalent instead of the text.

var grammar = EdiGrammar.NewTradacoms();
var interchange = default(Interchange);

using (var stream = new StreamReader(@"C:\edi testing\tradacoms.utilitybill.edi"))
{
    interchange = new EdiSerializer().Deserialize<Interchange>(stream, grammar);
}

using (var tw = new StreamWriter(File.Open(@"C:\edi testing\tradacoms UtilityBill test1.edi", FileMode.Create)))
{
    using (var twEDI = new EdiTextWriter(tw, grammar))
    {
        new EdiSerializer().Serialize(twEDI, interchange);
    }
}

Why does the enum get serialized as an integer? And why does the reserialized file look dramatically different from the sample?

Thanks!

cleftheris commented 3 years ago

Hi @shinji144 and thanks for your interenst in Edi.Net.

The reason behind the different serialization behavior has todo with the sample.

First of all this usecase shows how someone needing to read an Edi transmission could decide to deserialize characters as well as letters into CLR enums. This feature unfortunately does not work bothways at the moment. It is however a nice enhancement since we could infer the serialization preference from the Picture (format) on the value attribute and decide whether we need to convert to the numeric counterpart underneath (which is the default behavior) or use the name instead.

On the other hand there is no complete parity between the sample nodels and the transmission. In other words models may be incomplete and ignore parts of the data hence the difference in serialized output. The example is meant to showcase the power that the library has in mappind edi to a more dotnet friendly poco class. In case you need to have a one to one parity you need to tweak the example models to suit your needs.