Example #1
0
            public IDescriptorSerializerContext StoreNewDescriptors(AbstractBufferedWriter writer, object obj)
            {
                if (obj == null)
                {
                    return(this);
                }
                InfoForType infoForType;
                var         iKnowDescriptor = obj as IKnowDescriptor;

                if (iKnowDescriptor != null)
                {
                    var descriptor = iKnowDescriptor.GetDescriptor();
                    if (!_typeOrDescriptor2InfoMap.TryGetValue(descriptor, out infoForType) &&
                        !_typeSerializersMapping._typeOrDescriptor2Info.TryGetValue(descriptor, out infoForType))
                    {
                        infoForType = new InfoForType {
                            Id = 0, Descriptor = descriptor
                        };
                    }
                }
                else
                {
                    var objType = obj.GetType();
                    if (!_typeOrDescriptor2InfoMap.TryGetValue(objType, out infoForType) &&
                        !_typeSerializersMapping._typeOrDescriptor2Info.TryGetValue(objType, out infoForType))
                    {
                        var descriptor = _typeSerializers.DescriptorOf(objType);
                        if (_typeOrDescriptor2InfoMap.TryGetValue(descriptor, out infoForType))
                        {
                            _typeOrDescriptor2InfoMap[objType] = infoForType;
                        }
                        else if (_typeSerializersMapping._typeOrDescriptor2Info.TryGetValue(descriptor, out infoForType))
                        {
                            _typeSerializersMapping._typeOrDescriptor2Info[objType] = infoForType;
                        }
                        else
                        {
                            infoForType = new InfoForType {
                                Id = 0, Descriptor = descriptor
                            };
                        }
                    }
                }
                if (infoForType.Id == 0)
                {
                    AddDescriptor(infoForType);
                }
                if (!infoForType.KnownNewTypeDiscoverer)
                {
                    infoForType.NewTypeDiscoverer      = _typeSerializers.GetNewDescriptorSaver(infoForType.Descriptor);
                    infoForType.KnownNewTypeDiscoverer = true;
                }
                var action = infoForType.NewTypeDiscoverer;

                if (action != null)
                {
                    action(obj, this);
                }
                return(this);
            }
Example #2
0
 void AddBuildInTypes()
 {
     _id2DescriptorMap.Add(null); // 0 = null
     _id2DescriptorMap.Add(null); // 1 = back reference
     foreach (var predefinedType in BasicSerializersFactory.TypeDescriptors)
     {
         var infoForType = new InfoForType {
             Id = _id2DescriptorMap.Count, Descriptor = predefinedType
         };
         _typeOrDescriptor2Info[predefinedType] = infoForType;
         _id2DescriptorMap.Add(infoForType);
     }
     while (_id2DescriptorMap.Count < ReservedBuildinTypes)
     {
         _id2DescriptorMap.Add(null);
     }
 }
Example #3
0
            public void AddDescriptor(InfoForType infoForType)
            {
                infoForType.Id = _typeSerializersMapping._id2DescriptorMap.Count + _id2InfoMap.Count;
                _typeOrDescriptor2InfoMap.Add(infoForType.Descriptor, infoForType);
                _id2InfoMap.Add(infoForType);
                var             idx = 0;
                ITypeDescriptor nestedDescriptor;

                while ((nestedDescriptor = infoForType.Descriptor.NestedType(idx)) != null)
                {
                    int _;
                    if (!TryDescriptor2Id(nestedDescriptor, out _))
                    {
                        AddDescriptor(new InfoForType {
                            Descriptor = nestedDescriptor
                        });
                    }
                    idx++;
                }
            }
