Exemplo n.º 1
0
        Timestamp ISerializer <Timestamp> .Read(ref ProtoReader.State state, Timestamp value)
        {
            var duration = new Duration(value.Seconds, value.Nanoseconds);

            duration = ReadDuration(ref state, duration);
            return(new Timestamp(duration.Seconds, duration.Nanoseconds));
        }
Exemplo n.º 2
0
        public KeyValuePair <TKey, TValue> Read(ref ProtoReader.State state, KeyValuePair <TKey, TValue> pair)
        {
            TKey   key   = pair.Key;
            TValue value = pair.Value;
            int    field;

            while ((field = state.ReadFieldHeader()) > 0)
            {
                switch (field)
                {
                case 1:
                    key = state.ReadAny(_keyFeatures, key, _keySerializer);
                    break;

                case 2:
                    value = state.ReadAny(_valueFeatures, value, _valueSerializer);
                    break;

                default:
                    state.SkipField();
                    break;
                }
            }
            if (TypeHelper <TKey> .IsReferenceType && TypeHelper <TKey> .ValueChecker.IsNull(key))
            {
                key = TypeModel.CreateInstance <TKey>(state.Context, _keySerializer);
            }
            if (TypeHelper <TValue> .IsReferenceType && TypeHelper <TValue> .ValueChecker.IsNull(value))
            {
                value = TypeModel.CreateInstance <TValue>(state.Context, _valueSerializer);
            }

            return(new KeyValuePair <TKey, TValue>(key, value));
        }
Exemplo n.º 3
0
        public void Deserialize(ProtoReader reader, ref ProtoReader.State state, ref T obj)
        {
            object o = obj;

            o   = deserializer(reader, ref state, o);
            obj = (T)o;
        }
Exemplo n.º 4
0
        public override object Read(ProtoReader source, ref ProtoReader.State state, object value)
        {
            int       field = source.FieldNumber;
            BasicList list  = new BasicList();

            if (packedWireType != WireType.None && source.WireType == WireType.String)
            {
                SubItemToken token = ProtoReader.StartSubItem(source, ref state);
                while (ProtoReader.HasSubValue(packedWireType, source))
                {
                    list.Add(Tail.Read(source, ref state, null));
                }
                ProtoReader.EndSubItem(token, source, ref state);
            }
            else
            {
                do
                {
                    list.Add(Tail.Read(source, ref state, null));
                } while (source.TryReadFieldHeader(ref state, field));
            }
            int   oldLen = AppendToCollection ? (value == null ? 0 : ((Array)value).Length) : 0;
            Array result = Array.CreateInstance(itemType, oldLen + list.Count);

            if (oldLen != 0)
            {
                ((Array)value).CopyTo(result, 0);
            }
            list.CopyTo(result, oldLen);
            return(result);
        }
Exemplo n.º 5
0
        decimal ISerializer <decimal> .Read(ref ProtoReader.State state, decimal value)
        {
            ulong low       = 0;
            uint  high      = 0;
            uint  signScale = 0;
            int   fieldNumber;

            while ((fieldNumber = state.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case FieldDecimalLow: low = state.ReadUInt64(); break;

                case FieldDecimalHigh: high = state.ReadUInt32(); break;

                case FieldDecimalSignScale: signScale = state.ReadUInt32(); break;

                default: state.SkipField(); break;
                }
            }
            int lo     = (int)(low & 0xFFFFFFFFL),
                mid    = (int)((low >> 32) & 0xFFFFFFFFL),
                hi     = (int)high;
            bool isNeg = (signScale & 0x0001) == 0x0001;
            byte scale = (byte)((signScale & 0x01FE) >> 1);

            return(new decimal(lo, mid, hi, isNeg, scale));
        }
Exemplo n.º 6
0
        LocalDate ISerializer <LocalDate> .Read(ref ProtoReader.State state, LocalDate value)
        {
            int field;
            int year = value.Year, month = value.Month, day = value.Day;

            while ((field = state.ReadFieldHeader()) > 0)
            {
                switch (field)
                {
                case 1:
                    year = state.ReadInt32();
                    break;

                case 2:
                    month = state.ReadInt32();
                    break;

                case 3:
                    day = state.ReadInt32();
                    break;

                default:
                    state.SkipField();
                    break;
                }
            }
            return(new LocalDate(year, month, day)); // ISO calendar is implicit
        }
