LispRootSerializer.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 challenge: 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. private void CheckRequired(LispChildAttribute ChildAttrib)
  20. {
  21. if(!ChildAttrib.Optional)
  22. LogManager.Log(LogLevel.Debug, "Required field '" + type.Name + "." + ChildAttrib.Name + "' was not declared in loded file.");
  23. }
  24. public object Read(List list)
  25. {
  26. object result = CreateObject(type);
  27. Properties props = new Properties(list);
  28. // iterate over all fields and properties
  29. foreach(FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) {
  30. LispChildAttribute ChildAttrib = (LispChildAttribute)
  31. field.GetCustomAttribute(typeof(LispChildAttribute));
  32. if(ChildAttrib != null) {
  33. string Name = ChildAttrib.Name;
  34. if(field.Type == typeof(int)) {
  35. int val = 0;
  36. if(!props.Get(Name, ref val)) {
  37. CheckRequired(ChildAttrib);
  38. } else {
  39. field.SetValue(result, val);
  40. }
  41. } else if(field.Type == typeof(string)) {
  42. string val = null;
  43. if(!props.Get(Name, ref val)) {
  44. CheckRequired(ChildAttrib);
  45. } else {
  46. field.SetValue(result, val);
  47. }
  48. } else if(field.Type == typeof(float)) {
  49. float val = 0;
  50. if(!props.Get(Name, ref val)) {
  51. CheckRequired(ChildAttrib);
  52. } else {
  53. field.SetValue(result, val);
  54. }
  55. } else if (field.Type.IsEnum) {
  56. Enum val = null;
  57. if (!props.Get(Name, ref val, field.Type)) {
  58. CheckRequired(ChildAttrib);
  59. } else {
  60. field.SetValue(result, val);
  61. }
  62. } else if(field.Type == typeof(bool)) {
  63. bool val = false;
  64. if(!props.Get(Name, ref val)) {
  65. CheckRequired(ChildAttrib);
  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. CheckRequired(ChildAttrib);
  73. } else {
  74. field.SetValue(result, val);
  75. }
  76. } else if(field.Type == typeof(List<string>)) {
  77. List<string> val = new List<string>();
  78. if(!props.GetStringList(Name, val)) {
  79. CheckRequired(ChildAttrib);
  80. } else {
  81. field.SetValue(result, val);
  82. }
  83. } else {
  84. ILispSerializer serializer = LispSerializer.GetSerializer(field.Type);
  85. if(serializer == null)
  86. throw new LispException("Type " + field.Type + " not supported for serialization");
  87. List val = null;
  88. if(!props.Get(Name, ref val)) {
  89. CheckRequired(ChildAttrib);
  90. } else {
  91. object oval = serializer.Read(val);
  92. field.SetValue(result, oval);
  93. }
  94. }
  95. }
  96. foreach(LispChildsAttribute childsAttrib in
  97. field.GetCustomAttributes(typeof(LispChildsAttribute))) {
  98. object objectList = field.GetValue(result);
  99. Type ListType = field.Type;
  100. MethodInfo AddMethod = ListType.GetMethod(
  101. "Add", new Type[] { childsAttrib.ListType }, null);
  102. if(AddMethod == null)
  103. throw new LispException("No Add method found for field " + field.Name);
  104. ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
  105. if(serializer == null)
  106. serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
  107. foreach(List childList in props.GetList(childsAttrib.Name)) {
  108. object child = serializer.Read(childList);
  109. AddMethod.Invoke(objectList, new object[] { child } );
  110. }
  111. }
  112. }
  113. if(result is ICustomLispSerializer) {
  114. ICustomLispSerializer custom = (ICustomLispSerializer) result;
  115. custom.CustomLispRead(props);
  116. custom.FinishRead();
  117. }
  118. return result;
  119. }
  120. public void Write(Writer writer, string name, object Object)
  121. {
  122. writer.StartList(name);
  123. foreach(FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) {
  124. LispChildAttribute ChildAttrib = (LispChildAttribute)
  125. field.GetCustomAttribute(typeof(LispChildAttribute));
  126. if(ChildAttrib != null && !ChildAttrib.Transient) {
  127. object Value = field.GetValue(Object);
  128. if(Value != null) {
  129. if(ChildAttrib.Translatable) {
  130. if(!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default))
  131. writer.WriteTranslatable(ChildAttrib.Name, Value.ToString());
  132. } else {
  133. Type childType = field.Type;
  134. ILispSerializer serializer = LispSerializer.GetSerializer(childType);
  135. if(serializer != null) {
  136. serializer.Write(writer, ChildAttrib.Name, Value);
  137. } else {
  138. if(ChildAttrib.Optional && childType.IsEnum) {
  139. // If it is an enum we need to convert ChildAttrib.Default
  140. // to an enum as ChildAttrib.Default is an Int32 by some (unknown) reason.
  141. Enum Defval = (Enum)Enum.ToObject(childType, ChildAttrib.Default);
  142. if (!Value.Equals(Defval))
  143. writer.Write(ChildAttrib.Name, Value);
  144. } else if(!ChildAttrib.Optional || !Value.Equals(ChildAttrib.Default)) {
  145. writer.Write(ChildAttrib.Name, Value);
  146. }
  147. }
  148. }
  149. } else {
  150. LogManager.Log(LogLevel.Debug, "Field '" + field.Name + "' is null");
  151. }
  152. }
  153. foreach(LispChildsAttribute childsAttrib in
  154. field.GetCustomAttributes(typeof(LispChildsAttribute))) {
  155. if(childsAttrib != null) {
  156. object list = field.GetValue(Object);
  157. if(! (list is IEnumerable))
  158. throw new LispException("Field '" + field.Name + "' is not IEnumerable");
  159. ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type);
  160. if(serializer == null)
  161. serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type);
  162. IEnumerable enumerable = (IEnumerable) list;
  163. foreach(object childObject in enumerable) {
  164. if(childsAttrib.Type.IsAssignableFrom(childObject.GetType())) {
  165. serializer.Write(writer, childsAttrib.Name, childObject);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. if(Object is ICustomLispSerializer) {
  172. ICustomLispSerializer custom = (ICustomLispSerializer) Object;
  173. custom.CustomLispWrite(writer);
  174. }
  175. writer.EndList(name);
  176. }
  177. private static object CreateObject(Type Type)
  178. {
  179. // create object
  180. ConstructorInfo Constructor = Type.GetConstructor(Type.EmptyTypes);
  181. if(Constructor == null)
  182. throw new LispException("Type '" + Type + "' has no public constructor without arguments");
  183. object Result = Constructor.Invoke(new object[] {});
  184. return Result;
  185. }
  186. }
  187. }