protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (EnumTypeNode) TypeNode; var enumInfo = typeNode.EnumInfo; var value = enumInfo.EnumValues != null ? enumInfo.EnumValues[(Enum)BoundValue] : BoundValue; Serialize(stream, value, enumInfo.SerializedType, enumInfo.EnumValueLength); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (CollectionTypeNode)TypeNode; /* Create temporary list */ Type collectionType = typeof(List<>).MakeGenericType(typeNode.ChildType); var collection = (IList)Activator.CreateInstance(collectionType); /* Create single serializer to do all the work */ var childSerializer = (ValueValueNode)typeNode.Child.CreateSerializer(this); var reader = new EndianAwareBinaryReader(stream, Endianness); var childSerializedType = childSerializer.TypeNode.GetSerializedType(); var count = TypeNode.FieldCountBinding != null ? Convert.ToInt32(TypeNode.FieldCountBinding.GetValue(this)) : int.MaxValue; int? itemLength = null; if (TypeNode.ItemLengthBinding != null) itemLength = Convert.ToInt32(TypeNode.ItemLengthBinding.GetValue(this)); var terminationValue = typeNode.TerminationValue; var terminationChild = typeNode.TerminationChild == null ? null : typeNode.TerminationChild.CreateSerializer(this); int itemCount = 0; for (int i = 0; i < count; i++) { if (ShouldTerminate(stream)) break; /* Check termination case */ if (terminationChild != null) { using (var streamResetter = new StreamResetter(stream)) { terminationChild.Deserialize(stream, eventShuttle); if (terminationChild.Value.Equals(terminationValue)) { streamResetter.CancelReset(); break; } } } var value = childSerializer.Deserialize(reader, childSerializedType, itemLength); collection.Add(value); itemCount++; } /* Create final collection */ Value = CreateCollection(itemCount); /* Copy temp list into final collection */ for (int i = 0; i < itemCount; i++) SetCollectionValue(collection[i], i); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var valueStream = (Stream)Value; var valueStreamlet = TypeNode.FieldLengthBinding.IsConst ? new Streamlet(valueStream, valueStream.Position, Convert.ToInt64(TypeNode.FieldLengthBinding.ConstValue)) : new Streamlet(valueStream); valueStreamlet.CopyTo(stream); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { if (TypeNode.SubtypeBinding == null) { _valueType = TypeNode.Type; if(_valueType.IsAbstract) throw new InvalidOperationException("Abstract types must have at least one subtype binding to be deserialized."); } else { var subTypeValue = TypeNode.SubtypeBinding.GetValue(this); var matchingAttribute = TypeNode.SubtypeAttributes.SingleOrDefault( attribute => subTypeValue.Equals(Convert.ChangeType(attribute.Value, subTypeValue.GetType(), null))); _valueType = matchingAttribute == null ? null : matchingAttribute.Subtype; } // skip over if null (this may happen if subtypes are unknown during deserialization) if (_valueType != null) { var typeNode = (ObjectTypeNode) TypeNode; var subType = typeNode.GetSubType(_valueType); Children = new List<ValueNode>(subType.Children.Select(child => child.CreateSerializer(this))); ObjectDeserializeOverride(stream, eventShuttle); } /* Check if we need to read past padding */ if (TypeNode.FieldLengthBinding != null) { var length = Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); if (length > stream.RelativePosition) { var padLength = length - stream.RelativePosition; var pad = new byte[padLength]; stream.Read(pad, 0, pad.Length); } } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { /* This is weird but we need to find the base stream so we can reference it directly */ Stream baseStream = stream; while (baseStream is StreamLimiter) baseStream = (baseStream as StreamLimiter).Source; var length = TypeNode.FieldLengthBinding == null ? (long?)null : Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); Value = length != null ? new Streamlet(baseStream, baseStream.Position, Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this))) : new Streamlet(baseStream, baseStream.Position); if (length != null) stream.Seek(length.Value, SeekOrigin.Current); else stream.Seek(0, SeekOrigin.End); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (EnumTypeNode)TypeNode; var enumInfo = typeNode.EnumInfo; var value = Deserialize(stream, enumInfo.SerializedType, enumInfo.EnumValueLength); if (enumInfo.ValueEnums != null) { value = enumInfo.ValueEnums[(string)value]; } Func<object, object> converter; var underlyingValue = TypeConverters.TryGetValue(enumInfo.UnderlyingType, out converter) ? converter(value) : value; Value = Enum.ToObject(TypeNode.BaseSerializedType, underlyingValue); }
internal override Task SerializeOverrideAsync(BoundedStream stream, EventShuttle eventShuttle, CancellationToken cancellationToken) { ThrowIfUnordered(); return(ObjectSerializeOverrideAsync(stream, eventShuttle, cancellationToken)); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { int? itemLength = null; if (TypeNode.ItemLengthBinding != null && TypeNode.ItemLengthBinding.IsConst) itemLength = Convert.ToInt32(TypeNode.ItemLengthBinding.ConstValue); int? itemCount = null; if (TypeNode.FieldCountBinding != null && TypeNode.FieldCountBinding.IsConst) itemCount = Convert.ToInt32(TypeNode.FieldCountBinding.ConstValue); PrimitiveCollectionSerializeOverride(stream, itemLength, itemCount); var typeNode = (CollectionTypeNode)TypeNode; /* Add termination */ if (typeNode.TerminationChild != null) { var terminationChild = typeNode.TerminationChild.CreateSerializer(this); terminationChild.Value = typeNode.TerminationValue; terminationChild.Serialize(stream, eventShuttle); } }
protected abstract void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle);
public void Serialize(StreamLimiter stream, EventShuttle eventShuttle) { try { var serializeWhenBindings = TypeNode.SerializeWhenBindings; if (serializeWhenBindings != null && !serializeWhenBindings.Any(binding => binding.ConditionalValue.Equals(binding.GetBoundValue(this)))) return; if (TypeNode.FieldLengthBinding != null && TypeNode.FieldLengthBinding.IsConst) stream = new StreamLimiter(stream, Convert.ToInt64(TypeNode.FieldLengthBinding.ConstValue)); Binding fieldOffsetBinding = TypeNode.FieldOffsetBinding; using (new StreamResetter(stream, fieldOffsetBinding != null)) { if (fieldOffsetBinding != null) stream.Position = Convert.ToInt64(fieldOffsetBinding.GetValue(this)); SerializeOverride(stream, eventShuttle); } } catch (IOException) { throw; } catch (Exception e) { string reference = Name == null ? string.Format("type '{0}'", TypeNode.Type) : string.Format("member '{0}'", Name); string message = string.Format("Error serializing {0}. See inner exception for detail.", reference); throw new InvalidOperationException(message, e); } }
public abstract void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle);
protected virtual void ObjectSerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var serializableChildren = GetSerializableChildren(); var serializationContextLazy = new Lazy<BinarySerializationContext>(CreateSerializationContext); foreach (var child in serializableChildren) { if (eventShuttle != null && eventShuttle.HasSerializationSubscribers) eventShuttle.OnMemberSerializing(this, child.Name, serializationContextLazy.Value, stream.GlobalRelativePosition); child.Serialize(stream, eventShuttle); if (eventShuttle != null && eventShuttle.HasSerializationSubscribers) eventShuttle.OnMemberSerialized(this, child.Name, child.BoundValue, serializationContextLazy.Value, stream.GlobalRelativePosition); } }
protected override Task ObjectSerializeOverrideAsync(BoundedStream stream, EventShuttle eventShuttle, CancellationToken cancellationToken) { ObjectSerializeOverride(stream, eventShuttle); return(Task.CompletedTask); }
internal override void SerializeOverride(BoundedStream stream, EventShuttle eventShuttle) { ThrowIfUnordered(); ObjectSerializeOverride(stream, eventShuttle); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var serializableChildren = GetSerializableChildren(); int? itemLength = null; if (TypeNode.ItemLengthBinding != null && TypeNode.ItemLengthBinding.IsConst) itemLength = Convert.ToInt32(TypeNode.ItemLengthBinding.ConstValue); foreach (var child in serializableChildren) { if (stream.IsAtLimit) break; var childStream = itemLength == null ? stream : new StreamLimiter(stream, itemLength.Value); child.Serialize(childStream, eventShuttle); } var typeNode = (CollectionTypeNode)TypeNode; if (typeNode.TerminationChild != null) { var terminationChild = typeNode.TerminationChild.CreateSerializer(this); terminationChild.Value = typeNode.TerminationValue; terminationChild.Serialize(stream, eventShuttle); } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { Child = ((RootTypeNode)TypeNode).Child.CreateSerializer(this); Child.Deserialize(stream, eventShuttle); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { Child.Serialize(stream, eventShuttle); }
//public override Node Parent //{ // get // { // var parent = base.Parent; // return parent.Parent; // } //} protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { foreach(var child in Children) child.Serialize(stream, eventShuttle); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { throw new InvalidOperationException("Deserializing object fields not supported."); }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { object value = Deserialize(stream, TypeNode.GetSerializedType()); Value = ConvertToFieldType(value); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { Serialize(stream, BoundValue, TypeNode.GetSerializedType()); }
protected override void SerializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { ObjectSerializeOverride(stream, eventShuttle); /* Check if we need to pad out object */ if (TypeNode.FieldLengthBinding != null) { var length = Convert.ToInt64(TypeNode.FieldLengthBinding.GetValue(this)); if (length > stream.RelativePosition) { var padLength = length - stream.RelativePosition; var pad = new byte[padLength]; stream.Write(pad, 0, pad.Length); } } }
public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle) { var typeNode = (CollectionTypeNode)TypeNode; var count = TypeNode.FieldCountBinding != null ? Convert.ToInt32(TypeNode.FieldCountBinding.GetValue(this)) : int.MaxValue; var terminationValue = typeNode.TerminationValue; var terminationChild = typeNode.TerminationChild == null ? null : typeNode.TerminationChild.CreateSerializer(this); IEnumerable<int> itemLengths = null; if (TypeNode.ItemLengthBinding != null) { var itemLengthValue = TypeNode.ItemLengthBinding.GetValue(this); var enumerableItemLengthValue = itemLengthValue as IEnumerable; itemLengths = enumerableItemLengthValue != null ? enumerableItemLengthValue.Cast<object>().Select(Convert.ToInt32) : GetInfiniteSequence(Convert.ToInt32(itemLengthValue)); } IEnumerator<int> itemLengthEnumerator = null; try { if (itemLengths != null) itemLengthEnumerator = itemLengths.GetEnumerator(); for (int i = 0; i < count; i++) { if (ShouldTerminate(stream)) break; /* Check termination case */ if (terminationChild != null) { using (var streamResetter = new StreamResetter(stream)) { terminationChild.Deserialize(stream, eventShuttle); if (terminationChild.Value.Equals(terminationValue)) { streamResetter.CancelReset(); break; } } } var child = typeNode.Child.CreateSerializer(this); if (itemLengthEnumerator != null) itemLengthEnumerator.MoveNext(); var childStream = itemLengthEnumerator == null ? stream : new StreamLimiter(stream, itemLengthEnumerator.Current); child.Deserialize(childStream, eventShuttle); /* Check child termination case */ if (TypeNode.ItemSerializeUntilBinding != null) { var itemTerminationValue = TypeNode.ItemSerializeUntilBinding.GetValue(this); var itemTerminationChild = child.GetChild(TypeNode.ItemSerializeUntilAttribute.ItemValuePath); if (itemTerminationChild.Value.Equals(itemTerminationValue)) { if (!TypeNode.ItemSerializeUntilAttribute.ExcludeLastItem) { Children.Add(child); } break; } } Children.Add(child); } } finally { if (itemLengthEnumerator != null) itemLengthEnumerator.Dispose(); } }
protected virtual async Task ObjectDeserializeOverrideAsync(BoundedStream stream, EventShuttle eventShuttle, CancellationToken cancellationToken) { // check to see if we are actually supposed to be a custom deserialization. This is a side-effect of // treating all object members as object nodes. In the case of sub-types we could later discover we // are actually a custom node because the specified subtype implements IBinarySerializable. if (IsCustomNode(out ValueNode customValueNode)) { await customValueNode.DeserializeOverrideAsync(stream, eventShuttle, cancellationToken) .ConfigureAwait(false); // this is a cheat, but another side-effect of this weird corner case _cachedValue = customValueNode.Value; return; } var lazyContext = CreateLazySerializationContext(); foreach (var child in GetSerializableChildren()) { EmitBeginDeserialization(stream, child, lazyContext, eventShuttle); await child.DeserializeAsync(stream, eventShuttle, cancellationToken).ConfigureAwait(false); EmitEndDeserialization(stream, child, lazyContext, eventShuttle); } }