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)); }
/// <summary> /// <para>从指定的<paramref name="stream"/>反序列化给定类型的值</para> /// <para>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 object Deserialize(Stream stream, Type type, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken); return(Deserialize(ref context, stream, type)); }
/// <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); }
/// <summary> /// <para>从指定的<paramref name="stream"/>反序列化给定类型的值</para> /// <para>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 T Deserialize <T>(Stream stream, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { BssomDeserializeContext context = new BssomDeserializeContext(option, cancellationToken); return(Deserialize <T>(ref context, stream)); }
public void DepthStep(ref BssomDeserializeContext context) { if (context.Depth >= MaximumObjectGraphDepth) { ThrowInsufficientExecutionStackException(); } context.Depth++; }
/// <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)); }
/// <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)); }
/// <summary> /// <para>从指定的<paramref name="stream"/>反序列化给定类型的值</para> /// <para>Deserializes the value of the given type from the specified <paramref name="stream"/></para> /// </summary> /// <param name="context">反序列化所需要的上下文. The context required for the deserialization </param> /// <param name="stream">要进行反序列化的流. The stream to deserialize from</param> /// <returns>反序列化的值. The deserialized value</returns> public static T Deserialize <T>(ref BssomDeserializeContext context, Stream stream) { if (TryDeserializeFromMemoryStream <T>(ref context, stream, out T result)) { return(result); } StreamDeserializeAux aux = new StreamDeserializeAux(stream); result = Deserialize <T>(ref context, aux.GetBssomBuffer()); aux.Dispose(); return(result); }
private static bool TryDeserializeFromMemoryStream(ref BssomDeserializeContext context, Stream stream, Type type, out object t) { #if NETSTANDARD && !NET45 if (stream is MemoryStream ms && ms.TryGetBuffer(out ArraySegment <byte> streamBuffer)) { var buffer = new SimpleBufferWriter(streamBuffer.Array, streamBuffer.Offset, streamBuffer.Count); t = Deserialize(ref context, buffer, type); stream.Seek(buffer.Position, SeekOrigin.Current); return(true); } #endif t = default; return(false); }
/// <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); }
/// <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 binary array to deserialize from</param> /// <param name="bufOffset">数组的起始位置. array start</param> /// <param name="readSize">读取的字节数. read buffer size</param> /// <param name="type">要反序列化的类型. The type to deserialize</param> /// <returns>反序列化的值. The deserialized value</returns> public static object Deserialize(ref BssomDeserializeContext context, byte[] buffer, int bufOffset, out int readSize, Type type) { if (buffer == null) { throw new ArgumentException(nameof(buffer)); } if (bufOffset > buffer.Length - 1) { throw new ArgumentException(nameof(bufOffset)); } SimpleBuffer buf = new SimpleBuffer(buffer, bufOffset); object value = Deserialize(ref context, buf, type); readSize = (int)buf.Position; return(value); }
/// <summary> /// <para>从指定的<paramref name="stream"/>反序列化给定类型的值</para> /// <para>Deserializes the value of the given type from the specified <paramref name="stream"/></para> /// </summary> /// <param name="context">反序列化所需要的上下文. The context required for the deserialization </param> /// <param name="stream">反序列化所需要的的缓冲区. The stream to deserialize from</param> /// <param name="type">要反序列化的类型. The type to deserialize</param> /// <returns>反序列化的值. The deserialized value</returns> public static object Deserialize(ref BssomDeserializeContext context, Stream stream, Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (TryDeserializeFromMemoryStream(ref context, stream, type, out object result)) { return(result); } StreamDeserializeAux aux = new StreamDeserializeAux(stream); result = Deserialize(ref context, aux.GetBssomBuffer(), type); aux.Dispose(); return(result); }
/// <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); }
/// <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)); }