Exemplo n.º 7
0
        LocalTime ISerializer <LocalTime> .Read(ref ProtoReader.State state, LocalTime value)
        {
            int field;
            int hours = value.Hour, minutes = value.Minute, seconds = value.Second, nanos = value.NanosecondOfSecond;

            while ((field = state.ReadFieldHeader()) > 0)
            {
                switch (field)
                {
                case 1:
                    hours = state.ReadInt32();
                    break;

                case 2:
                    minutes = state.ReadInt32();
                    break;

                case 3:
                    seconds = state.ReadInt32();
                    break;

                case 4:
                    nanos = state.ReadInt32();
                    break;

                default:
                    state.SkipField();
                    break;
                }
            }
            return(new LocalTime(hours, minutes, seconds).PlusNanoseconds(nanos));
        }
Exemplo n.º 8
0
        public object Read(ref ProtoReader.State state, object value)
        {
            object[] values     = new object[members.Length];
            bool     invokeCtor = false;

            if (value == null)
            {
                invokeCtor = true;
            }
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = GetValue(value, i);
            }
            int field;

            while ((field = state.ReadFieldHeader()) > 0)
            {
                invokeCtor = true;
                if (field <= tails.Length)
                {
                    IRuntimeProtoSerializerNode tail = tails[field - 1];
                    values[field - 1] = tails[field - 1].Read(ref state, tail.RequiresOldValue ? values[field - 1] : null);
                }
                else
                {
                    state.SkipField();
                }
            }
            return(invokeCtor ? ctor.Invoke(values) : value);
        }
Exemplo n.º 9
0
        internal static Timestamp ReadTimestamp(ref ProtoReader.State state, Timestamp value)
        {
            var duration = new Duration(value.Seconds, value.Nanoseconds);

            duration = ReadDuration(ref state, duration);
            return(new Timestamp(duration.Seconds, duration.Nanoseconds));
        }
Exemplo n.º 10
0
        public override object Read(ref ProtoReader.State state, object value)
        {
            Debug.Assert(value is null); // not expecting incoming
            string s = (string)Tail.Read(ref state, null);

            return(s.Length == 0 ? null : new Uri(s, UriKind.RelativeOrAbsolute));
        }
Exemplo n.º 11
0
        public override object Read(ref ProtoReader.State state, object value)
        {
            var ss = (SubTypeState <TParent>)value;

            ss.ReadSubType <TChild>(ref state);
            return(ss);
        }
Exemplo n.º 12
0
        public override object Read(ProtoReader source, ref ProtoReader.State state, object value)
        {
            TDictionary typed = (AppendToCollection ? ((TDictionary)value) : null)
                                ?? (TDictionary)Activator.CreateInstance(concreteType);

            do
            {
                var          key        = DefaultKey;
                var          typedValue = DefaultValue;
                SubItemToken token      = ProtoReader.StartSubItem(source, ref state);
                int          field;
                while ((field = source.ReadFieldHeader(ref state)) > 0)
                {
                    switch (field)
                    {
                    case 1:
                        key = (TKey)keyTail.Read(source, ref state, null);
                        break;

                    case 2:
                        typedValue = (TValue)Tail.Read(source, ref state, Tail.RequiresOldValue ? (object)typedValue : null);
                        break;

                    default:
                        source.SkipField(ref state);
                        break;
                    }
                }

                ProtoReader.EndSubItem(token, source, ref state);
                typed[key] = typedValue;
            } while (source.TryReadFieldHeader(ref state, fieldNumber));

            return(typed);
        }
Exemplo n.º 13
0
        private static void Merge(ProtoReader reader, ref ProtoReader.State state, ref protogen.Database obj)
        {
            SubItemToken tok;
            int          field;

            if (obj == null)
            {
                obj = new protogen.Database();
            }
            while ((field = reader.ReadFieldHeader(ref state)) != 0)
            {
                switch (field)
                {
                case 1:
                    do
                    {
                        protogen.Order _1 = default;
                        tok = ProtoReader.StartSubItem(reader, ref state);
                        Merge(reader, ref state, ref _1);
                        ProtoReader.EndSubItem(tok, reader, ref state);
                        obj.Orders.Add(_1);
                    } while (reader.TryReadFieldHeader(ref state, 1));
                    break;

                default:
                    reader.AppendExtensionData(ref state, obj);
                    break;
                }
            }
        }
 public static List <Student> CreateProtoReaderBuffer(byte[] encoded)
 {
     using (MemoryStream rs = new MemoryStream(encoded))
     {
         ProtoReader.State pr       = ProtoReader.State.Create(rs, null, null);
         List <Student>    students = new List <Student>();
         Student           student  = new Student();
         while (pr.GetPosition() < rs.Length)
         {
             var fieldHeader = pr.ReadFieldHeader();
             if (fieldHeader == 1)
             {
                 student = new Student();
                 students.Add(student);
             }
             if (fieldHeader == 1)
             {
                 student.Id = BclHelpers.ReadGuid(ref pr);
             }
             else if (fieldHeader == 2)
             {
                 student.Name = pr.ReadString();
             }
             else if (fieldHeader == 3)
             {
                 student.AdmissionDate = BclHelpers.ReadDateTime(ref pr);
             }
             else if (fieldHeader == 4)
             {
                 student.Age = pr.ReadInt32();
             }
         }
         return(students);
     }
 }
