| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757 |
- 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;
- /// <summary>
- /// Serialize an object
- /// </summary>
- /// <param name="FilePath">The Filepath to write the data to</param>
- /// <param name="o">The Object to serialize</param>
- protected static void SerializeObject(string FilePath, object o) {
- Stream str = new FileStream(FilePath, FileMode.Create);
- SerializeObject(str, o, DefaultStringEncoding);
- str.Close();
- }
- /// <summary>
- /// Serialize an object
- /// </summary>
- /// <param name="str">Stream Object to write the Data in</param>
- /// <param name="o">The Object to serialize</param>
- protected static void SerializeObject(Stream str, object o) {
- SerializeObject(str, o, DefaultStringEncoding);
- }
- /// <summary>
- /// Serialize an object
- /// </summary>
- /// <param name="FilePath">The Filepath to write the data to</param>
- /// <param name="o">The Object to serialize</param>
- protected static void SerializeObject(string FilePath, object o, StringEncoding encode) {
- Stream str = new FileStream(FilePath, FileMode.Create);
- SerializeObject(str, o, encode);
- str.Close();
- }
- /// <summary>
- /// Serialize an object
- /// </summary>
- /// <param name="str">Stream Object to write the Data in</param>
- /// <param name="o">The Object to serialize</param>
- /// <param name="size">The size of arrays</param>
- 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<T>(string FilePath) {
- return (T)Serializer.DeserializeObject(FilePath, typeof(T));
- }
- protected static T DeserializeObject<T>(string FilePath, StringEncoding encode) {
- return (T)Serializer.DeserializeObject(FilePath, typeof(T), encode);
- }
- protected static T DeserializeObject<T>(Stream str) {
- return (T)Serializer.DeserializeObject(str, typeof(T));
- }
- protected static T DeserializeObject<T>(Stream str, StringEncoding encode) {
- return (T)Serializer.DeserializeObject(str, typeof(T), encode);
- }
- /// <summary>
- /// Deserialze an object
- /// </summary>
- /// <param name="FilePath">The File that contains</param>
- /// <param name="T">The object type to deserialize</param>
- /// <returns>Returns the object</returns>
- 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;
- }
- /// <summary>
- /// Deserialze an object
- /// </summary>
- /// <param name="str">Stream Object to read the Data from</param>
- /// <param name="T">The object type to deserialize</param>
- /// <returns>Returns the object</returns>
- protected static object DeserializeObject(Stream str, Type T) {
- if (str.Length > 0)
- return DeserializeObject(str, T, DefaultStringEncoding);
- return null;
- }
- /// <summary>
- /// Deserialze an object
- /// </summary>
- /// <param name="FilePath">The File that contains</param>
- /// <param name="T">The object type to deserialize</param>
- /// <returns>Returns the object</returns>
- 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;
- }
- /// <summary>
- /// Deserialze an object
- /// </summary>
- /// <param name="str">Stream Object to read the Data from</param>
- /// <param name="T">The object type to deserialize</param>
- /// <param name="size">The Size of arrays</param>
- /// <returns>Returns the object</returns>
- 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<ushort>(str, encode);
- string pattern = DeserializeObject<string>(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<uint>(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<string>(str, encode);
- return new Uri(uri);
- } else if (T == typeof(Image)) {
- return Image.FromStream(DeserializeObject<MemoryStream>(str, encode));
- } else if (T == typeof(Version)) {
- int major = DeserializeObject<int>(str, encode);
- int minor = DeserializeObject<int>(str, encode);
- int build = DeserializeObject<int>(str, encode);
- int rev = DeserializeObject<int>(str, encode);
- return new Version(major, minor, build, rev);
- } else if (T == typeof(Rectangle)) {
- int x = DeserializeObject<int>(str, encode);
- int y = DeserializeObject<int>(str, encode);
- int w = DeserializeObject<int>(str, encode);
- int h = DeserializeObject<int>(str, encode);
- return new System.Drawing.Rectangle(x, y, w, h);
- } else if (T == typeof(RectangleF)) {
- float x = DeserializeObject<float>(str, encode);
- float y = DeserializeObject<float>(str, encode);
- float w = DeserializeObject<float>(str, encode);
- float h = DeserializeObject<float>(str, encode);
- return new System.Drawing.RectangleF(x, y, w, h);
- } else if (T == typeof(Size)) {
- int w = DeserializeObject<int>(str, encode);
- int h = DeserializeObject<int>(str, encode);
- return new Size(w, h);
- } else if (T == typeof(SizeF)) {
- float w = DeserializeObject<float>(str, encode);
- float h = DeserializeObject<float>(str, encode);
- return new SizeF(w, h);
- } else if (T == typeof(Point)) {
- int x = DeserializeObject<int>(str, encode);
- int y = DeserializeObject<int>(str, encode);
- return new Point(x, y);
- } else if (T == typeof(PointF)) {
- float x = DeserializeObject<float>(str, encode);
- float y = DeserializeObject<float>(str, encode);
- return new PointF(x, y);
- } else if (T == typeof(Color)) {
- byte a = DeserializeObject<byte>(str, encode);
- byte r = DeserializeObject<byte>(str, encode);
- byte g = DeserializeObject<byte>(str, encode);
- byte b = DeserializeObject<byte>(str, encode);
- Color c = Color.FromArgb(a, r, g, b);
- } else if (T == typeof(Font)) {
- byte style = DeserializeObject<byte>(str, encode);
- float fsize = DeserializeObject<float>(str, encode);
- byte unit = DeserializeObject<byte>(str, encode);
- string FontFamily = DeserializeObject<string>(str, encode);
- return new Font(FontFamily, fsize, (FontStyle)style, (GraphicsUnit)unit);
- } else if (T == typeof(System.Globalization.CultureInfo)) {
- int culture_id = DeserializeObject<int>(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();
- }
- /** <summary>Do an array serialization. Used internally</summary> */
- 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();
- }
- /** <summary>Do an array serialization. Used internally</summary> */
- 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);
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- 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);
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- public static dynamic DeserializeArray(Stream str, Type ElemType) {
- return DeserializeArray(str, ElemType, DefaultStringEncoding);
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- 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);
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- public static dynamic DeserializeArray(Stream str, Type ElemType, StringEncoding encode) {
- dynamic ret;
- bool bReading = true;
- List<dynamic> res = new List<dynamic>();
- 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<T>(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<T>();
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- public static T DeserializeArray<T>(Stream str) {
- return DeserializeArray(str, typeof(T), DefaultStringEncoding);
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- public static T DeserializeArray<T>(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<T>();
- }
- /** <summary>Do an array deserialization. Used internally</summary> */
- public static T DeserializeArray<T>(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<T>() {
- TypeInfo typeinfo = typeof(T).GetTypeInfo();
- T empty_res = (T)Activator.CreateInstance(typeinfo);
- return empty_res;
- }
- }
- }
|