internal override bool OnTryRead(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options, ref ReadStack state, out T?value) { if (reader.TokenType == BinaryTokenType.Null) { value = null; return(true); } if (state.Current.ObjectState < StackFrameObjectState.CreatedObject) { if (!reader.ReadTypeSeq()) { value = default; return(false); } state.Current.ObjectState = StackFrameObjectState.CreatedObject; } if (!_converter.TryRead(ref reader, typeof(T), options, ref state, out ReferenceID refId, out T typedValue)) { value = default; return(false); } value = typedValue; return(true); //if (!reader.ReadTypeSeq()) //{ // return null; //} //T value = _converter.Read(ref reader, typeof(T), options); //return value; }
public override void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options) { typeInfo.Type = TypeEnum.Byte; typeInfo.SerializeType = ClassType.Value; }
public override int GetBytesCount(ref BinaryReader reader, BinarySerializerOptions options) { return(BinarySerializerConstants.BytesCount_Byte); }
public override void Write(BinaryWriter writer, decimal value, BinarySerializerOptions options) { writer.WriteDecimalValue(value); }
protected internal abstract bool OnWriteResume(BinaryWriter writer, T dictionary, BinarySerializerOptions options, ref WriteStack state);
public override string Read(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options) { return(reader.GetString()); }
public override void Write(BinaryWriter writer, ushort value, BinarySerializerOptions options) { writer.WriteUInt16Value(value); }
internal override bool OnTryRead(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options, ref ReadStack state, [MaybeNullWhen(false)] out T value) { object obj; if (state.UseFastPath) { // 刚进入对象读取 if (reader.CurrentTypeInfo == null || reader.CurrentTypeInfo.SerializeType != ClassType.Object) { ThrowHelper.ThrowBinaryException_DeserializeUnableToConvertValue(TypeToConvert); } reader.AheadReadStartToken(); if (state.Current.BinaryClassInfo.CreateObject == null) { ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(state.Current.BinaryClassInfo.Type, ref reader, ref state); } RefState refState = BinarySerializer.ReadReferenceForObject(this, ref state, ref reader, out object refValue); if (refState == RefState.None) { obj = state.Current.BinaryClassInfo.CreateObject(); state.ReferenceResolver.AddReferenceObject(state.Current.RefId, obj); } else { obj = refValue; } // Process all properties. while (true) { reader.AheadReadPropertyName(); BinaryPropertyInfo binaryPropertyInfo = null; state.Current.PropertyState = StackFramePropertyState.Name; if (reader.TokenType == BinaryTokenType.EndObject) { break; } Debug.Assert(reader.TokenType == BinaryTokenType.PropertyName); ushort propertySeq = reader.CurrentPropertySeq; BinaryMemberInfo mi = state.GetMemberInfo(propertySeq); // Debug.Assert(mi != null); binaryPropertyInfo = BinarySerializer.LookupProperty( obj, mi.NameAsUtf8Bytes, ref state, out bool useExtensionProperty); state.Current.UseExtensionProperty = useExtensionProperty; // binaryPropertyInfo = state.LookupProperty(mi.NameAsString); state.Current.BinaryPropertyInfo = binaryPropertyInfo; state.Current.PropertyPolymorphicConverter = null; if (binaryPropertyInfo == null) { if (!reader.TrySkip(options)) { value = default; return(false); } state.Current.EndProperty(); continue; } if (!binaryPropertyInfo.ShouldDeserialize) { if (!reader.TrySkip(options)) { state.Current.ReturnValue = obj; value = default; return(false); } state.Current.EndProperty(); continue; } // Obtain the CLR value from the Binary and set the member. if (!state.Current.UseExtensionProperty) { binaryPropertyInfo.ReadBinaryAndSetMember(obj, ref state, ref reader); } else { // TODO 扩展属性 } state.Current.EndProperty(); } } else { // Slower path that supports continuation and preserved references. if (state.Current.ObjectState == StackFrameObjectState.None) { // 刚进入对象读取 if (reader.CurrentTypeInfo == null || reader.CurrentTypeInfo.SerializeType != ClassType.Object) { ThrowHelper.ThrowBinaryException_DeserializeUnableToConvertValue(TypeToConvert); } if (!reader.ReadStartToken()) { value = default; return(false); } RefState refState = BinarySerializer.ReadReferenceForObject(this, ref state, ref reader, out object refValue); if (refState == RefState.None) { state.Current.ObjectState = StackFrameObjectState.StartToken; } else if (refState == RefState.Created) { state.Current.ObjectState = StackFrameObjectState.CreatedObject; state.Current.ReturnValue = refValue; } } // 创建对象 if (state.Current.ObjectState < StackFrameObjectState.CreatedObject) { if (state.Current.BinaryClassInfo.CreateObject == null) { ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(state.Current.BinaryClassInfo.Type, ref reader, ref state); } obj = state.Current.BinaryClassInfo.CreateObject(); state.ReferenceResolver.AddReferenceObject(state.Current.RefId, obj); state.Current.ReturnValue = obj; state.Current.ObjectState = StackFrameObjectState.CreatedObject; } else { obj = state.Current.ReturnValue !; Debug.Assert(obj != null); } // Process all properties. while (true) { // Determine the property. // 读取属性索引 if (state.Current.PropertyState == StackFramePropertyState.None) { if (!reader.ReadPropertyName()) { state.Current.ReturnValue = obj; value = default; return(false); } state.Current.PropertyState = StackFramePropertyState.ReadName; // } BinaryPropertyInfo binaryPropertyInfo; if (state.Current.PropertyState <= StackFramePropertyState.ReadName) { state.Current.PropertyState = StackFramePropertyState.Name; if (reader.TokenType == BinaryTokenType.EndObject) { break; } Debug.Assert(reader.TokenType == BinaryTokenType.PropertyName); ushort propertySeq = reader.CurrentPropertySeq; BinaryMemberInfo mi = state.GetMemberInfo(propertySeq); Debug.Assert(mi != null); binaryPropertyInfo = BinarySerializer.LookupProperty( obj, mi.NameAsUtf8Bytes, ref state, out bool useExtensionProperty); state.Current.UseExtensionProperty = useExtensionProperty; // binaryPropertyInfo = state.LookupProperty(mi.NameAsString); state.Current.BinaryPropertyInfo = binaryPropertyInfo; state.Current.PropertyPolymorphicConverter = null; if (binaryPropertyInfo == null) { state.Current.EndProperty(); continue; } } else { Debug.Assert(state.Current.BinaryPropertyInfo != null); binaryPropertyInfo = state.Current.BinaryPropertyInfo !; } if (state.Current.PropertyState < StackFramePropertyState.ReadValue) { if (!binaryPropertyInfo.ShouldDeserialize) { if (!reader.TrySkip(options)) { state.Current.ReturnValue = obj; value = default; return(false); } state.Current.EndProperty(); continue; } } if (state.Current.PropertyState < StackFramePropertyState.TryRead) { // Obtain the CLR value from the Binary and set the member. if (!state.Current.UseExtensionProperty) { if (!binaryPropertyInfo.ReadBinaryAndSetMember(obj, ref state, ref reader)) { state.Current.ReturnValue = obj; value = default; return(false); } } else { // TODO 扩展属性 } state.Current.EndProperty(); } } } // Check if we are trying to build the sorted cache. if (state.Current.PropertyRefCache != null) { state.Current.BinaryClassInfo.UpdateSortedPropertyCache(ref state.Current); } value = (T)obj; return(true); }
///// <summary> ///// Whether the converter is built-in and handles a number type. ///// </summary> //internal bool IsInternalConverterForNumberType; /// <summary> /// Loosely-typed ReadCore() that forwards to strongly-typed ReadCore(). /// </summary> internal abstract object ReadCoreAsObject(ref BinaryReader reader, BinarySerializerOptions options, ref ReadStack state);
public override void Write(BinaryWriter writer, DateTimeOffset value, BinarySerializerOptions options) { writer.WriteDateTimeOffsetValue(value); }
public override void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options) { typeInfo.SerializeType = ClassType.Object; if (typeof(T) == typeof(object)) { typeInfo.Type = TypeEnum.Object; } else { typeInfo.FullName = options.GetTypeFullName(typeof(T)); typeInfo.Type = TypeEnum.Class; } }
public override DateTimeOffset Read(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options) { return(reader.GetDateTimeOffset()); }
public override void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options) { typeInfo.Type = TypeEnum.Nullable; }
public override T?Read(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options) { ThrowHelper.ThrowBinaryException(); return(default);
public override void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options) { typeInfo.SerializeType = ClassType.Enumerable; typeInfo.Type = TypeEnum.Class; typeInfo.FullName = options.GetTypeFullName(typeof(TCollection)); }
internal abstract bool TryReadAsObject(ref BinaryReader reader, BinarySerializerOptions options, ref ReadStack state, out object value);
public override void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options) { }
/// <summary> /// Loosely-typed WriteCore() that forwards to strongly-typed WriteCore(). /// </summary> internal abstract bool WriteCoreAsObject(BinaryWriter writer, object value, BinarySerializerOptions options, ref WriteStack state);
public override ushort Read(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options) { return(reader.GetUInt16()); }
/// <summary> /// Loosely-typed WriteWithQuotes() that forwards to strongly-typed WriteWithQuotes(). /// </summary> internal abstract void WriteWithQuotesAsObject(BinaryWriter writer, object value, BinarySerializerOptions options, ref WriteStack state);
public override decimal Read(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options) { return(reader.GetDecimal()); }
internal virtual void Initialize(BinarySerializerOptions options) { }
public static SerializerResult WriteBinary(Object obj, Stream s, BinarySerializerOptions opt) { return(null); }
/// <summary> /// Creates the instance and assigns it to state.Current.ReturnValue. /// </summary> internal virtual void CreateInstanceForReferenceResolver(ref BinaryReader reader, ref ReadStack state, BinarySerializerOptions options) { }
public override byte Read(ref BinaryReader reader, Type typeToConvert, BinarySerializerOptions options) { return(reader.GetByte()); }
/// <summary> /// 默认的固定字节数, 返回0的数,表示固定字节,返回0,表示自带长度格式,返回小于0,表示不定,reader将根据当前TypeEnum类型自动尝试读取 /// </summary> public virtual int GetBytesCount(ref BinaryReader reader, BinarySerializerOptions options) { return(BinarySerializerConstants.BytesCount_Dynamic); }
public override void Write(BinaryWriter writer, byte value, BinarySerializerOptions options) { writer.WriteByteValue(value); }
/// <summary> /// 设置类型的元数据 /// </summary> /// <param name="typeInfo">类型元数据</param> /// <param name="typeMap">当前序列化的TypeMap</param> /// <returns></returns> public abstract void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options);
public override void SetTypeMetadata(BinaryTypeInfo typeInfo, TypeMap typeMap, BinarySerializerOptions options) { base.SetTypeMetadata(typeInfo, typeMap, options); typeInfo.Type = TypeEnum.TimeZoneInfo; typeInfo.FullName = null; }
protected override bool OnWriteResume(BinaryWriter writer, TCollection value, BinarySerializerOptions options, ref WriteStack state) { IEnumerator <TElement> enumerator; if (state.Current.CollectionEnumerator == null) { enumerator = value.GetEnumerator(); if (!enumerator.MoveNext()) { return(true); } } else { enumerator = (IEnumerator <TElement>)state.Current.CollectionEnumerator; } BinaryConverter <TElement> converter = GetElementConverter(ref state); int index = state.Current.EnumeratorIndex; if (!state.SupportContinuation) { do { state.Current.WriteEnumerableIndex(index, writer); TElement element = enumerator.Current; converter.TryWrite(writer, element, options, ref state); // 列表状态下,每个写每个元素时独立的,故需清理多态属性 state.Current.PolymorphicBinaryPropertyInfo = null; index++; } while (enumerator.MoveNext()); } else { do { if (ShouldFlush(writer, ref state)) { state.Current.CollectionEnumerator = enumerator; return(false); } if (!state.Current.ProcessedEnumerableIndex) { state.Current.WriteEnumerableIndex(index, writer); state.Current.ProcessedEnumerableIndex = true; } TElement element = enumerator.Current; if (!converter.TryWrite(writer, element, options, ref state)) { state.Current.CollectionEnumerator = enumerator; state.Current.EnumeratorIndex = index; return(false); } // 列表状态下,每个写每个元素时独立的,故需清理多态属性 state.Current.PolymorphicBinaryPropertyInfo = null; state.Current.ProcessedEnumerableIndex = false; index++; } while (enumerator.MoveNext()); } return(true); }