c# - protobuf-net does not deserialize DateTime.Kind correctly -
c# - protobuf-net does not deserialize DateTime.Kind correctly -
using protobuf-net.dll version 1.0.0.280
when deserialize datetime
(wrapped in object) date/time ok datetime.kind
property 'unspecified'
consider test case serialize/deserialize datetime.
[testmethod] public void testdatetimeserialization() { var obj = new datetimewrapper {date = datetime.utcnow}; obj.date = datetime.specifykind(obj.date, datetimekind.utc); var serialized = obj.serializeproto(); var deserialized = serialized.deserializeproto<datetimewrapper>(); assert.areequal(datetimekind.utc, deserialized.date.kind); } public static byte[] serializeproto<t>(this t item) t : class { using (var ms = new memorystream()) { serializer.serialize(ms, item); homecoming ms.toarray(); } } public static t deserializeproto<t>(this byte[] raw) t : class, new() { using (var ms = new memorystream(raw)) { homecoming serializer.deserialize<t>(ms); } }
the assert fails, kind == unspecified
as result of protobuf-net not serializing property (see below) 1 solution assume datetimekind
equal utc when displaying dates on client side (only know should utc of course):
public static datetime todisplaytime(this datetime utcdatetime, timezoneinfo timezone) { if (utcdatetime.kind != datetimekind.utc)//may unspecified due serialization utcdatetime = datetime.specifykind(utcdatetime, datetimekind.utc); datetime result = timezoneinfo.converttime(utcdatetime, timezone); homecoming result; }
this saves having assign each datetime
property on receiving side.
protobuf.net has maintain compatibility protobuf binary format, designed java date/time datatypes. no kind
field in java -> no kind
back upwards in protobuf binary format -> kind
not transferred across network. or along lines.
as turns out, protobuf.net encodes ticks
field (only), you'll find code in bclhelpers.cs
.
but sense free add together field in protobuf message definition value.
c# .net serialization protobuf-net
Comments
Post a Comment