Example #1
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));
        }
Example #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="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));
        }
Example #3
0
        /// <summary>
        /// <para>异步的从指定的<paramref name="stream"/>反序列化给定类型的值</para>
        /// <para>Asynchronously deserializes the value of the given type from the specified <paramref name="stream"/></para>
        /// </summary>
        /// <param name="stream">反序列化所需要的的流. The stream to deserialize from</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 async Task <T> DeserializeAsync <T>(Stream stream, BssomSerializerOptions option = null,
                                                          CancellationToken cancellationToken          = default)
        {
            BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken);

            if (TryDeserializeFromMemoryStream <T>(ref context, stream, out T result))
            {
                return(result);
            }

            StreamDeserializeAux aux      = new StreamDeserializeAux(stream);
            IBssomBuffer         bssomBuf = await aux.GetBssomBufferAsync().ConfigureAwait(false);

            result = Deserialize <T>(ref context, bssomBuf);
            aux.Dispose();
            return(result);
        }
Example #4
0
        /// <summary>
        /// <para>异步的从指定的<paramref name="stream"/>反序列化给定类型的值</para>
        /// <para>Asynchronously deserializes the value of the given type from the specified <paramref name="stream"/></para>
        /// </summary>
        /// <param name="stream">反序列化所需要的的流. The stream to deserialize from</param>
        /// <param name="type">要反序列化的类型. The type to deserialize</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 async Task <object> DeserializeAsync(Stream stream, Type type, BssomSerializerOptions option = null,
                                                           CancellationToken cancellationToken = default)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken);

            if (TryDeserializeFromMemoryStream(ref context, stream, out object result))
            {
                return(result);
            }

            StreamDeserializeAux aux      = new StreamDeserializeAux(stream);
            IBssomBuffer         bssomBuf = await aux.GetBssomBufferAsync().ConfigureAwait(false);

            result = Deserialize(ref context, bssomBuf, type);
            aux.Dispose();
            return(result);
        }
Example #5
0
 public BssomReader(IBssomBuffer bssomBuffer)
 {
     BssomBuffer = bssomBuffer;
 }
        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);
            }
Example #7
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 buffer to deserialize from</param>
        /// <param name="type">要反序列化的类型. The type to deserialize</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 object Deserialize(IBssomBuffer buffer, Type type, BssomSerializerOptions option = null, CancellationToken cancellationToken = default)
        {
            BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken);

            return(Deserialize(ref context, buffer, type));
        }
Example #8
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 buffer to deserialize from</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>(IBssomBuffer buffer, BssomSerializerOptions option = null, CancellationToken cancellationToken = default)
        {
            BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken);

            return(Deserialize <T>(ref context, buffer));
        }