// Copyright Epic Games, Inc. All Rights Reserved. using EpicGames.Core; using EpicGames.Serialization.Converters; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; namespace EpicGames.Serialization { /// /// Attribute used to mark a property that should be serialized to compact binary /// [AttributeUsage(AttributeTargets.Property)] public class CbFieldAttribute : Attribute { /// /// Name of the serialized field /// public string? Name { get; set; } /// /// Default constructor /// public CbFieldAttribute() { } /// /// Constructor /// /// public CbFieldAttribute(string Name) { this.Name = Name; } } /// /// Attribute used to indicate that this object is the base for a class hierarchy. Each derived class must have a [CbDiscriminator] attribute. /// public class CbPolymorphicAttribute : Attribute { } /// /// Sets the name used for discriminating between derived classes during serialization /// public class CbDiscriminatorAttribute : Attribute { /// /// Name used to identify this class /// public string Name { get; } /// /// Constructor /// /// Name used to identify this class public CbDiscriminatorAttribute(string Name) => this.Name = Name; } /// /// Exception thrown when serializing cb objects /// public class CbException : Exception { /// public CbException(string Message) : base(Message) { } /// public CbException(string Message, Exception Inner) : base(Message, Inner) { } } /// /// Attribute-driven compact binary serializer /// public static class CbSerializer { /// /// Serialize an object /// /// Type of the object to serialize /// /// public static CbObject Serialize(Type Type, object Value) { CbWriter Writer = new CbWriter(); CbConverter.GetConverter(Type).WriteObject(Writer, Value); return Writer.ToObject(); } /// /// Serialize an object /// /// /// /// public static CbObject Serialize(T Value) { CbWriter Writer = new CbWriter(); CbConverter.GetConverter().Write(Writer, Value); return Writer.ToObject(); } /// /// Serialize a property to a given writer /// /// /// /// public static void Serialize(CbWriter Writer, T Value) { CbConverter.GetConverter().Write(Writer, Value); } /// /// Serialize a named property to the given writer /// /// /// /// /// public static void Serialize(CbWriter Writer, Utf8String Name, T Value) { CbConverter.GetConverter().WriteNamed(Writer, Name, Value); } /// /// Deserialize an object from a /// /// /// Type of the object to read /// public static object? Deserialize(CbField Field, Type Type) { return CbConverter.GetConverter(Type).ReadObject(Field); } /// /// Deserialize an object from a /// /// /// /// public static T Deserialize(CbField Field) { return CbConverter.GetConverter().Read(Field); } /// /// Deserialize an object from a /// /// /// /// public static T Deserialize(CbObject Object) => Deserialize(Object.AsField()); /// /// Deserialize an object from a block of memory /// /// /// /// public static T Deserialize(ReadOnlyMemory Data) => Deserialize(new CbField(Data)); } }