Exemplo n.º 15
0
        private static Duration ReadDurationFallback(ref ProtoReader.State state, Duration value)
        {
            var seconds = value.Seconds;
            var nanos   = value.Nanoseconds;
            int fieldNumber;

            while ((fieldNumber = state.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case 1:
                    seconds = state.ReadInt64();
                    break;

                case 2:
                    nanos = state.ReadInt32();
                    break;

                default:
                    state.SkipField();
                    break;
                }
            }
            return(new Duration(seconds, nanos));
        }
Exemplo n.º 16
0
 public ProtoReader ReadROM(out ProtoReader.State state)
 {
     if (!_ros.IsSingleSegment)
     {
         throw new InvalidOperationException("Expected single segment");
     }
     return(ProtoReader.Create(out state, _ros.First, Model));
 }
Exemplo n.º 17
0
        T ISubTypeSerializer <T> .ReadSubType(ref ProtoReader.State state, SubTypeState <T> value)
        {
            value.OnBeforeDeserialize(_subTypeOnBeforeDeserialize);
            DeserializeBody(ref state, ref value, (ref SubTypeState <T> s) => s.Value, (ref SubTypeState <T> s, T v) => s.Value = v);
            var val = value.Value;

            Callback(ref val, TypeModel.CallbackType.AfterDeserialize, state.Context);
            return(val);
        }
        public override object Read(ProtoReader source, ref ProtoReader.State state, object value)
        {
            object result = Tail.Read(source, ref state, value);

            if (setSpecified != null)
            {
                setSpecified.Invoke(value, new object[] { true });
            }
            return(result);
        }
Exemplo n.º 19
0
        public object Read(ProtoReader source, ref ProtoReader.State state, object value)
        {
            // convert the incoming value
            object[] args = { value };
            value = toTail.Invoke(null, args);

            // invoke the tail and convert the outgoing value
            args[0] = rootTail.Read(source, ref state, value);
            return(fromTail.Invoke(null, args));
        }
Exemplo n.º 20
0
 internal static Duration ReadDuration(ref ProtoReader.State state, Duration value)
 {
     if (state.WireType == WireType.String && state.RemainingInCurrent >= 20)
     {
         if (TryReadDurationFast(ref state, ref value))
         {
             return(value);
         }
     }
     return(ReadDurationFallback(ref state, value));
 }
Exemplo n.º 21
0
        public override object Read(ref ProtoReader.State state, object value)
        {
            Debug.Assert(value != null);
            object newValue = Tail.Read(ref state, Tail.RequiresOldValue ? field.GetValue(value) : null);

            if (newValue != null)
            {
                field.SetValue(value, newValue);
            }
            return(null);
        }
Exemplo n.º 22
0
 public object Read(ProtoReader source, ref ProtoReader.State state, object value)
 {
     if (wellKnown)
     {
         return(BclHelpers.ReadDuration(source, ref state));
     }
     else
     {
         Helpers.DebugAssert(value == null); // since replaces
         return(BclHelpers.ReadTimeSpan(source, ref state));
     }
 }
Exemplo n.º 23
0
 public object Read(ref ProtoReader.State state, object value)
 {
     if (wellKnown)
     {
         return(BclHelpers.ReadTimestamp(ref state));
     }
     else
     {
         Debug.Assert(value == null); // since replaces
         return(BclHelpers.ReadDateTime(ref state));
     }
 }
Exemplo n.º 24
0
 public override object Read(ProtoReader source, ref ProtoReader.State state, object value)
 {
     Helpers.DebugAssert(fieldNumber == source.FieldNumber);
     if (strict)
     {
         source.Assert(ref state, wireType);
     }
     else if (NeedsHint)
     {
         source.Hint(wireType);
     }
     return(Tail.Read(source, ref state, value));
 }
