コード例 #1
0
        void ISerializer <DateTime> .Write(ref ProtoWriter.State state, DateTime value)
        {
            var model       = state.Model;
            var includeKind = model != null && model.HasOption(Meta.TypeModel.TypeModelOptions.IncludeDateTimeKind);

            ((ISerializer <ScaledTicks>) this).Write(ref state, ScaledTicks.Create(value, includeKind));
        }
コード例 #2
0
 void ISerializer <ScaledTicks> .Write(ref ProtoWriter.State state, ScaledTicks value)
 {
     if (value.Value != 0)
     {
         state.WriteFieldHeader(ScaledTicks.FieldTimeSpanValue, WireType.SignedVarint);
         state.WriteInt64(value.Value);
     }
     if (value.Scale != TimeSpanScale.Days)
     {
         state.WriteFieldHeader(ScaledTicks.FieldTimeSpanScale, WireType.Varint);
         state.WriteInt32((int)value.Scale);
     }
     if (value.Kind != DateTimeKind.Unspecified)
     {
         state.WriteFieldHeader(ScaledTicks.FieldTimeSpanKind, WireType.Varint);
         state.WriteInt32((int)value.Kind);
     }
 }
コード例 #3
0
        private static void WriteTimeSpanImpl(ref ProtoWriter.State state, TimeSpan timeSpan, DateTimeKind kind)
        {
            switch (state.WireType)
            {
            case WireType.String:
            case WireType.StartGroup:
                var scaled = new ScaledTicks(timeSpan, kind);
                state.WriteMessage <ScaledTicks>(SerializerFeatures.OptionSkipRecursionCheck, scaled, SerializerCache <PrimaryTypeProvider> .InstanceField);
                break;

            case WireType.Fixed64:
                state.WriteInt64(timeSpan.Ticks);
                break;

            default:
                ThrowHelper.ThrowProtoException("Unexpected wire-type: " + state.WireType.ToString());
                break;
            }
        }
コード例 #4
0
        ScaledTicks ISerializer <ScaledTicks> .Read(ref ProtoReader.State state, ScaledTicks _)
        {
            int           fieldNumber;
            TimeSpanScale scale = TimeSpanScale.Days;
            long          value = 0;
            var           kind  = DateTimeKind.Unspecified;

            while ((fieldNumber = state.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case ScaledTicks.FieldTimeSpanScale:
                    scale = (TimeSpanScale)state.ReadInt32();
                    break;

                case ScaledTicks.FieldTimeSpanValue:
                    state.Assert(WireType.SignedVarint);
                    value = state.ReadInt64();
                    break;

                case ScaledTicks.FieldTimeSpanKind:
                    kind = (DateTimeKind)state.ReadInt32();
                    switch (kind)
                    {
                    case DateTimeKind.Unspecified:
                    case DateTimeKind.Utc:
                    case DateTimeKind.Local:
                        break;         // fine

                    default:
                        ThrowHelper.ThrowProtoException("Invalid date/time kind: " + kind.ToString());
                        break;
                    }
                    break;

                default:
                    state.SkipField();
                    break;
                }
            }
            return(new ScaledTicks(value, scale, kind));
        }