public void Serialize <TBufferWriter>(ref Writer <TBufferWriter> writer, SubType obj) where TBufferWriter : IBufferWriter <byte> { _baseTypeSerializer.Serialize(ref writer, obj); writer.WriteEndBase(); // the base object is complete. _stringCodec.WriteField(ref writer, 0, typeof(string), obj.String); _intCodec.WriteField(ref writer, 1, typeof(int), obj.Int); _objectCodec.WriteField(ref writer, 1, typeof(object), obj.Ref); _intCodec.WriteField(ref writer, 1, typeof(int), obj.Int); _intCodec.WriteField(ref writer, 409, typeof(int), obj.Int); /*writer.WriteFieldHeader(session, 1025, typeof(Guid), Guid.Empty.GetType(), WireType.Fixed128); * writer.WriteFieldHeader(session, 1020, typeof(object), typeof(Program), WireType.Reference);*/ }
static void Test <T>(Func <SerializerSession> getSession, IFieldCodec <T> serializer, T expected) { var session = getSession(); var pipe = new Pipe(); var writer = new Writer <PipeWriter>(pipe.Writer, session); serializer.WriteField(ref writer, 0, typeof(T), expected); Console.WriteLine($"Size: {writer.Position} bytes."); Console.WriteLine($"Wrote References:\n{GetWriteReferenceTable(session)}"); pipe.Writer.FlushAsync().GetAwaiter().GetResult(); pipe.Writer.Complete(); pipe.Reader.TryRead(out var readResult); var reader = new Reader(readResult.Buffer, getSession()); var initialHeader = reader.ReadFieldHeader(); //Console.WriteLine(initialHeader); var actual = serializer.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); Console.WriteLine($"Expect: {expected}\nActual: {actual}"); var references = GetReadReferenceTable(reader.Session); Console.WriteLine($"Read references:\n{references}"); }
public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Dictionary <TKey, TValue> value) where TBufferWriter : IBufferWriter <byte> { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); if (value.Comparer != EqualityComparer <TKey> .Default) { _comparerCodec.WriteField(ref writer, 0, typeof(IEqualityComparer <TKey>), value.Comparer); } Int32Codec.WriteField(ref writer, 1, typeof(int), value.Count); uint innerFieldIdDelta = 1; foreach (var element in value) { _pairCodec.WriteField(ref writer, innerFieldIdDelta, CodecFieldType, element); innerFieldIdDelta = 0; } writer.WriteEndObject(); }
private void RoundTripTest(SubType expected, bool assertRef = true) { using var writerSession = GetSession(); var pipe = new Pipe(); var writer = Writer.Create(pipe.Writer, writerSession); _serializer.WriteField(ref writer, 0, typeof(SubType), expected); writer.Commit(); _log.WriteLine($"Size: {writer.Position} bytes."); _log.WriteLine($"Wrote References:\n{GetWriteReferenceTable(writerSession)}"); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); using var readerSesssion = GetSession(); var reader = Reader.Create(readResult.Buffer, readerSesssion); var initialHeader = reader.ReadFieldHeader(); _log.WriteLine("Header:"); _log.WriteLine(initialHeader.ToString()); var actual = _serializer.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); _log.WriteLine($"Expect: {expected}\nActual: {actual}"); Assert.Equal(expected.BaseTypeString, actual.BaseTypeString); Assert.Null(actual.AddedLaterString); // The deserializer isn't 'aware' of this field which was added later - version tolerance. Assert.Equal(expected.String, actual.String); Assert.Equal(expected.Int, actual.Int); if (assertRef) { Assert.Equal(expected.Ref, actual.Ref); } Assert.Equal(writer.Position, reader.Position); Assert.Equal(writer.Session.ReferencedObjects.CurrentReferenceId, reader.Session.ReferencedObjects.CurrentReferenceId); var references = GetReadReferenceTable(reader.Session); _log.WriteLine($"Read references:\n{references}"); }
void IFieldCodec <T?> .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, T?value) { // If the value is null, write it as the null reference. if (!value.HasValue && ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, null)) { return; } // The value is not null. _fieldCodec.WriteField(ref writer, fieldIdDelta, typeof(T), value.Value); }
public void Serialize <TBufferWriter>(ref Buffers.Writer <TBufferWriter> writer, PooledResponse <TResult> instance) where TBufferWriter : System.Buffers.IBufferWriter <byte> { if (instance.Exception is null) { _resultCodec.WriteField(ref writer, 0U, ResultType, instance.TypedResult); } else { _exceptionCodec.WriteField(ref writer, 1U, ExceptionType, instance.Exception); } }
void IFieldCodec <KeyValuePair <TKey, TValue> > .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, KeyValuePair <TKey, TValue> value) { ReferenceCodec.MarkValueField(writer.Session); writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); _keyCodec.WriteField(ref writer, 0, typeof(TKey), value.Key); _valueCodec.WriteField(ref writer, 1, typeof(TValue), value.Value); writer.WriteEndObject(); }
void IFieldCodec <Tuple <T> > .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Tuple <T> value) { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); _valueCodec.WriteField(ref writer, 1, typeof(T), value.Item1); writer.WriteEndObject(); }
void IFieldCodec <ValueTuple <T> > .WriteField <TBufferWriter>( ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, ValueTuple <T> value) { ReferenceCodec.MarkValueField(writer.Session); writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); _valueCodec.WriteField(ref writer, 1, typeof(T), value.Item1); writer.WriteEndObject(); }
/// <inheritdoc/> void IFieldCodec <object> .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); var array = (Array)value; var rank = array.Rank; var lengths = new int[rank]; var indices = new int[rank]; // Write array lengths. for (var i = 0; i < rank; i++) { lengths[i] = array.GetLength(i); } _intArrayCodec.WriteField(ref writer, 0, DimensionFieldType, lengths); var remaining = array.Length; uint innerFieldIdDelta = 1; while (remaining-- > 0) { var element = array.GetValue(indices); _elementCodec.WriteField(ref writer, innerFieldIdDelta, CodecElementType, (T)element); innerFieldIdDelta = 0; // Increment the indices array by 1. if (remaining > 0) { var idx = rank - 1; while (idx >= 0 && ++indices[idx] >= lengths[idx]) { indices[idx] = 0; --idx; if (idx < 0) { _ = ThrowIndexOutOfRangeException(lengths); } } } } writer.WriteEndObject(); }
static void TestSkip(Func <SerializerSession> getSession, IFieldCodec <SubType> serializer, SubType expected) { var session = getSession(); var writer = new Writer(); serializer.WriteField(writer, session, 0, typeof(SubType), expected); var reader = new Reader(writer.ToBytes()); var deserializationContext = getSession(); var initialHeader = reader.ReadFieldHeader(session); var skipCodec = new SkipFieldCodec(); skipCodec.ReadValue(reader, deserializationContext, initialHeader); Console.WriteLine($"Skipped {reader.CurrentPosition}/{reader.Length} bytes."); }
public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, List <T> value) where TBufferWriter : IBufferWriter <byte> { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); Int32Codec.WriteField(ref writer, 0, Int32Codec.CodecFieldType, value.Count); uint innerFieldIdDelta = 1; foreach (var element in value) { _fieldCodec.WriteField(ref writer, innerFieldIdDelta, CodecElementType, element); innerFieldIdDelta = 0; } writer.WriteEndObject(); }
static void TestSkip(Func <SerializerSession> getSession, IFieldCodec <SubType> serializer, SubType expected) { var session = getSession(); var pipe = new Pipe(); var writer = new Writer <PipeWriter>(pipe.Writer, session); serializer.WriteField(ref writer, 0, typeof(SubType), expected); pipe.Writer.FlushAsync().GetAwaiter().GetResult(); pipe.Writer.Complete(); pipe.Reader.TryRead(out var readResult); var reader = new Reader(readResult.Buffer, getSession()); var initialHeader = reader.ReadFieldHeader(); var skipCodec = new SkipFieldCodec(); skipCodec.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); Console.WriteLine($"Skipped {reader.Position} bytes."); }
public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Memory <T> value) where TBufferWriter : IBufferWriter <byte> { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); Int32Codec.WriteField(ref writer, 0, typeof(int), value.Length); var first = true; foreach (var element in value.Span) { _fieldCodec.WriteField(ref writer, first ? 1U : 0, typeof(T), element); first = false; } writer.WriteEndObject(); }
void IFieldCodec <Queue <T> > .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Queue <T> value) { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); Int32Codec.WriteField(ref writer, 0, typeof(int), value.Count); var first = true; foreach (var element in value) { _fieldCodec.WriteField(ref writer, first ? 1U : 0, typeof(T), element); first = false; } writer.WriteEndObject(); }
private static void Test <T>(Func <SerializerSession> getSession, IFieldCodec <T> serializer, T expected) { using var writerSession = getSession(); var pipe = new Pipe(); var writer = Writer.Create(pipe.Writer, writerSession); serializer.WriteField(ref writer, 0, typeof(T), expected); writer.Commit(); Console.WriteLine($"Size: {writer.Position} bytes."); Console.WriteLine($"Wrote References:\n{GetWriteReferenceTable(writerSession)}"); _ = pipe.Writer.FlushAsync().AsTask().GetAwaiter().GetResult(); pipe.Writer.Complete(); _ = pipe.Reader.TryRead(out var readResult); { using var readerSesssion = getSession(); var reader = Reader.Create(readResult.Buffer, readerSesssion); var result = BitStreamFormatter.Format(ref reader); Console.WriteLine(result); } { using var readerSesssion = getSession(); var reader = Reader.Create(readResult.Buffer, readerSesssion); var initialHeader = reader.ReadFieldHeader(); Console.WriteLine("Header:"); Console.WriteLine(initialHeader.ToString()); var actual = serializer.ReadValue(ref reader, initialHeader); pipe.Reader.AdvanceTo(readResult.Buffer.End); pipe.Reader.Complete(); Console.WriteLine($"Expect: {expected}\nActual: {actual}"); var references = GetReadReferenceTable(reader.Session); Console.WriteLine($"Read references:\n{references}"); } }
static void Test <T>(Func <SerializerSession> getSession, IFieldCodec <T> serializer, T expected) { var session = getSession(); var writer = new Writer(); serializer.WriteField(writer, session, 0, typeof(T), expected); Console.WriteLine($"Size: {writer.CurrentOffset} bytes."); Console.WriteLine($"Wrote References:\n{GetWriteReferenceTable(session)}"); //Console.WriteLine("TokenStream: " + string.Join(" ", TokenStreamParser.Parse(new Reader(writer.ToBytes()), getSession()))); var reader = new Reader(writer.ToBytes()); var deserializationContext = getSession(); var initialHeader = reader.ReadFieldHeader(session); //Console.WriteLine(initialHeader); var actual = serializer.ReadValue(reader, deserializationContext, initialHeader); Console.WriteLine($"Expect: {expected}\nActual: {actual}"); var references = GetReadReferenceTable(deserializationContext); Console.WriteLine($"Read references:\n{references}"); }
void IFieldCodec <Dictionary <TKey, TValue> > .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Dictionary <TKey, TValue> value) { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited); if (value.Comparer != EqualityComparer <TKey> .Default) { _comparerCodec.WriteField(ref writer, 0, typeof(IEqualityComparer <TKey>), value.Comparer); } var first = true; foreach (var element in value) { _pairCodec.WriteField(ref writer, first ? 1U : 0, typeof(KeyValuePair <TKey, TValue>), element); first = false; } writer.WriteEndObject(); }
public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) where TBufferWriter : IBufferWriter <byte> { if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) { return; } var type = value.GetType(); writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecType, WireType.TagDelimited); _typeCodec.WriteField(ref writer, 0, typeof(Type), type); if (type.IsValueType) { var serializer = _valueTypeSerializerFactory.GetSerializer(type); serializer.WriteValue(ref writer, value); } else { _objectSerializer.WriteValue(ref writer, value); } writer.WriteEndObject(); }
void IFieldCodec <TField> .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, TField value) => _codec.WriteField(ref writer, fieldIdDelta, expectedType, value);