Example #4
0
        public IDescriptorSerializerContext StoreNewDescriptors(object?obj)
        {
            if (obj == null)
            {
                return(this);
            }
            InfoForType infoForType;
            var         objType = obj.GetType();

            if (obj is IKnowDescriptor iKnowDescriptor)
            {
                var descriptor = iKnowDescriptor.GetDescriptor();
                if (!_typeOrDescriptor2Info.TryGetValue(descriptor, out infoForType))
                {
                    infoForType = new InfoForType {
                        Id = 0, Descriptor = descriptor
                    };
                }
            }
            else
            {
                if (!_typeOrDescriptor2Info.TryGetValue(objType, out infoForType))
                {
                    var descriptor = _typeSerializers.DescriptorOf(objType);
                    if (!_typeOrDescriptor2Info.TryGetValue(descriptor !, out infoForType))
                    {
                        infoForType = new InfoForType {
                            Id = 0, Descriptor = descriptor
                        };
                    }
                    else
                    {
                        _typeOrDescriptor2Info[objType] = infoForType;
                    }
                }
            }
Example #5
0
        static void StoreObjectCore(TypeSerializers typeSerializers, AbstractBufferedWriter writer, object obj, InfoForType infoForType, ITypeSerializersLightMapping mapping)
        {
            writer.WriteVUInt32((uint)infoForType.Id);
            if (!infoForType.KnownSimpleSaver)
            {
                infoForType.SimpleSaver      = typeSerializers.GetSimpleSaver(infoForType.Descriptor);
                infoForType.KnownSimpleSaver = true;
            }
            var simpleSaver = infoForType.SimpleSaver;

            if (simpleSaver != null)
            {
                simpleSaver(writer, obj);
                return;
            }
            if (!infoForType.KnownComplexSaver)
            {
                infoForType.ComplexSaver      = typeSerializers.GetComplexSaver(infoForType.Descriptor);
                infoForType.KnownComplexSaver = true;
            }
            var complexSaver = infoForType.ComplexSaver;
            var ctx          = new TypeBinarySerializerContext(mapping, writer, obj);

            complexSaver(writer, ctx, obj);
        }
Example #6
0
        public void LoadTypeDescriptors(AbstractBufferedReader reader)
        {
            var typeId      = reader.ReadVUInt32();
            var firstTypeId = typeId;

            while (typeId != 0)
            {
                if (typeId < firstTypeId)
                {
                    firstTypeId = typeId;
                }
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(_typeSerializers, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (typeId >= _id2DescriptorMap.Count)
                {
                    _id2DescriptorMap.Add(null);
                }
                if (_id2DescriptorMap[(int)typeId] == null)
                {
                    _id2DescriptorMap[(int)typeId] = new InfoForType {
                        Id = (int)typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVUInt32();
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null ? _id2DescriptorMap[(int)placeHolderDescriptor.TypeId].Descriptor : d);
                });
            }
            // This additional cycle is needed to fill names of recursive structures
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d => d);
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                var infoForType = _id2DescriptorMap[(int)i];
                var descriptor  = _typeSerializers.MergeDescriptor(infoForType.Descriptor);
                infoForType.Descriptor             = descriptor;
                _typeOrDescriptor2Info[descriptor] = infoForType;
            }
        }

        ITypeDescriptor NestedDescriptorReader(AbstractBufferedReader reader)
        {
            var typeId = reader.ReadVUInt32();

            if (typeId < _id2DescriptorMap.Count)
            {
                var infoForType = _id2DescriptorMap[(int)typeId];
                if (infoForType != null)
                {
                    return(infoForType.Descriptor);
                }
            }
            return(new PlaceHolderDescriptor(typeId));
        }
Example #7
0
        public IDescriptorSerializerContext StoreNewDescriptors(AbstractBufferedWriter writer, object obj)
        {
            if (obj == null)
            {
                return(this);
            }
            InfoForType infoForType;
            var         iKnowDescriptor = obj as IKnowDescriptor;

            if (iKnowDescriptor != null)
            {
                var descriptor = iKnowDescriptor.GetDescriptor();
                if (!_typeOrDescriptor2Info.TryGetValue(descriptor, out infoForType))
                {
                    infoForType = new InfoForType {
                        Id = 0, Descriptor = descriptor
                    };
                }
            }
            else
            {
                var objType = obj.GetType();
                if (!_typeOrDescriptor2Info.TryGetValue(objType, out infoForType))
                {
                    var descriptor = _typeSerializers.DescriptorOf(objType);
                    if (!_typeOrDescriptor2Info.TryGetValue(descriptor, out infoForType))
                    {
                        infoForType = new InfoForType {
                            Id = 0, Descriptor = descriptor
                        };
                    }
                    else
                    {
                        _typeOrDescriptor2Info[objType] = infoForType;
                    }
                }
            }
            DescriptorSerializerContext ctx = null;

            if (infoForType.Id == 0)
            {
                ctx = new DescriptorSerializerContext(this, writer);
                ctx.AddDescriptor(infoForType);
            }
            if (!infoForType.KnownNewTypeDiscoverer)
            {
                infoForType.NewTypeDiscoverer      = _typeSerializers.GetNewDescriptorSaver(infoForType.Descriptor, obj.GetType());
                infoForType.KnownNewTypeDiscoverer = true;
            }
            var action = infoForType.NewTypeDiscoverer;

            if (action != null)
            {
                if (ctx == null)
                {
                    ctx = new DescriptorSerializerContext(this, writer);
                }
                action(obj, ctx);
            }
            if (ctx != null && ctx.SomeTypeStored)
            {
                return(ctx);
            }
            return(this);
        }