LispRootSerializer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // $Id$
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using Lisp;
  7. namespace LispReader
  8. {
  9. /// <summary>Serializes and Deserializes an object based on reflection.</summary>
  10. /// <remarks>If someone is looking for a chalenge: Speed could be improved heavily
  11. /// by creating CIL at runtime...</remarks>
  12. public class LispRootSerializer : ILispSerializer
  13. {
  14. private Type type;
  15. public LispRootSerializer(Type type)
  16. {
  17. this.type = type;
  18. }
  19. public object Read(List list)
  20. {
  21. object result = CreateObject(type);
  22. Properties props = new Properties(list);
  23. // iterate over all fields and properties
  24. foreach(FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) {
  25. LispChildAttribute ChildAttrib = (LispChildAttribute)
  26. field.GetCustomAttribute(typeof(LispChildAttribute));
  27. if(ChildAttrib != null) {
  28. string Name = ChildAttrib.Name;
  29. if(field.Type == typeof(int)) {
  30. int val = 0;
  31. if(!props.Get(Name, ref val)) {
  32. if(!ChildAttrib.Optional)
  33. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  34. } else {
  35. field.SetValue(result, val);
  36. }
  37. } else if(field.Type == typeof(string)) {
  38. string val = null;
  39. if(!props.Get(Name, ref val)) {
  40. if(!ChildAttrib.Optional)
  41. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  42. } else {
  43. field.SetValue(result, val);
  44. }
  45. } else if(field.Type == typeof(float)) {
  46. float val = 0;
  47. if(!props.Get(Name, ref val)) {
  48. if(!ChildAttrib.Optional)
  49. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  50. } else {
  51. field.SetValue(result, val);
  52. }
  53. } else if (field.Type.IsEnum) {
  54. Enum val = null;
  55. if (!props.Get(Name, ref val, field.Type)) {
  56. if (!ChildAttrib.Optional)
  57. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  58. } else {
  59. field.SetValue(result, val);
  60. }
  61. } else if(field.Type == typeof(bool)) {
  62. bool val = false;
  63. if(!props.Get(Name, ref val)) {
  64. if(!ChildAttrib.Optional)
  65. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  66. } else {
  67. field.SetValue(result, val);
  68. }
  69. } else if(field.Type == typeof(List<int>)) {
  70. List<int> val = new List<int>();
  71. if(!props.GetIntList(Name, val)) {
  72. if(!ChildAttrib.Optional)
  73. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  74. } else {
  75. field.SetValue(result, val);
  76. }
  77. } else {
  78. ILispSerializer serializer = LispSerializer.GetSerializer(field.Type);
  79. if(serializer == null)
  80. throw new LispException("Type " + field.Type + " not supported for serialization");
  81. List val = null;
  82. if(!props.Get(Name, ref val)) {
  83. if(!ChildAttrib.Optional)
  84. LogManager.Log(LogLevel.Debug, "Field '" + Name + "' not in lisp");
  85. } else {
  86. object oval = serializer.Read(val);
  87. field.SetValue(result, oval);
  88. }
  89. }
  90. }
  91. foreach(LispChildsAttribute childsAttrib in
  92. field.GetCustomAttributes(typeof(LispChildsAttribute))) {
  93. object objectList = field.GetValue(result);
  94. Type ListType = field.Type;
  95. MethodInfo AddMethod = ListType.GetMethod(
  96. "Add", new Type[] { childsAttrib.ListType }, null);
  97. if(AddMethod == null)
  98. throw new LispException("No Add method found for field " + field.Name);
  99. ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
  100. if(serializer == null)
  101. serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
  102. foreach(List childList in props.GetList(childsAttrib.Name)) {
  103. object child = serializer.Read(childList);
  104. AddMethod.Invoke(objectList, new object[] { child } );
  105. }
  106. }
  107. }
  108. if(result is ICustomLispSerializer) {
  109. ICustomLispSerializer custom = (ICustomLispSerializer) result;
  110. custom.CustomLispRead(props);
  111. custom.FinishRead();
  112. }
  113. return result;
  114. }
  115. public void Write(Writer writer, string name, object Object)
  116. {
  117. writer.StartList(name);
  118. foreach(FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) {
  119. LispChildAttribute ChildAttrib = (LispChildAttribute)
  120. field.GetCustomAttribute(typeof(LispChildAttribute));
  121. if(ChildAttrib != null) {
  122. object Value = field.GetValue(Object);
  123. if(Value != null) {
  124. if(ChildAttrib.Translatable) {
  125. if(!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default))
  126. writer.WriteTranslatable(ChildAttrib.Name, Value.ToString());
  127. } else {
  128. Type childType = field.Type;
  129. ILispSerializer serializer = LispSerializer.GetSerializer(childType);
  130. if(serializer != null) {
  131. serializer.Write(writer, ChildAttrib.Name, Value);
  132. } else {
  133. if(ChildAttrib.Optional && childType.IsEnum) {
  134. // If it is an enum we need to convert ChildAttrib.Default
  135. // to an enum as ChildAttrib.Default is an Int32 by some (unknown) reason.
  136. Enum Defval = (Enum)Enum.ToObject(childType, ChildAttrib.Default);
  137. if (!Value.Equals(Defval))
  138. writer.Write(ChildAttrib.Name, Value);
  139. } else if(!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default)) {
  140. writer.Write(ChildAttrib.Name, Value);
  141. }
  142. }
  143. }
  144. } else {
  145. LogManager.Log(LogLevel.DebugWarning, "Field '" + field.Name + "' is null");
  146. }
  147. }
  148. foreach(LispChildsAttribute childsAttrib in
  149. field.GetCustomAttributes(typeof(LispChildsAttribute))) {
  150. if(childsAttrib != null) {
  151. object list = field.GetValue(Object);
  152. if(! (list is IEnumerable))
  153. throw new LispException("Field '" + field.Name + "' is not IEnumerable");
  154. ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
  155. if(serializer == null)
  156. serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
  157. IEnumerable enumerable = (IEnumerable) list;
  158. foreach(object childObject in enumerable) {
  159. if(childsAttrib.Type.IsAssignableFrom(childObject.GetType())) {
  160. serializer.Write(writer, childsAttrib.Name, childObject);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. if(Object is ICustomLispSerializer) {
  167. ICustomLispSerializer custom = (ICustomLispSerializer) Object;
  168. custom.CustomLispWrite(writer);
  169. }
  170. writer.EndList(name);
  171. }
  172. private static object CreateObject(Type Type)
  173. {
  174. // create object
  175. ConstructorInfo Constructor = Type.GetConstructor(Type.EmptyTypes);
  176. if(Constructor == null)
  177. throw new LispException("Type '" + Type + "' has no public constructor without arguments");
  178. object Result = Constructor.Invoke(new object[] {});
  179. return Result;
  180. }
  181. }
  182. }