Files
RobustToolbox/Robust.Shared/Serialization/Manager/SerializationManager.CustomSerializers.cs
T
DrSmugleafandGitHub 5f31036ab2 Cleanup serialization code (#1735)
* Cleanup serialization code

* Merge fixes

* american codebase
2021-05-02 14:23:39 +02:00

63 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using Robust.Shared.Serialization.Manager.Result;
using Robust.Shared.Serialization.Markdown;
using Robust.Shared.Serialization.Markdown.Validation;
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
namespace Robust.Shared.Serialization.Manager
{
public partial class SerializationManager
{
private readonly Dictionary<Type, object> _customTypeSerializers = new();
private DeserializationResult ReadWithSerializer<T, TNode, TSerializer>(
TNode node,
ISerializationContext? context = null,
bool skipHook = false)
where TSerializer : ITypeReader<T, TNode>
where T : notnull
where TNode : DataNode
{
var serializer = (ITypeReader<T, TNode>)GetTypeSerializer(typeof(TSerializer));
return serializer.Read(this, node, DependencyCollection, skipHook, context);
}
private DataNode WriteWithSerializer<T, TSerializer>(
T value,
ISerializationContext? context = null,
bool alwaysWrite = false)
where TSerializer : ITypeWriter<T>
where T : notnull
{
var serializer = (ITypeWriter<T>)GetTypeSerializer(typeof(TSerializer));
return serializer.Write(this, value, alwaysWrite, context);
}
private TCommon CopyWithSerializer<TCommon, TSource, TTarget, TSerializer>(
TSource source,
TTarget target,
bool skipHook,
ISerializationContext? context = null)
where TSource : TCommon
where TTarget : TCommon
where TCommon : notnull
where TSerializer : ITypeCopier<TCommon>
{
var serializer = (ITypeCopier<TCommon>) GetTypeSerializer(typeof(TSerializer));
return serializer.Copy(this, source, target, skipHook, context);
}
private ValidationNode ValidateWithSerializer<T, TNode, TSerializer>(
TNode node,
ISerializationContext? context)
where T : notnull
where TNode : DataNode
where TSerializer : ITypeValidator<T, TNode>
{
var serializer = (ITypeValidator<T, TNode>) GetTypeSerializer(typeof(TSerializer));
return serializer.Validate(this, node, DependencyCollection, context);
}
}
}