示例#1
0
        public static object Deserialize(Type type, ref BssomReader reader, ref BssomDeserializeContext context)
        {
            object formatter = context.Option.FormatterResolver.GetFormatterWithVerify(type);

            if (!DeserializerDelegates.TryGetValue(type, out DeserializeMethod deserializerDelegate))
            {
                Type formatterType         = typeof(IBssomFormatter <>).MakeGenericType(type);
                ParameterExpression param0 = Expression.Parameter(typeof(object), "formatter");
                ParameterExpression param1 = Expression.Parameter(typeof(BssomReader).MakeByRefType(), "reader");
                ParameterExpression param2 = Expression.Parameter(typeof(BssomDeserializeContext).MakeByRefType(), "context");

                MethodInfo deserializeMethod = formatterType.GetRuntimeMethod(nameof(Deserialize), new[] { typeof(BssomReader).MakeByRefType(), typeof(BssomDeserializeContext).MakeByRefType() });

                //(object)IBssomFormatter<T>.Deserialize(ref reader,option);
                UnaryExpression body = Expression.Convert(Expression.Call(
                                                              Expression.Convert(param0, formatterType),
                                                              deserializeMethod,
                                                              param1,
                                                              param2), typeof(object));

                deserializerDelegate = Expression.Lambda <DeserializeMethod>(body, param0, param1, param2).Compile();
                DeserializerDelegates.TryAdd(type, deserializerDelegate);
            }

            return(deserializerDelegate(formatter, ref reader, ref context));
        }
示例#2
0
        /// <summary>
        /// <para>从指定的<paramref name="buffer"/>反序列化给定类型的值</para>
        /// <para>Deserializes the value of the given type from the specified <paramref name="buffer"/></para>
        /// </summary>
        /// <param name="buffer">反序列化所需要的的二进制数组. The binary array to deserialize from</param>
        /// <param name="bufOffset">数组的起始位置. array start</param>
        /// <param name="readSize">读取的字节数. read buffer size</param>
        /// <param name="option">使用的配置,如果为<c>null</c>,则使用默认配置. The options. Use <c>null</c> to use default options</param>
        /// <param name="cancellationToken">取消该操作的令牌. The token to cancel the operation</param>
        /// <returns>反序列化的值. The deserialized value</returns>
        public static T Deserialize <T>(byte[] buffer, int bufOffset, out int readSize, BssomSerializerOptions option = null, CancellationToken cancellationToken = default)
        {
            if (buffer == null)
            {
                throw new ArgumentException(nameof(buffer));
            }

            if (bufOffset > buffer.Length - 1)
            {
                throw new ArgumentException(nameof(bufOffset));
            }

            if (option == null)
            {
                option = BssomSerializerOptions.Default;
            }

            BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken);
            SimpleBuffer            buf     = new SimpleBuffer(buffer, bufOffset);
            BssomReader             reader  = new BssomReader(buf);
            T value = context.Option.FormatterResolver.GetFormatterWithVerify <T>().Deserialize(ref reader, ref context);

            readSize = (int)buf.Position;
            return(value);
        }
示例#3
0
        /// <summary>
        /// <para>从指定的<paramref name="buffer"/>反序列化给定类型的值</para>
        /// <para>Deserializes the value of the given type from the specified <paramref name="buffer"/></para>
        /// </summary>
        /// <param name="context">反序列化所需要的上下文. The context required for the deserialization </param>
        /// <param name="buffer">反序列化所需要的的缓冲区. The buffer to deserialize from</param>
        /// <param name="type">要反序列化的类型. The type to deserialize</param>
        /// <returns>反序列化的值. The deserialized value</returns>
        public static object Deserialize(ref BssomDeserializeContext context, IBssomBuffer buffer, Type type)
        {
            if (context.Option == null)
            {
                context.Option = BssomSerializerOptions.Default;
            }

            BssomReader reader = new BssomReader(buffer);

            return(RawObjectDeserializer.Deserialize(type, ref reader, ref context));
        }
示例#4
0
        /// <summary>
        /// <para>从指定的<paramref name="buffer"/>反序列化给定类型的值</para>
        /// <para>Deserializes the value of the given type from the specified <paramref name="buffer"/></para>
        /// </summary>
        /// <param name="context">反序列化所需要的上下文. The context required for the deserialization </param>
        /// <param name="buffer">要进行反序列化的缓冲区. The buffer to deserialize from</param>
        /// <returns>反序列化的值. The deserialized value</returns>
        public static T Deserialize <T>(ref BssomDeserializeContext context, IBssomBuffer buffer)
        {
            if (context.Option == null)
            {
                context.Option = BssomSerializerOptions.Default;
            }

            BssomReader reader = new BssomReader(buffer);

            return(context.Option.FormatterResolver.GetFormatterWithVerify <T>().Deserialize(ref reader, ref context));
        }