Exemplo n.º 25
0
        T ISerializer <T> .Read(ref ProtoReader.State state, T value)
        {
            bool isFirst = Interlocked.Increment(ref _deserializeCount) == 1;

            if (isFirst)
            {
                RuntimeTypeModel.OnDebugLog("Pre first read", typeof(T));
            }
            value = state.ReadBaseType <TBase, T>(value);
            if (isFirst)
            {
                RuntimeTypeModel.OnDebugLog("Post first read", typeof(T));
            }
            return(value);
        }
Exemplo n.º 26
0
        public override object Read(ref ProtoReader.State state, object value)
        {
            bool isFirst = Interlocked.Increment(ref _deserializeCount) == 1;

            if (isFirst)
            {
                RuntimeTypeModel.OnDebugLog("Pre first read", typeof(T));
            }
            value = state.ReadBaseType <TBase, T>(TypeHelper <T> .FromObject(value));
            if (isFirst)
            {
                RuntimeTypeModel.OnDebugLog("Post first read", typeof(T));
            }
            return(value);
        }
Exemplo n.º 27
0
        T ISubTypeSerializer <T> .ReadSubType(ref ProtoReader.State state, SubTypeState <T> value)
        {
            bool isFirst = Interlocked.Increment(ref _subDeserializeCount) == 1;

            if (isFirst)
            {
                RuntimeTypeModel.OnDebugLog("Pre first sub-read", typeof(T));
            }
            var ret = subTypeDeserializer(ref state, value);

            if (isFirst)
            {
                RuntimeTypeModel.OnDebugLog("Pre first sub-read", typeof(T));
            }
            return(ret);
        }
Exemplo n.º 28
0
        internal static unsafe Guid Read(ref ProtoReader.State state)
        {
            // note: we can't use a stackalloc-span here because the compiler doesn't trust
            // state, which is a ref-local (and can store spans), not to store it; since we *don't*
            // do that, we can be evil
            byte *ptr       = stackalloc byte[MAX_LENGTH];
            var   available = state.ReadBytes(new Span <byte>(ptr, MAX_LENGTH));

            char standardFormat;

            switch (available.Length)
            {
            case 0:
                return(Guid.Empty);

            case 16:     // treat as big-endian bytes
                // expand those bytes to hex, backwards so we don't overwrite live data
                int write = 32;
                for (int i = 15; i >= 0; i--)
                {
                    var val = ptr[i];
                    ptr[--write] = ToHex(val & 0b1111);
                    ptr[--write] = ToHex((val >> 4) & 0b1111);
                }
                available      = new Span <byte>(ptr, 32);
                standardFormat = 'N';
                break;

            case 32:     // no hyphens
                standardFormat = 'N';
                break;

            case 36:     // hyphens
                standardFormat = 'D';
                break;

            default:
                ThrowHelper.Format($"Unexpected Guid length: {available.Length}");
                return(default);
            }

            if (!(Utf8Parser.TryParse(available, out Guid guid, out int bytes, standardFormat) && bytes == available.Length))
            {
                ThrowHelper.Format($"Failed to read Guid: '{Encoding.UTF8.GetString(ptr, available.Length)}'");
            }

            return(guid);
Exemplo n.º 29
0
                public MyMessage Read(ref ProtoReader.State state, MyMessage value)
                {
                    value ??= new MyMessage();
                    int field;

                    while ((field = state.ReadFieldHeader()) > 0)
                    {
                        switch (field)
                        {
                        case 12: value.Y = state.ReadString(); break;

                        case 42: value.X = state.ReadInt32(); break;

                        default: state.SkipField(); break;
                        }
                    }
                    return(value);
                }
Exemplo n.º 30
0
        public override object Read(ref ProtoReader.State state, object value)
        {
            var category = GetCategory();

            switch (category)
            {
            case SerializerFeatures.CategoryMessageWrappedAtRoot:
            case SerializerFeatures.CategoryMessage:
                return(state.ReadMessage <T>(default, TypeHelper <T> .FromObject(value), CustomSerializer));

            case SerializerFeatures.CategoryScalar:
                return(CustomSerializer.Read(ref state, TypeHelper <T> .FromObject(value)));

            default:
                category.ThrowInvalidCategory();
                return(default);
            }
        }