/// <summary> /// <para>将指定的键和值添加到Map</para> /// <para>Adds the specified key and value to the Map</para> /// </summary> public void Add(object key, object value) { if (key is BssomValue bsVal) { switch (bsVal.ValueType) { case BssomValueType.Number: case BssomValueType.String: case BssomValueType.Guid: case BssomValueType.Boolean: case BssomValueType.Char: case BssomValueType.DateTime: case BssomValueType.Decimal: case BssomValueType.Float: { _dict.Add(key, value); return; } default: throw BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(key.GetType()); } } BssMapKeyResolverProvider.VertyBssMapKeyType(key); _dict.Add(key, value); }
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); }