public StreamWriterWrapper(Stream stream, TextSerializationContext context) { _indentation = 0; Stream = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true); Context = context; }
public void Write_Should_use_context_marshalling_for_object() { using Stream stream = new MemoryStream(); using TextSerializationContext context = new TextSerializationContext() .Marshal <int, string>(i => $"value {i}"); TextSerializer.Write <object>(stream, 42, context); stream.Position = 0; object copy = Read <object>(stream); Check.That(copy).IsInstanceOf <string>().And.IsEqualTo("value 42"); }
public void Write_Should_use_context_marshalling_When_same_type() { using Stream stream = new MemoryStream(); using TextSerializationContext context = new TextSerializationContext() .Marshal <int, int>(_ => 1337); TextSerializer.Write(stream, 42, context); stream.Position = 0; int copy = Read <int>(stream); Check.That(copy).IsEqualTo(1337); }
public void Write_Should_use_context_unmarshalling_When_sub_field() { using Stream stream = new MemoryStream(); using TextSerializationContext context = new TextSerializationContext() .Unmarshal <int, int>(i => i * 2); Write(stream, new Point(1, 2)); stream.Position = 0; Point copy = TextSerializer.Read <Point>(stream, context); Check.That(copy).IsEqualTo(new Point(2, 4)); }
public void Save() { using var context = new TextSerializationContext() .Marshal <RenderingObject, RenderObjectInfo>(from => new RenderObjectInfo() { Path = from.ResourceName, Region = from.Bounds, Origin = new Origin { X = from.Origin.X, Y = from.Origin.Y } }) .Marshal <BaseAction, string>(_ => null) .Marshal <IGameAI, AIFactory>(ai => ai.GetFactory()) .Unmarshal <AIFactory, IGameAI>(factory => factory.Get()) .Unmarshal <RenderObjectInfo, RenderingObject>(from => new RenderingObject(GameContext.Content.GetSprite(from), from.Path)); var textSerializer = new TextSerializer(context); using (Stream stream = File.Create("save.txt")) { var entities = GameContext.EntitySets.Serializable.GetEntities().ToArray(); textSerializer.Serialize(stream, entities); } }
public static ReadAction <T> GetReadAction <T>(Type type, TextSerializationContext context) { if (!_readActions.TryGetValue(type, out IReadActionWrapper readAction)) { lock (_readActions) { if (!_readActions.ContainsKey(type)) { TypeInfo typeInfo = type.GetTypeInfo(); readAction = (IReadActionWrapper)Activator.CreateInstance( (type.GetTypeInfo().IsValueType ? typeof(StructReadActionWrapper <>) : typeof(ClassReadActionWrapper <>)).MakeGenericType(type), typeof(Converter <>) .MakeGenericType(type).GetTypeInfo() .GetDeclaredMethod(nameof(Converter <string> .Read)) .CreateDelegate(typeof(ReadAction <>).MakeGenericType(type))); _readActions.AddOrUpdate(type, readAction, (_, d) => d); } } } return(readAction.Get <T>(context)); }
public ReadAction <T> Get <T>(TextSerializationContext context) => context?.GetValueRead <TReal, T>() ?? (typeof(TReal) == typeof(T) ? (ReadAction <T>)(Delegate) _readAction : ((StreamReaderWrapper r) => (T)(object)_readAction(r)));
public ReadAction <T> Get <T>(TextSerializationContext context) => context?.GetValueRead <TReal, T>() ?? (ReadAction <T>)(Delegate) _readAction;