示例#5
0
        public static BssomFieldOffsetInfo IndexOf(IBssomBuffer buffer, IIndexOfInputSource indexOfInputSource)
        {
            if (buffer is null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (indexOfInputSource is null)
            {
                throw new ArgumentNullException(nameof(indexOfInputSource));
            }

            BssomReader          reader = new BssomReader(buffer);
            BssomFieldOffsetInfo info   = new BssomFieldOffsetInfo();

            indexOfInputSource.Reset();
            if (!indexOfInputSource.MoveNext())
            {
                throw BssomSerializationOperationException.InputDataSouceIsEmpty();
            }

Next:
            byte bssOjectType = reader.SkipBlankCharacterAndReadBssomType();

            switch (bssOjectType)
            {
            case BssomType.Map1:
            {
                object key = indexOfInputSource.CurrentMapKey();
                if (!BssMapKeyResolverProvider.TryGetBssMapKeyResolver(key.GetType(), out IBssMapKeyResolver resolver))
                {
                    throw BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(key.GetType());
                }

                reader.SkipVariableNumber();        //Skip Length
                int count = reader.ReadVariableNumber();
                Raw64BytesISegment keyISegment = resolver.GetMap1KeySegment(key);
                for (int i = 0; i < count; i++)
                {
                    int keyLength = reader.GetMap1KeyLength();
                    if (keyISegment.DataLen != keyLength)
                    {
                        reader.BssomBuffer.Seek(keyLength, BssomSeekOrgin.Current); //Advance Key
                        reader.SkipObject();                                        //Skip Value
                        continue;
                    }

                    //determine if the key is equal
                    bool keyIsEqual = true;
                    for (int z = 0; z < keyISegment.Length; z++)
                    {
                        if (keyISegment[z] != reader.ReadRaw64(ref keyLength))
                        {
                            reader.BssomBuffer.Seek(keyLength, BssomSeekOrgin.Current); //Advance remaining keyLength
                            reader.SkipObject();                                        //Skip Value
                            keyIsEqual = false;
                            break;
                        }
                    }

                    if (keyIsEqual)
                    {
                        if (!indexOfInputSource.MoveNext())
                        {
                            info.Offset       = reader.Position;
                            info.IsArray1Type = false;
                            return(info);
                        }
                        else
                        {
                            goto Next;
                        }
                    }
                }
                if (indexOfInputSource.MoveNext())
                {
                    throw BssomSerializationOperationException.BssomMapIsNull(key);
                }

                info.Offset = -1;
                return(info);
            }

            case BssomType.Map2:
            {
                object key = indexOfInputSource.CurrentMapKey();
                if (!BssMapKeyResolverProvider.TryGetBssMapKeyResolver(key.GetType(), out IBssMapKeyResolver resolver))
                {
                    throw BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(key.GetType());
                }

                UInt64BytesISegment keyISegment = resolver.GetMap2KeySegment(key);
                BssMapHeadPackInfo  aprp        = BssMapHeadPackInfo.Create(ref reader);
                if (aprp.MapHead.ElementCount > 0)
                {
                    ref byte refb = ref reader.BssomBuffer.TryReadFixedRef(aprp.MapHead.RouteLength, out bool haveEnoughSizeAndCanBeFixed);
                    if (haveEnoughSizeAndCanBeFixed)
                    {
                        if (BssMapObjMarshalReader.TrySeek(keyISegment, resolver.KeyType, resolver.KeyIsNativeType, ref aprp, ref reader, ref refb))
                        {
                            if (!indexOfInputSource.MoveNext())
                            {
                                info.Offset       = reader.Position;
                                info.IsArray1Type = false;
                                return(info);
                            }
                            else
                            {
                                goto Next;
                            }
                        }
                    }
                    else
                    {
                        if (BssMapObjMarshalReader.TrySeekSlow(keyISegment, resolver.KeyType, resolver.KeyIsNativeType, ref aprp, ref reader))
                        {
                            if (!indexOfInputSource.MoveNext())
                            {
                                info.Offset       = reader.Position;
                                info.IsArray1Type = false;
                                return(info);
                            }
                            else
                            {
                                goto Next;
                            }
                        }
                    }
                }
                if (indexOfInputSource.MoveNext())
                {
                    throw BssomSerializationOperationException.BssomMapIsNull(key);
                }

                info.Offset = -1;
                return(info);
            }