Snippet for serialization of any primitive type

I recently came up with this for InlineString and realized it is nearly universally applicable for primitive types.

Standard library Julia serialization works well by default for primitive types, and will just call read and write. If you have simple symmetric read and write functions for your primitive types, you should be good to go without these additional methods.

If for some reason you:

  • don’t have or want both read and write functions
  • have read and write functions that aren’t round-trippable
  • have read and write functions that transform the data before writing in a way that is either expensive or produces an inefficient data representation

then this serialization method may be right for you!

1 Like