kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.01k stars 196 forks source link

Model and parser classes (Java) #1047

Open WaterKnight opened 1 year ago

WaterKnight commented 1 year ago

Hello,

I am using the Java generator, which makes a class for every type with the fields and the parser logic in there.

In my case, the readings will be serialized again to for example JSON, which is why I already wrote some custom serializer logic that would omit the parser-related fields like _io, _parent, _root etc. or intermediate readings that would be replaced by instances, i.e., some fields declared in the .ksy file are only there to make the parser work but do not need to be exposed in the output.

Furthermore, there might be different parsers for the same format, even non-Kaitai ones. For example, in my scenario, the aforementioned JSON representation should be able to map to the Java data structure as well.

Therefore, would it not be better to separate the model (POJOs) from the parser classes? That should allow for better interfacing, then you can easily use automatic model mappers.

GreyCat commented 1 year ago

In theory, it's doable, nothing in .ksy specification prevents from building a compiler that will create such separated model + parser implementations. If you want to pursue that route, I would advise starting with creating some kind of proof of concept manually, i.e. by taking a moderately complicated .ksy and translating it into what you expect compiler to deliver.

In practice, I'm not sure that such level of complexity will be good match for use cases of KS project. DFDL, for example, follows such approach — keeping model and parser/serializer separate.

WaterKnight commented 1 year ago

Thanks for the DFDL hint, but I would like to use other Kaitai Struct features respectively have already written some dozen .ksy :-)

For now, I have written a little script that takes the generated/compiled Kaitai Struct Java files and generates analogous model classes out of them (which worked for my .ksy files at least), and then used an automatic serializer/deserializer to convert it from Kaitai class to the model class on runtime.

Shall see how it fares.