using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Fire.Serialize.Property;
namespace System.Fire.CSV {
public class Serializer {
protected const StringEncoding DefaultStringEncoding = StringEncoding.UTF8;
///
/// Serialize an object
///
/// The Filepath to write the data to
/// The Object to serialize
protected static void SerializeObject(string FilePath, object o) {
Stream str = new FileStream(FilePath, FileMode.Create);
SerializeObject(str, o, DefaultStringEncoding);
str.Close();
}
///
/// Serialize an object
///
/// Stream Object to write the Data in
/// The Object to serialize
protected static void SerializeObject(Stream str, object o) {
SerializeObject(str, o, DefaultStringEncoding);
}
///
/// Serialize an object
///
/// The Filepath to write the data to
/// The Object to serialize
protected static void SerializeObject(string FilePath, object o, StringEncoding encode) {
Stream str = new FileStream(FilePath, FileMode.Create);
SerializeObject(str, o, encode);
str.Close();
}
///
/// Serialize an object
///
/// Stream Object to write the Data in
/// The Object to serialize
/// The size of arrays
protected static void SerializeObject(Stream str, object o, StringEncoding encode) {
if (o == null) {
return;
}
TypeInfo typeinfo = o.GetType().GetTypeInfo();
var SerializeMethod1 =
typeinfo.DeclaredMethods.FirstOrDefault(x =>
x.Name == "ToCSVStream" &&
x.GetParameters().Length == 1 &&
x.GetParameters()[0].ParameterType == typeof(Stream) &&
x.IsStatic == false);
var SerializeMethod3 =
typeinfo.DeclaredMethods.FirstOrDefault(x =>
x.Name == "ToCSVStream" &&
x.GetParameters().Length == 2 &&
x.GetParameters()[0].ParameterType == typeof(Stream) &&
x.GetParameters()[1].ParameterType == typeof(StringEncoding) &&
x.IsStatic == false);
/*if (typeinfo.IsGenericType) {
System.Diagnostics.Debug.WriteLine("- Generic Type");
Type GenType = typeinfo.GetGenericTypeDefinition();
Type[] interfaces = GenType.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++)
System.Diagnostics.Debug.WriteLine("- - " + interfaces[i].Name);
}*/
if (SerializeMethod3 != null) {
SerializeMethod3.Invoke(o, new object[] { str, encode });
} else if (SerializeMethod1 != null) {
SerializeMethod1.Invoke(o, new object[] { str });
} else {
if (o.GetType() == typeof(string)) {
string s = (string)o;
s = "\"" + s.Replace("\"","\\\"") + "\"";
byte[] b;
if (encode == StringEncoding.UTF8)
b = Text.Encoding.UTF8.GetBytes(s);
else if (encode == StringEncoding.UTF7)
b = Text.Encoding.UTF7.GetBytes(s);
else if (encode == StringEncoding.UTF32)
b = Text.Encoding.UTF32.GetBytes(s);
else if (encode == StringEncoding.Asci)
b = Text.Encoding.ASCII.GetBytes(s);
else
b = Text.Encoding.Unicode.GetBytes(s); //Enumerable.ToArray((dynamic)o);
str.Write(b,0,b.Length);
} else if (o.GetType() == typeof(MemoryStream)) {
MemoryStream mem = (MemoryStream)o;
if (mem.Length > uint.MaxValue) {
SerializeObject(str, (uint)0, encode);
} else {
SerializeObject(str, (uint)mem.Length, encode);
long xpos = mem.Position;
mem.Position = 0;
while (mem.Position < mem.Length) {
byte[] b = new byte[10000];
int readedBytes = mem.Read(b, 0, 10000);
str.Write(b, 0, readedBytes);
}
mem.Position = xpos; // Reset position to the old value
}
} else if (o.GetType() == typeof(System.Text.RegularExpressions.Regex)) {
System.Diagnostics.Debug.WriteLine("RegEx: " + ((Text.RegularExpressions.Regex)o).ToString());
str.Write(new byte[] { 123 },0,1);
SerializeObject(str, (ushort)((Text.RegularExpressions.Regex)o).Options, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Text.RegularExpressions.Regex)o).ToString(), encode);
str.Write(new byte[] { 125 },0,1);
} else if (o.GetType() == typeof(Version)) {
System.Diagnostics.Debug.WriteLine("Version");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((Version)o).Major, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Version)o).Minor, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Version)o).Build, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Version)o).Revision, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(Uri)) {
System.Diagnostics.Debug.WriteLine("Uri");
SerializeObject(str, ((Uri)o).OriginalString, encode);
} else if (o.GetType() == typeof(Rectangle)) {
System.Diagnostics.Debug.WriteLine("Rectangle");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((Rectangle)o).X, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Rectangle)o).Y, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Rectangle)o).Width, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Rectangle)o).Height, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(RectangleF)) {
System.Diagnostics.Debug.WriteLine("RectangleF");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((RectangleF)o).X, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((RectangleF)o).Y, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((RectangleF)o).Width, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((RectangleF)o).Height, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(Size)) {
System.Diagnostics.Debug.WriteLine("Size");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((Size)o).Width, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Size)o).Height, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(SizeF)) {
System.Diagnostics.Debug.WriteLine("SizeF");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((SizeF)o).Width, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((SizeF)o).Height, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(Point)) {
System.Diagnostics.Debug.WriteLine("Point");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((Point)o).X, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Point)o).Y, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(PointF)) {
System.Diagnostics.Debug.WriteLine("PointF");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((PointF)o).X, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((PointF)o).Y, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(Color)) {
System.Diagnostics.Debug.WriteLine("Color");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, ((Color)o).A, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Color)o).R, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Color)o).G, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Color)o).B, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(System.Globalization.CultureInfo)) {
System.Diagnostics.Debug.WriteLine("CultureInfo");
SerializeObject(str, ((System.Globalization.CultureInfo)o).LCID, encode);
} else if (o.GetType() == typeof(System.Drawing.Font)) {
System.Diagnostics.Debug.WriteLine("Font");
str.Write(new byte[] { 123 }, 0, 1);
SerializeObject(str, (byte)((Font)o).Style, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Font)o).Size, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Font)o).Unit, encode);
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, ((Font)o).FontFamily.Name, encode);
str.Write(new byte[] { 125 }, 0, 1);
} else if (o.GetType() == typeof(bool)) {
System.Diagnostics.Debug.WriteLine("Boolean: " + ((bool)o ? "True" : "False"));
if ((bool)o == true) {
str.WriteByte(1);
} else {
str.WriteByte(0);
}
} else if (o.GetType() == typeof(sbyte)) {
System.Diagnostics.Debug.WriteLine("SByte");
SerializeObject(str, ((sbyte)o).ToString(), encode);
} else if (o.GetType() == typeof(char)) {
System.Diagnostics.Debug.WriteLine("Char");
SerializeObject(str, ((char)o).ToString(), encode);
} else if (o.GetType() == typeof(decimal)) {
System.Diagnostics.Debug.WriteLine("Decimal");
SerializeObject(str, ((decimal)o).ToString(), encode);
} else if (o.GetType() == typeof(DateTime)) {
System.Diagnostics.Debug.WriteLine("DateTime");
SerializeObject(str, ((DateTime)o).Ticks, encode);
} else if (typeinfo.IsArray) {
System.Diagnostics.Debug.WriteLine("Array");
SerializeArray(str, (Array)o, encode);
/*} else if (typeinfo.IsGenericType && typeinfo.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable<>)))
SerializeArray(str, Enumerable.ToArray((dynamic)o), size, encode);
*/
} else if (typeinfo.IsGenericType) {
bool bIsListable = false;
string iEnumarableName = typeof(IEnumerable<>).Name;
Type[] interfaces = typeinfo.GetGenericTypeDefinition().GetInterfaces();
foreach (Type i in interfaces) {
if (i.Name == iEnumarableName) {
bIsListable = true;
break;
}
}
if (bIsListable) {
SerializeArray(str, Enumerable.ToArray((dynamic)o), encode);
}
} else {
byte[] buf = null;
if (o.GetType() == typeof(byte))
buf = new byte[] { (byte)o };
else if (typeof(BitConverter).GetTypeInfo()
.DeclaredMethods.Any(x =>
x.Name == "GetBytes" &&
x.IsStatic &&
x.GetParameters()
.SingleOrDefault()
.ParameterType == o.GetType()))
buf = BitConverter.GetBytes((dynamic)o);
else {
/*foreach (dynamic dyn in typeinfo.DeclaredFields
.Where(x => (x.IsPublic || x.IsAssembly) && !x.IsStatic)
.Select(x => x.GetValue(o))) {
FieldInfo fi = (FieldInfo)dyn;
System.Diagnostics.Debug.WriteLine("- " + fi.Name + " [" + (fi.IsPublic ? "Y" : "N") + (fi.IsAssembly ? "Y" : "N") + (fi.IsStatic ? "Y" : "N") + "]");
SerializeObject(str, dyn, size, encode);
}*/
foreach (FieldInfo fi in typeinfo.DeclaredFields) {
if ((fi.IsPublic || fi.IsAssembly) && !fi.IsStatic && fi.GetCustomAttribute(typeof(Annotations.SerializeIgnore)) == null) {
System.Diagnostics.Debug.WriteLine("- " + fi.Name + " [" + (fi.IsPublic ? "Y" : "N") + (fi.IsAssembly ? "Y" : "N") + (fi.IsStatic ? "Y" : "N") + "]");
SerializeObject(str, fi.GetValue(o), encode);
}
}
/*foreach(PropertyInfo pi in typeinfo.DeclaredProperties) {
if(pi.CanRead) {
SerializeObject(str, pi.GetValue(o), size, encode);
}
}*/
return;
}
str.Write(buf, 0, buf.Length);
return;
}
}
}
protected static T DeserializeObject(string FilePath) {
return (T)Serializer.DeserializeObject(FilePath, typeof(T));
}
protected static T DeserializeObject(string FilePath, StringEncoding encode) {
return (T)Serializer.DeserializeObject(FilePath, typeof(T), encode);
}
protected static T DeserializeObject(Stream str) {
return (T)Serializer.DeserializeObject(str, typeof(T));
}
protected static T DeserializeObject(Stream str, StringEncoding encode) {
return (T)Serializer.DeserializeObject(str, typeof(T), encode);
}
///
/// Deserialze an object
///
/// The File that contains
/// The object type to deserialize
/// Returns the object
protected static object DeserializeObject(string FilePath, Type T) {
if (File.Exists(FilePath)) {
Stream str = new FileStream(FilePath, FileMode.Open);
if (str.Length > 0) {
var res = DeserializeObject(str, T, DefaultStringEncoding);
str.Close();
return res;
}
str.Close();
}
return null;
}
///
/// Deserialze an object
///
/// Stream Object to read the Data from
/// The object type to deserialize
/// Returns the object
protected static object DeserializeObject(Stream str, Type T) {
if (str.Length > 0)
return DeserializeObject(str, T, DefaultStringEncoding);
return null;
}
///
/// Deserialze an object
///
/// The File that contains
/// The object type to deserialize
/// Returns the object
protected static object DeserializeObject(string FilePath, Type T, StringEncoding encode) {
if (File.Exists(FilePath)) {
Stream str = new FileStream(FilePath, FileMode.Open);
if (str.Length > 0) {
var res = DeserializeObject(str, T, encode);
str.Close();
return res;
}
str.Close();
}
return null;
}
///
/// Deserialze an object
///
/// Stream Object to read the Data from
/// The object type to deserialize
/// The Size of arrays
/// Returns the object
protected static object DeserializeObject(Stream str, Type T, StringEncoding encode) {
if (str.Length == 0)
return null;
var typeinfo = T.GetTypeInfo();
var DeserializeMethod1 =
typeinfo.DeclaredMethods.FirstOrDefault(x =>
x.Name == "FromDataStream" &&
x.GetParameters().Length == 1 &&
x.GetParameters()[0].ParameterType == typeof(Stream) &&
x.IsStatic == true &&
x.ReturnType == T);
var DeserializeMethod3 =
typeinfo.DeclaredMethods.FirstOrDefault(x =>
x.Name == "FromDataStream" &&
x.GetParameters().Length == 2 &&
x.GetParameters()[0].ParameterType == typeof(Stream) &&
x.GetParameters()[1].ParameterType == typeof(StringEncoding) &&
x.IsStatic == true &&
x.ReturnType == T);
if (DeserializeMethod3 != null) {
return DeserializeMethod3.Invoke(null, new object[] { str, encode });
} else if (DeserializeMethod1 != null) {
return DeserializeMethod1.Invoke(null, new object[] { str });
} else {
if (T == typeof(string)) {
string tmp = "";
bool bEscaped = false;
bool bRead = true;
bool bUseQuotes = false;
byte[] buf = new byte[1];
MemoryStream mem = new MemoryStream();
while(bRead) {
str.Read(buf, 0, 1);
if (buf[0] == 34 && mem.Length == 0 && bEscaped == false) {
bUseQuotes = true;
} else if (buf[0] == 92 && bEscaped == false) {
bEscaped = true;
} else if (mem.Length == 0 && (buf[0] == 9 || buf[0] == 32)) {
// ignore space or tab at the beginning
} else if (bUseQuotes && buf[0] == 34 && bEscaped == false) {
// finish
bRead = false;
} else if (!bUseQuotes && mem.Length>0 && bEscaped == false && (buf[0]==32 || buf[0]==59 || buf[0]==44 || buf[0]==10 || buf[0]==13)) {
// finish and undoo last position
bRead = false;
str.Position--;
} else {
mem.Write(buf,0,1);
}
bEscaped = false;
}
byte[] b = mem.ToArray();
mem.Close();
mem.Dispose();
if (encode == StringEncoding.UTF8)
tmp = Text.Encoding.UTF8.GetString(b);
else if (encode == StringEncoding.UTF7)
tmp = Text.Encoding.UTF7.GetString(b);
else if (encode == StringEncoding.UTF32)
tmp = Text.Encoding.UTF32.GetString(b);
else if (encode == StringEncoding.Asci)
tmp = Text.Encoding.ASCII.GetString(b);
else
tmp = Text.Encoding.Unicode.GetString(b);
return tmp;
} else if (T == typeof(System.Text.RegularExpressions.Regex)) {
ushort opt = DeserializeObject(str, encode);
string pattern = DeserializeObject(str, encode);
return new Text.RegularExpressions.Regex(pattern, (Text.RegularExpressions.RegexOptions)opt);
} else if (T == typeof(MemoryStream)) {
//return new MemoryStream(DeserializeArray(str, typeof(byte[]), ArraySize.bits_32, StringEncoding.Asci));
MemoryStream mem = new MemoryStream();
uint length = DeserializeObject(str, encode);
while (mem.Length < length) {
int bytesToRead = 10000;
if (length - mem.Length < 10000) {
bytesToRead = (int)(length - mem.Length);
}
if (bytesToRead >= 1) {
byte[] b = new byte[bytesToRead];
str.Read(b, 0, bytesToRead);
mem.Write(b, 0, bytesToRead);
} else {
break;
}
}
} else if (T == typeof(Uri)) {
string uri = DeserializeObject(str, encode);
return new Uri(uri);
} else if (T == typeof(Image)) {
return Image.FromStream(DeserializeObject(str, encode));
} else if (T == typeof(Version)) {
int major = DeserializeObject(str, encode);
int minor = DeserializeObject(str, encode);
int build = DeserializeObject(str, encode);
int rev = DeserializeObject(str, encode);
return new Version(major, minor, build, rev);
} else if (T == typeof(Rectangle)) {
int x = DeserializeObject(str, encode);
int y = DeserializeObject(str, encode);
int w = DeserializeObject(str, encode);
int h = DeserializeObject(str, encode);
return new System.Drawing.Rectangle(x, y, w, h);
} else if (T == typeof(RectangleF)) {
float x = DeserializeObject(str, encode);
float y = DeserializeObject(str, encode);
float w = DeserializeObject(str, encode);
float h = DeserializeObject(str, encode);
return new System.Drawing.RectangleF(x, y, w, h);
} else if (T == typeof(Size)) {
int w = DeserializeObject(str, encode);
int h = DeserializeObject(str, encode);
return new Size(w, h);
} else if (T == typeof(SizeF)) {
float w = DeserializeObject(str, encode);
float h = DeserializeObject(str, encode);
return new SizeF(w, h);
} else if (T == typeof(Point)) {
int x = DeserializeObject(str, encode);
int y = DeserializeObject(str, encode);
return new Point(x, y);
} else if (T == typeof(PointF)) {
float x = DeserializeObject(str, encode);
float y = DeserializeObject(str, encode);
return new PointF(x, y);
} else if (T == typeof(Color)) {
byte a = DeserializeObject(str, encode);
byte r = DeserializeObject(str, encode);
byte g = DeserializeObject(str, encode);
byte b = DeserializeObject(str, encode);
Color c = Color.FromArgb(a, r, g, b);
} else if (T == typeof(Font)) {
byte style = DeserializeObject(str, encode);
float fsize = DeserializeObject(str, encode);
byte unit = DeserializeObject(str, encode);
string FontFamily = DeserializeObject(str, encode);
return new Font(FontFamily, fsize, (FontStyle)style, (GraphicsUnit)unit);
} else if (T == typeof(System.Globalization.CultureInfo)) {
int culture_id = DeserializeObject(str, encode);
return new System.Globalization.CultureInfo(culture_id);
} else if (T == typeof(bool)) {
byte[] b = new byte[1];
str.Read(b, 0, 1);
return b[0] == 1;
} else if (T == typeof(sbyte)) {
byte[] b = new byte[1];
str.Read(b, 0, 1);
return (sbyte)(-128 + b[0]);
} else if (T == typeof(char)) {
byte[] b = new byte[2];
str.Read(b, 0, 2);
return BitConverter.ToChar(b, 0);
} else if (T == typeof(decimal)) {
int[] iNum = new int[4];
iNum[0] = (int)DeserializeObject(str, typeof(int), encode);
iNum[1] = (int)DeserializeObject(str, typeof(int), encode);
iNum[2] = (int)DeserializeObject(str, typeof(int), encode);
iNum[3] = (int)DeserializeObject(str, typeof(int), encode);
//int[] iNum = (int[])DeserializeObject(str, typeof(int[]));
return new decimal(iNum);
} else if (T == typeof(DateTime)) {
return new DateTime((long)DeserializeObject(str, typeof(long), encode));
}
if (typeinfo.IsArray) {
return DeserializeArray(str, typeinfo.GetElementType(), encode);
} else if (typeinfo.IsGenericType && typeinfo.GetGenericTypeDefinition() == typeof(List<>)) {
return Enumerable.ToList(DeserializeArray(str, typeinfo.GetGenericArguments().First(), encode));
}
if (!T.IsClass) {
byte[] buf = new byte[Marshal.SizeOf(T)];
str.Read(buf, 0, buf.Length);
return buf;
}
object ret = Activator.CreateInstance(typeinfo);
foreach (var field in typeinfo.DeclaredFields.Where(x => (x.IsPublic || x.IsAssembly) && !x.IsStatic && x.GetCustomAttribute(typeof(Annotations.SerializeIgnore)) == null)) {
field.SetValue(ret, DeserializeObject(str, field.FieldType, encode));
}
/*foreach(PropertyInfo pi in typeinfo.DeclaredProperties) {
if(pi.CanRead) {
dynamic d = DeserializeObject(str, pi.PropertyType, size, encode);
if (pi.CanWrite)
pi.SetValue(ret, d);
}
}*/
return ret;
}
}
public static void SerializeArray(string FilePath, Array array) {
Stream str = new FileStream(FilePath, FileMode.Create);
SerializeArray(str, array, DefaultStringEncoding);
str.Close();
}
/** Do an array serialization. Used internally */
public static void SerializeArray(Stream str, Array array) {
SerializeArray(str, array, DefaultStringEncoding);
}
public static void SerializeArray(string FilePath, Array array, StringEncoding encode) {
Stream str = new FileStream(FilePath, FileMode.Create);
SerializeArray(str, array, encode);
str.Close();
}
/** Do an array serialization. Used internally */
public static void SerializeArray(Stream str, Array array, StringEncoding encode) {
str.Write(new byte[] { 91 }, 0, 1);
bool bFirst = true;
foreach (var item in array) {
if(bFirst == false)
str.Write(new byte[] { 44 }, 0, 1);
SerializeObject(str, item, encode);
bFirst = false;
}
str.Write(new byte[] { 93 }, 0, 1);
}
/** Do an array deserialization. Used internally */
public static dynamic DeserializeArray(string FilePath, Type ElemType) {
if (File.Exists(FilePath)) {
Stream str = new FileStream(FilePath, FileMode.Open);
var res = DeserializeArray(str, ElemType, DefaultStringEncoding);
str.Close();
return res;
}
return createEmptyObject(ElemType);
}
/** Do an array deserialization. Used internally */
public static dynamic DeserializeArray(Stream str, Type ElemType) {
return DeserializeArray(str, ElemType, DefaultStringEncoding);
}
/** Do an array deserialization. Used internally */
public static dynamic DeserializeArray(string FilePath, Type ElemType, StringEncoding encode) {
if (File.Exists(FilePath)) {
Stream str = new FileStream(FilePath, FileMode.Open);
var res = DeserializeArray(str, ElemType, encode);
str.Close();
return res;
}
return createEmptyObject(ElemType);
}
/** Do an array deserialization. Used internally */
public static dynamic DeserializeArray(Stream str, Type ElemType, StringEncoding encode) {
dynamic ret;
bool bReading = true;
List res = new List();
bool bInBrace = false;
bool bEscaped = false;
while (bReading) {
long curPos = str.Position;
byte[] bIsNull = new byte[1];
str.Read(bIsNull, 0, 1);
str.Position = curPos;
if (bIsNull[0] == 92 && bEscaped == false) {
bEscaped = true;
} else {
if (bInBrace) {
// TODO: Lese komma-seperator (byte[44])
if (bIsNull[0] == 93) {
bReading = false;
bInBrace = false;
} else {
dynamic tmp = Activator.CreateInstance(ElemType.MakeArrayType());
tmp = (dynamic)DeserializeObject(str, ElemType, encode);
res.Add(tmp);
}
} else {
if (bIsNull[0] == 91) {
bInBrace = true;
} else if (bIsNull[0] == 9 || bIsNull[0] == 32) {
// allowed character
} else {
// Error, not an array format
}
}
}
}
ret = res.ToArray();
return ret;
}
public static T DeserializeArray(string FilePath) {
if (File.Exists(FilePath)) {
Stream str = new FileStream(FilePath, FileMode.Open);
var res = DeserializeArray(str, typeof(T), DefaultStringEncoding);
str.Close();
return res;
}
return createEmptyObject();
}
/** Do an array deserialization. Used internally */
public static T DeserializeArray(Stream str) {
return DeserializeArray(str, typeof(T), DefaultStringEncoding);
}
/** Do an array deserialization. Used internally */
public static T DeserializeArray(string FilePath, StringEncoding encode) {
if (File.Exists(FilePath)) {
Stream str = new FileStream(FilePath, FileMode.Open);
var res = DeserializeArray(str, typeof(T), encode);
str.Close();
return res;
}
return createEmptyObject();
}
/** Do an array deserialization. Used internally */
public static T DeserializeArray(Stream str, StringEncoding encode) {
return DeserializeArray(str, typeof(T), encode);
}
private static dynamic createEmptyObject(Type T) {
TypeInfo typeinfo = T.GetTypeInfo();
if (typeinfo.IsArray) {
return Activator.CreateInstance(typeinfo, 0);
} else {
object empty_res = Activator.CreateInstance(typeinfo);
return empty_res;
}
}
private static T createEmptyObject() {
TypeInfo typeinfo = typeof(T).GetTypeInfo();
T empty_res = (T)Activator.CreateInstance(typeinfo);
return empty_res;
}
}
}