mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-04-09 06:15:49 +02:00
Tweak serialization code and remove ISerializedObject
This commit is contained in:
parent
8e9e8f7fad
commit
894b890fe5
Configuration
Data/Serialization
@ -9,8 +9,10 @@
|
||||
using TweetDuck.Data.Serialization;
|
||||
|
||||
namespace TweetDuck.Configuration{
|
||||
sealed class UserConfig : ISerializedObject{
|
||||
private static readonly FileSerializer<UserConfig> Serializer = new FileSerializer<UserConfig>();
|
||||
sealed class UserConfig{
|
||||
private static readonly FileSerializer<UserConfig> Serializer = new FileSerializer<UserConfig>{
|
||||
OnReadUnknownProperty = (obj, property, value) => false
|
||||
};
|
||||
|
||||
static UserConfig(){
|
||||
Serializer.RegisterTypeConverter(typeof(WindowState), WindowState.Converter);
|
||||
@ -138,10 +140,6 @@ public UserConfig(string file){ // TODO make private after removing UserConfigLe
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
bool ISerializedObject.OnReadUnknownProperty(string property, string value){
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Save(){
|
||||
try{
|
||||
WindowsUtils.CreateDirectoryForFile(file);
|
||||
|
@ -6,12 +6,15 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace TweetDuck.Data.Serialization{
|
||||
sealed class FileSerializer<T> where T : ISerializedObject{
|
||||
sealed class FileSerializer<T>{
|
||||
private const string NewLineReal = "\r\n";
|
||||
private const string NewLineCustom = "\r~\n";
|
||||
|
||||
private static readonly ITypeConverter BasicSerializerObj = new BasicTypeConverter();
|
||||
|
||||
public delegate bool OnReadUnknownPropertyHandler(T obj, string property, string value);
|
||||
public OnReadUnknownPropertyHandler OnReadUnknownProperty { get; set; }
|
||||
|
||||
private readonly Dictionary<string, PropertyInfo> props;
|
||||
private readonly Dictionary<Type, ITypeConverter> converters;
|
||||
|
||||
@ -49,7 +52,7 @@ public void Write(string file, T obj){
|
||||
|
||||
public void Read(string file, T obj){
|
||||
using(StreamReader reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))){
|
||||
if (reader.Peek() == 0){
|
||||
if (reader.Peek() <= 1){
|
||||
throw new FormatException("Input appears to be a binary file.");
|
||||
}
|
||||
|
||||
@ -75,7 +78,7 @@ public void Read(string file, T obj){
|
||||
throw new SerializationException($"Invalid file format, cannot convert value: {value} (property: {property})");
|
||||
}
|
||||
}
|
||||
else if (!obj.OnReadUnknownProperty(property, value)){
|
||||
else if (!(OnReadUnknownProperty?.Invoke(obj, property, value) ?? false)){
|
||||
throw new SerializationException($"Invalid file format, unknown property: {property}+");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
namespace TweetDuck.Data.Serialization{
|
||||
interface ISerializedObject{
|
||||
bool OnReadUnknownProperty(string property, string value);
|
||||
}
|
||||
}
|
@ -2,11 +2,8 @@
|
||||
|
||||
namespace TweetDuck.Data.Serialization{
|
||||
sealed class SingleTypeConverter<T> : ITypeConverter{
|
||||
public delegate string FuncConvertToString<U>(U value);
|
||||
public delegate U FuncConvertToObject<U>(string value);
|
||||
|
||||
public FuncConvertToString<T> ConvertToString { get; set; }
|
||||
public FuncConvertToObject<T> ConvertToObject { get; set; }
|
||||
public Func<T, string> ConvertToString { get; set; }
|
||||
public Func<string, T> ConvertToObject { get; set; }
|
||||
|
||||
bool ITypeConverter.TryWriteType(Type type, object value, out string converted){
|
||||
try{
|
||||
|
Loading…
Reference in New Issue
Block a user