In our cross-platform multi-app networked system developed mainly with Untiy engine we use custom binary serialization that simply writes primitives using BinaryWriter and then, using mirror layout, reads the same using BinaryReader. Note that we do not use BinaryFormatter and instead rely on manually specified binary layouts. It works very well for us as we have absolute control over what and how is serialized. We also go by primitive representations of objects rather then some kind of string formatting and then parsing to keep it compact. So for example we would write an integer directly rather than int.ToString(). In other words, compact over human-readable. Bare minimum to unambiguously deserialize something back to exactly what was serialized.
Recently I've bumped into NodaTime as a "better" (.NET's than DateTime, DateTimeOffset, TimeZoneInfo) solution for dealing with all things time. I especially like the strictness of types to force me think what exactly I'm talking about (clean universal time line vs zig-zag-y madness of time zones etc.).
I moved our date&time management to NodaTime but now I have troubles serializing the Noda types into binary stream as primitives. There seems to be no "standard" property accessing paradigm. Some types have getters, like Duration.TotalNanoseconds, some instead have a'la ToXyz() methods like Instant.ToUnixTicks(). By the way, this is something I suggest to simplify: getters for pure access of expectable bare-bones representation of the type and methods a'la ToXyz() for conversion calculation. This is missing, for example, for Instant type.
I did try the format/parse approach with Noda's format patters, but for some reason most provided format patterns are not roundtrip or even throw an exception that those patterns don't support parsing, only formatting:
binaryWriter.Write(ZonedDateTimePattern.ExtendedFormatOnlyIso.Format(value));
ZonedDateTimePattern.ExtendedFormatOnlyIso.Parse(binaryReader.ReadString()).Value;
What I want is something as simple as this:
binaryWriter.Write(instant.Nanoseconds);
instant = new Instant(binaryReader.ReadDouble());
binaryWriter.Write(duration.nanoseconds);
duration= new Duration(binaryReader.ReadDouble());
What one consistent, preferably non-formatting-parsing-roundtrip-based approach can I use to achieve compact binary serialization of all NodaTime types?
If that's not possible, what are recommended roundtrip patters for each NodaTime type?
PS: The text patterns in the code sample explicitly say they're not round-trip (format-only, no parsing) in their name. My bad.