internal void EnsureNativeType(byte nativeType) { if (ReadBssomType() != BssomType.NativeCode || ReadBssomType() != nativeType) { BssomSerializationOperationException.UnexpectedCodeRead(Position); } }
internal void EnsureNativeTypeWithSkipBlankCharacter(byte nativeType) { if (SkipBlankCharacterAndReadBssomType() != BssomType.NativeCode || ReadBssomType() != nativeType) { BssomSerializationOperationException.UnexpectedCodeRead(Position); } }
public void EnsureType(byte buildInType) { byte t = ReadOne <byte>(); if (t != buildInType) { BssomSerializationOperationException.UnexpectedCodeRead(t, Position); } }
internal void EnsureMapToken(BssMapRouteToken token) { BssMapRouteToken t = ReadOne <BssMapRouteToken>(); if (t != token) { BssomSerializationOperationException.UnexpectedCodeRead((byte)t, Position); } }
internal void SkipBlankCharacterAndEnsureType(byte buildInType) { byte t = SkipBlankCharacterAndReadBssomType(); if (t != buildInType) { BssomSerializationOperationException.UnexpectedCodeRead(t, Position); } }
public int GetCurrentArrayIndex() { if (curIsMapKey) { BssomSerializationOperationException.InputTypeAndDataIsInconsistent(GetCurrentSegmentString(), "Array"); } return(aryIndexNumber); }
public int CurrentArrayIndex() { Entry entry = entries.Current; if (entry.ValueIsMapKey) { BssomSerializationOperationException.InputTypeAndDataIsInconsistent(entry.Value.ToString(), "Array"); } return((int)entry.Value); }
public object CurrentMapKey() { Entry entry = entries.Current; if (!entry.ValueIsMapKey) { BssomSerializationOperationException.InputTypeAndDataIsInconsistent(entry.Value.ToString(), "Map"); } return(entry.Value); }
/// <summary> /// <para>将给定的值序列化到<paramref name="stream"/>中</para> /// <para>Serializes a given value with the specified <paramref name="stream"/></para> /// </summary> /// <param name="context">序列化所需要的上下文. The context required for the serialization </param> /// <param name="value">要序列化的值. The value to serialize</param> /// <param name="stream">要序列化的流. The stream to serialize with</param> public static void Serialize <T>(ref BssomSerializeContext context, T value, Stream stream) { using (ExpandableBufferWriter buffer = ExpandableBufferWriter.CreateGlobar()) { Serialize(ref context, value, buffer); try { buffer.CopyTo(stream, context.CancellationToken); } catch (Exception ex) { BssomSerializationOperationException.CopyStreamError(ex); } } }
public unsafe UInt64BytesISegment GetCurrentSegmentFromMap2StringKey() { if (!curIsMapKey) { BssomSerializationOperationException.InputTypeAndDataIsInconsistent(GetCurrentSegmentString(), "Map"); } byte[] buffer = GetBuffer(UTF8Encoding.UTF8.GetMaxByteCount(mapKeyLength)); fixed(char *pChar = _str) fixed(byte *pByte = &buffer[0]) { int length = UTF8Encoding.UTF8.GetBytes(pChar + mapKeyIndex, mapKeyLength, pByte, buffer.Length); return(new UInt64BytesISegment(buffer, length)); } }
internal bool TryReadNullWithEnsureBuildInType(byte buildInType) { byte type = SkipBlankCharacterAndPeekBssomType(); if (type == BssomType.NullCode) { BssomBuffer.SeekWithOutVerify(BssomBinaryPrimitives.NullSize, BssomSeekOrgin.Current); return(true); } else if (type == buildInType) { BssomBuffer.SeekWithOutVerify(BssomBinaryPrimitives.BuildInTypeCodeSize, BssomSeekOrgin.Current); return(false); } return(BssomSerializationOperationException.UnexpectedCodeRead <bool>(type, Position)); }
internal DateTime ReadDateTime() { byte type = SkipBlankCharacterAndReadBssomType(); switch (type) { case BssomType.TimestampCode: return(ReadStandDateTimeWithOutTypeHead()); case BssomType.NativeCode: EnsureType(NativeBssomType.DateTimeCode); return(ReadNativeDateTimeWithOutTypeHead()); default: throw BssomSerializationOperationException.UnexpectedCodeRead(type, BssomBuffer.Position); } }
internal void SkipStandDateTimeWithOutTypeHead() { byte code = BssomBuffer.ReadRef(1); if (code == 4) { BssomBuffer.Seek(5, BssomSeekOrgin.Current); } else if (code == 8) { BssomBuffer.Seek(9, BssomSeekOrgin.Current); } else if (code == 12) { BssomBuffer.Seek(13, BssomSeekOrgin.Current); } else { throw BssomSerializationOperationException.UnexpectedCodeRead(code, BssomBuffer.Position); } }
/// <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); } } }
private int GetSizeOfOneObjectToBuffer() { int size; byte objType = GetBssomTypeFromStreamPack.Get(stream, buffer); buffer[0] = objType; position++; if (!BssomBinaryPrimitives.TryGetPrimitiveTypeSizeFromStaticTypeSizes(objType, out size)) { switch (objType) { case BssomType.StringCode: size = (int)ReadVariableNumberCore(); break; case BssomType.NativeCode: { ReadStreamToBuffer(1); objType = buffer[position - 1]; if (objType == NativeBssomType.CharCode) { size = BssomBinaryPrimitives.CharSize; } else if (objType == NativeBssomType.DateTimeCode) { size = BssomBinaryPrimitives.NativeDateTimeSize; } else if (objType == NativeBssomType.DecimalCode) { size = BssomBinaryPrimitives.DecimalSize; } else if (objType == NativeBssomType.GuidCode) { size = BssomBinaryPrimitives.GuidSize; } else { throw BssomSerializationOperationException.UnexpectedCodeRead(objType); } break; } case BssomType.Map1: case BssomType.Map2: case BssomType.Array2: size = (int)ReadVariableNumberCore(); break; case BssomType.Array1: { ReadStreamToBuffer(1); if (buffer[position - 1] /*elementType*/ == BssomType.NativeCode) { ReadStreamToBuffer(1); } size = (int)ReadVariableNumberCore(); break; } default: throw BssomSerializationOperationException.UnexpectedCodeRead(objType); } } return(size); }
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); }