dtsod and extension methods improvements
This commit is contained in:
parent
878d0503f5
commit
ec56843836
41
DTLib.Dtsod/ClassSerializer/DtsodAttributes.cs
Normal file
41
DTLib.Dtsod/ClassSerializer/DtsodAttributes.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
|
namespace DTLib.Dtsod;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)]
|
||||||
|
public class DtsodSerializableAttribute : Attribute
|
||||||
|
{
|
||||||
|
public DtsodVersion Version;
|
||||||
|
public DtsodSerializableAttribute(DtsodVersion ver) => Version = ver;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
|
public class SerializeAsAttribute: Attribute
|
||||||
|
{
|
||||||
|
public string Key;
|
||||||
|
/// <summary>
|
||||||
|
/// use it only for base types
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">name the field will be serialized as</param>
|
||||||
|
public SerializeAsAttribute(string key) => Key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
|
public class SerializationMethodAttribute<TSource,TSerialized> : Attribute
|
||||||
|
{
|
||||||
|
public Func<TSource, TSerialized> Method;
|
||||||
|
/// <summary>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="method">how to serialize field</param>
|
||||||
|
public SerializationMethodAttribute(Func<TSource, TSerialized> method) => Method = method;
|
||||||
|
}
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
|
public class DeserializationMethodAttribute<TSource,TSerialized> : Attribute
|
||||||
|
{
|
||||||
|
public Func<TSerialized, TSource> Method;
|
||||||
|
/// <summary>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="method">how to deserialize field</param>
|
||||||
|
public DeserializationMethodAttribute( Func<TSerialized, TSource> method) => Method = method;
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@ -151,7 +151,9 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
|
|||||||
List<dynamic> list = new();
|
List<dynamic> list = new();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
list.Add(ReadValue(out bool _eol));
|
var item = ReadValue(out bool _eol);
|
||||||
|
if(item!=null)
|
||||||
|
list.Add(item);
|
||||||
if (_eol) break;
|
if (_eol) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,6 +241,12 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
|
|||||||
break;
|
break;
|
||||||
case ';':
|
case ';':
|
||||||
case ',':
|
case ',':
|
||||||
|
if(b.Length == 0)
|
||||||
|
{
|
||||||
|
if(endOfList)
|
||||||
|
return null;
|
||||||
|
throw new Exception("zero length value");
|
||||||
|
}
|
||||||
string str = b.ToString();
|
string str = b.ToString();
|
||||||
b.Clear();
|
b.Clear();
|
||||||
return ParseValue(str);
|
return ParseValue(str);
|
||||||
@ -287,42 +295,54 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
|
|||||||
{ typeof(double), (val, b) => b.Append(val.ToString(CultureInfo.InvariantCulture)) },
|
{ typeof(double), (val, b) => b.Append(val.ToString(CultureInfo.InvariantCulture)) },
|
||||||
{ typeof(decimal), (val, b) => b.Append(val.ToString(CultureInfo.InvariantCulture)).Append("de") }
|
{ typeof(decimal), (val, b) => b.Append(val.ToString(CultureInfo.InvariantCulture)).Append("de") }
|
||||||
};
|
};
|
||||||
short tabscount = -1;
|
|
||||||
protected StringBuilder Serialize(DtsodV23 dtsod, StringBuilder b = null)
|
protected StringBuilder Serialize(DtsodV23 dtsod, ref int tabscount, StringBuilder b = null)
|
||||||
{
|
{;
|
||||||
tabscount++;
|
tabscount++;
|
||||||
if (b is null) b = new StringBuilder();
|
if (b is null) b = new StringBuilder();
|
||||||
foreach (var pair in dtsod)
|
foreach (var pair in dtsod)
|
||||||
{
|
{
|
||||||
b.Append('\t', tabscount).Append(pair.Key).Append(": ");
|
b.Append('\t', tabscount).Append(pair.Key).Append(": ");
|
||||||
SerializeType(pair.Value);
|
SerializeType(pair.Value, ref tabscount);
|
||||||
b.Append(";\n");
|
b.Append(";\n");
|
||||||
|
|
||||||
void SerializeType(dynamic value)
|
void SerializeType(dynamic value, ref int tabscount)
|
||||||
{
|
{
|
||||||
if (value is null) b.Append("null");
|
if (value is null) b.Append("null");
|
||||||
|
else if (value is DtsodV23 _dtsod)
|
||||||
|
{
|
||||||
|
b.Append("{\n");
|
||||||
|
Serialize(_dtsod, ref tabscount, b);
|
||||||
|
b.Append('}');
|
||||||
|
}
|
||||||
else if (value is IList _list)
|
else if (value is IList _list)
|
||||||
{
|
{
|
||||||
b.Append('[');
|
b.Append('[');
|
||||||
foreach (object el in _list)
|
foreach (object el in _list)
|
||||||
{
|
{
|
||||||
SerializeType(el);
|
SerializeType(el, ref tabscount);
|
||||||
b.Append(',');
|
b.Append(',');
|
||||||
}
|
}
|
||||||
b.Remove(b.Length - 1, 1).Append(']');
|
b.Remove(b.Length - 1, 1).Append(']');
|
||||||
}
|
}
|
||||||
else if (value is DtsodV23 _dtsod)
|
|
||||||
{
|
|
||||||
b.Append("{\n");
|
|
||||||
Serialize(_dtsod, b);
|
|
||||||
b.Append('}');
|
|
||||||
}
|
|
||||||
else TypeSerializeFuncs[value.GetType()].Invoke(value, b);
|
else TypeSerializeFuncs[value.GetType()].Invoke(value, b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tabscount--;
|
tabscount--;
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => Serialize(this).ToString();
|
public override string ToString()
|
||||||
|
{
|
||||||
|
int tabscount = -1;
|
||||||
|
return Serialize(this, ref tabscount).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
///serializes dtsod as part of list
|
||||||
|
public string ToString(string partialListName)
|
||||||
|
{
|
||||||
|
int tabscount = 0;
|
||||||
|
var builder = new StringBuilder().Append('$').Append(partialListName).Append(":\n{\n");
|
||||||
|
return Serialize(this, ref tabscount, builder).Append("};\n").ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +0,0 @@
|
|||||||
namespace DTLib.Dtsod;
|
|
||||||
|
|
||||||
public class DtsodSerializableAttribute : Attribute
|
|
||||||
{
|
|
||||||
public DtsodVersion Version;
|
|
||||||
public DtsodSerializableAttribute(DtsodVersion ver) => Version = ver;
|
|
||||||
}
|
|
||||||
@ -2,37 +2,9 @@
|
|||||||
|
|
||||||
public static class Collections
|
public static class Collections
|
||||||
{
|
{
|
||||||
|
|
||||||
public static void ForEach<T>(this IEnumerable<T> en, Action<T> act)
|
public static void ForEach<T>(this IEnumerable<T> en, Action<T> act)
|
||||||
{
|
{
|
||||||
foreach (T elem in en)
|
foreach (T elem in en)
|
||||||
act(elem);
|
act(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// массив в лист
|
|
||||||
public static List<T> ToList<T>(this T[] input)
|
|
||||||
{
|
|
||||||
var list = new List<T>();
|
|
||||||
list.AddRange(input);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
// удаление нескольких элементов массива
|
|
||||||
public static T[] RemoveRange<T>(this T[] input, int startIndex, int count)
|
|
||||||
{
|
|
||||||
var list = input.ToList();
|
|
||||||
list.RemoveRange(startIndex, count);
|
|
||||||
return list.ToArray();
|
|
||||||
}
|
|
||||||
public static T[] RemoveRange<T>(this T[] input, int startIndex) => input.RemoveRange(startIndex, input.Length - startIndex);
|
|
||||||
|
|
||||||
|
|
||||||
// метод как у листов
|
|
||||||
public static bool Contains<T>(this T[] array, T value)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < array.Length; i++)
|
|
||||||
if (array[i].Equals(value))
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -144,4 +144,6 @@ public static class StringConverter
|
|||||||
if (max * length != s.Length) parts.Add(s.Substring(max * length, s.Length - max * length));
|
if (max * length != s.Length) parts.Add(s.Substring(max * length, s.Length - max * length));
|
||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsNullOrEmpty(this string s) => String.IsNullOrEmpty(s);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user