/// <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)); }
/// <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>Serializes a given value with the <paramref name="buffer"/></para> /// </summary> /// <param name="buffer">序列化所使用的buffer. The value to serialize</param> /// <param name="bufOffset">buffer起始写入的偏移量. The offset that the buffer starts writing to</param> /// <param name="value">要序列化的值. The value to serialize</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 number of bytes spent on serialization</returns> public static int Serialize <T>(ref byte[] buffer, int bufOffset, T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (bufOffset > buffer.Length - 1) { throw new ArgumentException(nameof(bufOffset)); } if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); using (ExpandableBufferWriter exbuffer = new ExpandableBufferWriter(buffer, bufOffset)) { BssomWriter writer = new BssomWriter(exbuffer); option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); buffer = exbuffer.GetBufferedArrayWithKeepFirstBuffer(); return((int)exbuffer.Buffered); } }
/// <summary> /// <para>在不进行序列化的情况下,获取对象被序列化后的二进制数据大小</para> /// <para>In the case of no serialization,Gets the size of the binary data after the object is serialized</para> /// </summary> /// <param name="value">要获取序列化大小的值. The value to get the serialization size</param> /// <param name="option">使用的配置,如果为<c>null</c>,则使用默认配置. The options. Use <c>null</c> to use default options</param> /// <returns>对象被序列化后的数据大小. The size of the data after the object is serialized</returns> public static int Size <T>(T value, BssomSerializerOptions option = null) { if (option == null) { option = BssomSerializerOptions.Default; } BssomSizeContext sizeContext = new BssomSizeContext(option); return(option.FormatterResolver.GetFormatterWithVerify <T>().Size(ref sizeContext, value)); }
/// <summary> /// <para>将给定的值序列化到<paramref name="stream"/>中</para> /// <para>Serializes a given value with the specified <paramref name="stream"/></para> /// </summary> /// <param name="stream">要序列化的流. The stream to serialize with</param> /// <param name="value">要序列化的值. The value to serialize</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>具有序列化值的字节数组. A byte array with the serialized value</returns> public static void Serialize <T>(Stream stream, T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); Serialize(ref context, value, stream); }
/// <summary> /// <para>获取将<see cref="FormatterResolver"/>属性设置为新值的<see cref="BssomSerializerOptions"/>副本</para> /// <para>Get a copy of <see cref="BssomSerializerOptions"/> with the <see cref="FormatterResolver"/> attribute set to the new value</para> /// </summary> /// <param name="resolver"><see cref="FormatterResolver"/>的新值. The new value for the <seealso cref="FormatterResolver"/></param> /// <returns>一个新的实例,实例内的其它属性都没有改变. The new instance,none of the other properties within the instance change</returns> public BssomSerializerOptions WithFormatterResolver(IFormatterResolver resolver) { if (FormatterResolver == resolver) { return(this); } BssomSerializerOptions result = Clone(); result.FormatterResolver = resolver; return(result); }
/// <summary> /// <para>获取将<see cref="IsPriorityToDeserializeObjectAsBssomValue"/>属性设置为新值的<see cref="BssomSerializerOptions"/>副本</para> /// <para>Get a copy of <see cref="BssomSerializerOptions"/> with the <see cref="IsPriorityToDeserializeObjectAsBssomValue"/> attribute set to the new value</para> /// </summary> /// <param name="isPriorityToDeserializeObjectAsBssomValue"><see cref="IsPriorityToDeserializeObjectAsBssomValue"/>的新值. The new value for the <seealso cref="IsPriorityToDeserializeObjectAsBssomValue"/></param> /// <returns>一个新的实例,实例内的其它属性都没有改变. The new instance,none of the other properties within the instance change</returns> public BssomSerializerOptions WithIsPriorityToDeserializeObjectAsBssomValue(bool isPriorityToDeserializeObjectAsBssomValue) { if (IsPriorityToDeserializeObjectAsBssomValue == isPriorityToDeserializeObjectAsBssomValue) { return(this); } BssomSerializerOptions result = Clone(); result.IsPriorityToDeserializeObjectAsBssomValue = isPriorityToDeserializeObjectAsBssomValue; return(result); }
/// <summary> /// <para>获取将<see cref="IsUseStandardDateTime"/>属性设置为新值的<see cref="BssomSerializerOptions"/>副本</para> /// <para>Get a copy of <see cref="BssomSerializerOptions"/> with the <see cref="IsUseStandardDateTime"/> attribute set to the new value</para> /// </summary> /// <param name="isUseStandardDateTime"><see cref="IsUseStandardDateTime"/>的新值. The new value for the <seealso cref="IsUseStandardDateTime"/></param> /// <returns>一个新的实例,实例内的其它属性都没有改变. The new instance,none of the other properties within the instance change</returns> public BssomSerializerOptions WithIsUseStandardDateTime(bool isUseStandardDateTime) { if (IsUseStandardDateTime == isUseStandardDateTime) { return(this); } BssomSerializerOptions result = Clone(); result.IsUseStandardDateTime = isUseStandardDateTime; return(result); }
/// <summary> /// <para>获取将<see cref="IDictionaryIsSerializeMap1Type"/>属性设置为新值的<see cref="BssomSerializerOptions"/>副本</para> /// <para>Get a copy of <see cref="BssomSerializerOptions"/> with the <see cref="IDictionaryIsSerializeMap1Type"/> attribute set to the new value</para> /// </summary> /// <param name="iDictionaryIsSerializeMap1Type"><see cref="IDictionaryIsSerializeMap1Type"/>的新值. The new value for the <seealso cref="IDictionaryIsSerializeMap1Type"/></param> /// <returns>一个新的实例,实例内的其它属性都没有改变. The new instance,none of the other properties within the instance change</returns> public BssomSerializerOptions WithIDictionaryIsSerializeMap1Type(bool iDictionaryIsSerializeMap1Type) { if (IDictionaryIsSerializeMap1Type == iDictionaryIsSerializeMap1Type) { return(this); } BssomSerializerOptions result = Clone(); result.IDictionaryIsSerializeMap1Type = iDictionaryIsSerializeMap1Type; return(result); }
/// <summary> /// Initializes a new instance of the <see cref="BssomSerializerOptions"/> struct. /// </summary> protected BssomSerializerOptions(BssomSerializerOptions copyFrom) { if (copyFrom == null) { throw new ArgumentNullException(nameof(copyFrom)); } Security = copyFrom.Security; FormatterResolver = copyFrom.FormatterResolver; IsPriorityToDeserializeObjectAsBssomValue = copyFrom.IsPriorityToDeserializeObjectAsBssomValue; IsUseStandardDateTime = copyFrom.IsUseStandardDateTime; IDictionaryIsSerializeMap1Type = copyFrom.IDictionaryIsSerializeMap1Type; }
/// <summary> /// <para>将给定的值序列化到内置的缓冲区中</para> /// <para>Serializes a given value with the built-in buffer</para> /// </summary> /// <param name="value">要序列化的值. The value to serialize</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>具有序列化值的字节数组. A byte array with the serialized value</returns> public static byte[] Serialize <T>(T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); using (ExpandableBufferWriter buffer = ExpandableBufferWriter.CreateGlobar()) { BssomWriter writer = new BssomWriter(buffer); option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); return(buffer.GetBufferedArray()); } }
/// <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>获取将<see cref="Security"/>属性设置为新值的<see cref="BssomSerializerOptions"/>副本</para> /// <para>Get a copy of <see cref="BssomSerializerOptions"/> with the <see cref="Security"/> attribute set to the new value</para> /// </summary> /// <param name="security"><see cref="Security"/>的新值. The new value for the <seealso cref="Security"/></param> /// <returns>一个新的实例,实例内的其它属性都没有改变. The new instance,none of the other properties within the instance change</returns> public BssomSerializerOptions WithSecurity(BssomSecurity security) { if (security is null) { throw new ArgumentNullException(nameof(security)); } if (Security == security) { return(this); } BssomSerializerOptions result = Clone(); result.Security = security; return(result); }
/// <summary> /// <para>将给定的值序列化到<paramref name="bufferWriter"/></para> /// <para>Serializes a given value with the specified <paramref name="bufferWriter"/></para> /// </summary> /// <param name="value">要序列化的值. The value to serialize</param> /// <param name="bufferWriter">要序列化的缓冲区写入器. The buffer writer to serialize with</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> public static void Serialize <T>(T value, IBssomBufferWriter bufferWriter, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (bufferWriter == null) { throw new ArgumentNullException(nameof(bufferWriter)); } if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); BssomWriter writer = new BssomWriter(bufferWriter); option.FormatterResolver.GetFormatterWithVerify <T>().Serialize(ref writer, ref context, value); }
/// <summary> /// <para>异步的将给定的值序列化到<paramref name="stream"/>中</para> /// <para>Asynchronously serializes a given value with the specified <paramref name="stream"/></para> /// </summary> /// <param name="stream">要序列化的流. The stream to serialize with</param> /// <param name="value">要序列化的值. The value to serialize</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>具有序列化值的字节数组. A byte array with the serialized value</returns> public static async Task SerializeAsync <T>(Stream stream, T value, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { if (option == null) { option = BssomSerializerOptions.Default; } BssomSerializeContext context = new BssomSerializeContext(option, cancellationToken); using (ExpandableBufferWriter buffer = ExpandableBufferWriter.CreateGlobar()) { Serialize(ref context, value, buffer); try { await buffer.CopyToAsync(stream, context.CancellationToken); } catch (Exception ex) { BssomSerializationOperationException.CopyStreamError(ex); } } }
/// <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)); }
/// <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="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, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { return(Deserialize <T>(buffer, 0, out int readSize, option, cancellationToken)); }
/// <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); }
public static IEnumerable <KeyValuePair <TKey, BssomFieldOffsetInfo> > ReadAllKeysByMapType <TKey>(byte[] buffer, int start, int end, BssomFieldOffsetInfo offsetInfo, BssomSerializerOptions option = null) { return(ReadAllKeysByMapType <TKey>(new SimpleBufferWriter(buffer, start, end), offsetInfo, option)); }
public static bool TryWrite <T>(byte[] buffer, int start, int end, BssomFieldOffsetInfo offsetInfo, T value, BssomSerializerOptions option = null) { return(TryWrite(new SimpleBufferWriter(buffer, start, end), offsetInfo, value, option)); }
public static T ReadValue <T>(byte[] buffer, int start, int end, BssomFieldOffsetInfo offsetInfo, BssomSerializerOptions option = null) { return(ReadValue <T>(new SimpleBufferWriter(buffer, start, end), offsetInfo, option)); }
/// <summary> /// <para>尝试在指定的位置对缓冲区进行对象写入</para> /// <para>Attempt to write an object to the buffer at the specified location</para> /// </summary> /// <typeparam name="T">被写入元素的类型. The type of element being written</typeparam> /// <param name="offsetInfo">用于写入元素的位置信息. Used to write the position information of the element</param> /// <param name="value">被写入的对象. Value being written</param> /// <param name="option">过程中所使用的配置. Configuration used in the process</param> /// <returns> /// <para>如果对象的二进制大小小于对象在缓冲区中的大小,则成功写入,返回true</para> /// <para>If the binary size of the value is smaller than the size of the member in the buffer, it is successfully written and true is returned</para> /// </returns> public bool TryWrite <T>(BssomFieldOffsetInfo offsetInfo, T value, BssomSerializerOptions option = null) { return(TryWrite <T>(bufferWriter, offsetInfo, value, option)); }
/// <summary> /// 通过指定缓冲区的位置信息来读取Map对象的字段位置信息. Read the field location information of the Map object by specifying the location information of the buffer /// </summary> /// <typeparam name="TKey">Map对象的Key类型,如不确认可以为<see cref="object"/>. The Key type of the Map object, if not confirmed, it can be <seealso cref="object"/></typeparam> /// <param name="offsetInfo">用于读取的位置信息. Location information for reading</param> /// <param name="option">过程中所使用的配置. Configuration used in the process</param> /// <returns>Map对象的字段位置信息. Field location information of the Map object</returns> /// <exception cref="BssomSerializationOperationException">缓冲区<paramref name="offsetInfo"/>位置处的对象并不是Map类型,类型读取错误. The object at the position of the buffer <paramref name="offsetInfo"/> is not a Map type, and the type is read incorrectly</exception> public IEnumerable <KeyValuePair <TKey, BssomFieldOffsetInfo> > ReadAllKeysByMapType <TKey>(BssomFieldOffsetInfo offsetInfo, BssomSerializerOptions option = null) { return(ReadAllKeysByMapType <TKey>(bufferWriter.GetBssomBuffer(), offsetInfo, option)); }
/// <summary> /// 通过指定缓冲区的位置信息来读取元素. Read the element by specifying the location information of the buffer /// </summary> /// <typeparam name="T">被读取元素的类型,如不确认可以为<see cref="object"/>. The type of the read element, if not confirmed, it can be <seealso cref="object"/></typeparam> /// <param name="offsetInfo">用于读取的位置信息. Location information for reading</param> /// <param name="option">过程中所使用的配置. Configuration used in the process</param> /// <returns>被读取的元素. Element being read</returns> public T ReadValue <T>(BssomFieldOffsetInfo offsetInfo, BssomSerializerOptions option = null) { return(ReadValue <T>(bufferWriter.GetBssomBuffer(), offsetInfo, option)); }
/// <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="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(byte[] buffer, int bufOffset, out int readSize, Type type, BssomSerializerOptions option = null, CancellationToken cancellationToken = default) { 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(buf, type, option, cancellationToken); readSize = (int)buf.Position; return(value); }