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 static void Main(string[] args) { TestOne(); var serviceCollection = new ServiceCollection(); serviceCollection.AddHagar(configuration => { configuration.Serializers.Add(typeof(SubTypeSerializer)); configuration.Serializers.Add(typeof(BaseTypeSerializer)); }); serviceCollection.AddSingleton <IGeneralizedCodec, DotNetSerializableCodec>(); serviceCollection.AddSingleton <IGeneralizedCodec, JsonCodec>(); var serviceProvider = serviceCollection.BuildServiceProvider(); var codecProvider = serviceProvider.GetRequiredService <CodecProvider>(); var serializer = codecProvider.GetCodec <SubType>(); SerializerSession GetSession() => serviceProvider.GetRequiredService <SessionPool>().GetSession(); var testString = "hello, hagar"; Test( GetSession, serializer, new SubType { BaseTypeString = "base", String = "sub", Int = 2, }); Test( GetSession, serializer, new SubType { BaseTypeString = "base", String = "sub", Int = int.MinValue, }); // Tests for duplicates Test( GetSession, serializer, new SubType { BaseTypeString = testString, String = testString, Int = 10 }); Test( GetSession, serializer, new SubType { BaseTypeString = testString, String = null, Int = 1 }); Test( GetSession, serializer, new SubType { BaseTypeString = testString, String = null, Int = 1 }); TestSkip( GetSession, serializer, new SubType { BaseTypeString = testString, String = null, Int = 1 }); Test( GetSession, serializer, new SubType { BaseTypeString = "HOHOHO", AddedLaterString = testString, String = null, Int = 1, Ref = testString }); var self = new SubType { BaseTypeString = "HOHOHO", AddedLaterString = testString, String = null, Int = 1 }; self.Ref = self; Test(GetSession, serializer, self); self.Ref = Guid.NewGuid(); Test(GetSession, serializer, self); Test( GetSession, new AbstractTypeSerializer <object>(), new WhackyJsonType { Number = 7, String = "bananas!" }); var mySerializable = new MySerializableClass { String = "yolooo", Integer = 38, Self = null, }; Test( GetSession, new AbstractTypeSerializer <object>(), mySerializable ); mySerializable.Self = mySerializable; Test( GetSession, new AbstractTypeSerializer <object>(), mySerializable ); Exception exception = null; try { throw new ReferenceNotFoundException(typeof(int), 2401); } catch (Exception e) { exception = e; } Test( GetSession, new AbstractTypeSerializer <object>(), exception ); Test(GetSession, new AbstractTypeSerializer <object>(), new LocalDate()); Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
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."); }