| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.IO; |
| using System.Linq; |
| using System.Linq; |
| using System.Text; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Threading.Tasks; |
| |
| |
| namespace Playnite.SDK.Data |
| namespace Playnite.SDK.Data |
| { |
| { |
| /// <summary> |
| /// <summary> |
. | |
| /// Represents serialization attribute to indicate that object member should be ignored during serialization. |
| |
| /// </summary> |
| |
| public class DontSerializeAttribute : Attribute |
| |
| { |
| |
| } |
| |
| |
| |
| /// <summary> |
| |
| /// Represents serialization attribute to set a specific serialization member name. |
| |
| /// </summary> |
| |
| public class SerializationPropertyNameAttribute : Attribute |
| |
| { |
| |
| /// <summary> |
| |
| /// Gets or sets serialization member name. |
| |
| /// </summary> |
| |
| public string PropertyName { get; set; } |
| |
| |
| |
| /// <summary> |
| |
| /// Creates new instance of <see cref="SerializationPropertyNameAttribute"/>. |
| |
| /// </summary> |
| |
| /// <param name="propertyName"></param> |
| |
| public SerializationPropertyNameAttribute(string propertyName) |
| |
| { |
| |
| PropertyName = propertyName; |
| |
| } |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Describes data serializer. |
| /// Describes data serializer. |
| /// </summary> |
| /// </summary> |
| public interface IDataSerializer |
| public interface IDataSerializer |
| { |
| { |
| /// <summary> |
| /// <summary> |
| /// Serailize an object to YAML string. |
| /// Serailize an object to YAML string. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
| string ToYaml(object obj); |
| string ToYaml(object obj); |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from YAML string. |
| /// Deserialize an object from YAML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="yaml"></param> |
| /// <param name="yaml"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromYaml<T>(string yaml) where T : class, new(); |
| T FromYaml<T>(string yaml) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object form YAML string. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="yaml"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromYaml<T>(string yaml, out T content) where T : class; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from a file containing YAML string. |
| /// Deserialize an object from a file containing YAML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="filePath"></param> |
| /// <param name="filePath"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromYamlFile<T>(string filePath) where T : class, new(); |
| T FromYamlFile<T>(string filePath) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to serialize an object to YAML file. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="filePath"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromYamlFile<T>(string filePath, out T content) where T : class; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Serailize an object to JSON string. |
| /// Serailize an object to JSON string. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <param name="formatted"></param> |
| /// <param name="formatted"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
| string ToJson(object obj, bool formatted = false); |
| string ToJson(object obj, bool formatted = false); |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Serialize an object to JSON string written to a stream. |
| /// Serialize an object to JSON string written to a stream. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <param name="stream"></param> |
| /// <param name="stream"></param> |
| /// <param name="formatted"></param> |
| /// <param name="formatted"></param> |
. | void ToJsonSteam(object obj, Stream stream, bool formatted = false); |
| void ToJsonStream(object obj, Stream stream, bool formatted = false); |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from JSON string. |
| /// Deserialize an object from JSON string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="json"></param> |
| /// <param name="json"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromJson<T>(string json) where T : class, new(); |
| T FromJson<T>(string json) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object from JSON string. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="json"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromJson<T>(string json, out T content) where T : class; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from JSON data stream. |
| /// Deserialize an object from JSON data stream. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="stream"></param> |
| /// <param name="stream"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromJsonStream<T>(Stream stream) where T : class, new(); |
| T FromJsonStream<T>(Stream stream) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object from JSON data stream. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="stream"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromJsonStream<T>(Stream stream, out T content) where T : class; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from a file containing JSON string. |
| /// Deserialize an object from a file containing JSON string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="filePath"></param> |
| /// <param name="filePath"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromJsonFile<T>(string filePath) where T : class, new(); |
| T FromJsonFile<T>(string filePath) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object from JSON file. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="filePath"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromJsonFile<T>(string filePath, out T content) where T : class; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from TOML string. |
| /// Deserialize an object from TOML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="toml"></param> |
| /// <param name="toml"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromToml<T>(string toml) where T : class, new(); |
| T FromToml<T>(string toml) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object from TOML string. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="toml"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromToml<T>(string toml, out T content) where T : class; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from a file containing TOML string. |
| /// Deserialize an object from a file containing TOML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="filePath"></param> |
| /// <param name="filePath"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | T FromTomlFile<T>(string filePath) where T : class, new(); |
| T FromTomlFile<T>(string filePath) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object from TOML file. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="filePath"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| bool TryFromTomlFile<T>(string filePath, out T content) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Creates clone of an object using json serialization. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="source"></param> |
| |
| /// <returns></returns> |
| |
| T GetClone<T>(T source) where T : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Creates clone of an object using json serialization. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <typeparam name="U"></typeparam> |
| |
| /// <param name="source"></param> |
| |
| /// <returns></returns> |
| |
| U GetClone<T, U>(T source) |
| |
| where T : class |
| |
| where U : class; |
| |
| |
| |
| /// <summary> |
| |
| /// Compares two objects using json serialization. |
| |
| /// </summary> |
| |
| /// <param name="object1"></param> |
| |
| /// <param name="object2"></param> |
| |
| /// <returns></returns> |
| |
| bool AreObjectsEqual(object object1, object object2); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Represents data serialization utility. |
| /// Represents data serialization utility. |
| /// </summary> |
| /// </summary> |
| public class Serialization |
| public class Serialization |
| { |
| { |
| private static IDataSerializer serializer; |
| private static IDataSerializer serializer; |
| |
| |
| internal static void Init(IDataSerializer dataSerializer) |
| internal static void Init(IDataSerializer dataSerializer) |
| { |
| { |
| serializer = dataSerializer; |
| serializer = dataSerializer; |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Serailize an object to YAML string. |
| /// Serailize an object to YAML string. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
| public static string ToYaml(object obj) |
| public static string ToYaml(object obj) |
| { |
| { |
| return serializer.ToYaml(obj); |
| return serializer.ToYaml(obj); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from YAML string. |
| /// Deserialize an object from YAML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="yaml"></param> |
| /// <param name="yaml"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromYaml<T>(string yaml) where T : class, new() |
| public static T FromYaml<T>(string yaml) where T : class |
| { |
| { |
| return serializer.FromYaml<T>(yaml); |
| return serializer.FromYaml<T>(yaml); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
. | |
| /// Tries to deserialize an object from YAML string. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="yaml"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromYaml<T>(string yaml, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromYaml<T>(yaml, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Deserialize an object from a file containing YAML string. |
| /// Deserialize an object from a file containing YAML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="filePath"></param> |
| /// <param name="filePath"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromYamlFile<T>(string filePath) where T : class, new() |
| public static T FromYamlFile<T>(string filePath) where T : class |
| { |
| { |
| return serializer.FromYamlFile<T>(filePath); |
| return serializer.FromYamlFile<T>(filePath); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
. | |
| /// Tries to deserialize an object from YAML file. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="filePath"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromYamlFile<T>(string filePath, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromYamlFile<T>(filePath, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Serailize an object to JSON string. |
| /// Serailize an object to JSON string. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <param name="formatted"></param> |
| /// <param name="formatted"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
| public static string ToJson(object obj, bool formatted = false) |
| public static string ToJson(object obj, bool formatted = false) |
| { |
| { |
| return serializer.ToJson(obj, formatted); |
| return serializer.ToJson(obj, formatted); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Serialize an object to JSON string written to a stream. |
| /// Serialize an object to JSON string written to a stream. |
| /// </summary> |
| /// </summary> |
| /// <param name="obj"></param> |
| /// <param name="obj"></param> |
| /// <param name="stream"></param> |
| /// <param name="stream"></param> |
| /// <param name="formatted"></param> |
| /// <param name="formatted"></param> |
. | public static void ToJsonSteam(object obj, Stream stream, bool formatted = false) |
| public static void ToJsonStream(object obj, Stream stream, bool formatted = false) |
| { |
| { |
. | serializer.ToJsonSteam(obj, stream, formatted); |
| serializer.ToJsonStream(obj, stream, formatted); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Deserialize an object from JSON string. |
| /// Deserialize an object from JSON string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="json"></param> |
| /// <param name="json"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromJson<T>(string json) where T : class, new() |
| public static T FromJson<T>(string json) where T : class |
| { |
| { |
| return serializer.FromJson<T>(json); |
| return serializer.FromJson<T>(json); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
. | |
| /// Tries to deserialize an object from JSON string. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="json"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromJson<T>(string json, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromJson<T>(json, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Deserialize an object from JSON data stream. |
| /// Deserialize an object from JSON data stream. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="stream"></param> |
| /// <param name="stream"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromJsonStream<T>(Stream stream) where T : class, new() |
| public static T FromJsonStream<T>(Stream stream) where T : class |
| { |
| { |
| return serializer.FromJsonStream<T>(stream); |
| return serializer.FromJsonStream<T>(stream); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
. | |
| /// Tries to deserialize an object from JSON data stream. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="stream"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromJsonStream<T>(Stream stream, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromJsonStream<T>(stream, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Deserialize an object from a file containing JSON string. |
| /// Deserialize an object from a file containing JSON string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="filePath"></param> |
| /// <param name="filePath"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromJsonFile<T>(string filePath) where T : class, new() |
| public static T FromJsonFile<T>(string filePath) where T : class |
| { |
| { |
| return serializer.FromJsonFile<T>(filePath); |
| return serializer.FromJsonFile<T>(filePath); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
. | |
| /// Tries to deserialize an object from JSON file. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="filePath"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromJsonFile<T>(string filePath, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromJsonFile<T>(filePath, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Deserialize an object from TOML string. |
| /// Deserialize an object from TOML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="toml"></param> |
| /// <param name="toml"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromToml<T>(string toml) where T : class, new() |
| public static T FromToml<T>(string toml) where T : class |
| { |
| { |
| return serializer.FromToml<T>(toml); |
| return serializer.FromToml<T>(toml); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
. | |
| /// Tries to deserialize an object from TOML string. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="toml"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromToml<T>(string toml, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromToml<T>(toml, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| /// Deserialize an object from a file containing TOML string. |
| /// Deserialize an object from a file containing TOML string. |
| /// </summary> |
| /// </summary> |
| /// <typeparam name="T"></typeparam> |
| /// <typeparam name="T"></typeparam> |
| /// <param name="filePath"></param> |
| /// <param name="filePath"></param> |
| /// <returns></returns> |
| /// <returns></returns> |
. | public static T FromTomlFile<T>(string filePath) where T : class, new() |
| public static T FromTomlFile<T>(string filePath) where T : class |
| { |
| { |
| return serializer.FromTomlFile<T>(filePath); |
| return serializer.FromTomlFile<T>(filePath); |
. | |
| } |
| |
| |
| |
| /// <summary> |
| |
| /// Tries to deserialize an object from TOML file. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="filePath"></param> |
| |
| /// <param name="content"></param> |
| |
| /// <returns></returns> |
| |
| public static bool TryFromTomlFile<T>(string filePath, out T content) where T : class |
| |
| { |
| |
| return serializer.TryFromTomlFile<T>(filePath, out content); |
| |
| } |
| |
| |
| |
| /// <summary> |
| |
| /// Creates clone of an object using json serialization. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <param name="source"></param> |
| |
| /// <returns></returns> |
| |
| public static T GetClone<T>(T source) where T : class |
| |
| { |
| |
| return serializer.GetClone<T>(source); |
| |
| } |
| |
| |
| |
| /// <summary> |
| |
| /// Creates clone of an object using json serialization. |
| |
| /// </summary> |
| |
| /// <typeparam name="T"></typeparam> |
| |
| /// <typeparam name="U"></typeparam> |
| |
| /// <param name="source"></param> |
| |
| /// <returns></returns> |
| |
| public static U GetClone<T, U>(T source) |
| |
| where T : class |
| |
| where U : class |
| |
| { |
| |
| return serializer.GetClone<T, U>(source); |
| |
| } |
| |
| |
| |
| /// <summary> |
| |
| /// Compares two objects using json serialization. |
| |
| /// </summary> |
| |
| /// <param name="object1"></param> |
| |
| /// <param name="object2"></param> |
| |
| /// <returns></returns> |
| |
| public static bool AreObjectsEqual(object object1, object object2) |
| |
| { |
| |
| return serializer.AreObjectsEqual(object1, object2); |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |