IComponentSerializer.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. namespace Urho.Resources
  3. {
  4. public interface IComponentSerializer
  5. {
  6. void Serialize(string key, object value);
  7. }
  8. public interface IComponentDeserializer
  9. {
  10. T Deserialize<T>(string key);
  11. }
  12. /// <summary>
  13. /// XMLElement based implementation of IComponentSerializer
  14. /// </summary>
  15. public class XmlComponentSerializer : IComponentSerializer, IComponentDeserializer
  16. {
  17. readonly XmlElement xmlElement;
  18. public XmlComponentSerializer(XmlElement xmlElement)
  19. {
  20. this.xmlElement = xmlElement;
  21. }
  22. public void Serialize(string key, object value)
  23. {
  24. if (value == null)
  25. return;
  26. if (value is string)
  27. xmlElement.SetString(key, (string)value);
  28. else if (value is Vector2)
  29. xmlElement.SetVector2(key, (Vector2)value);
  30. else if (value is Vector3)
  31. xmlElement.SetVector3(key, (Vector3)value);
  32. else if (value is Vector4)
  33. xmlElement.SetVector4(key, (Vector4)value);
  34. else if (value is IntRect)
  35. xmlElement.SetIntRect(key, (IntRect)value);
  36. else if (value is Quaternion)
  37. xmlElement.SetQuaternion(key, (Quaternion)value);
  38. else if (value is Color)
  39. xmlElement.SetColor(key, (Color)value);
  40. else if (value is float)
  41. xmlElement.SetFloat(key, (float)value);
  42. else if (value is int)
  43. xmlElement.SetInt(key, (int)value);
  44. else if (value is uint)
  45. xmlElement.SetUInt(key, (uint)value);
  46. else if (value is bool)
  47. xmlElement.SetBool(key, (bool)value);
  48. else if (value is double)
  49. xmlElement.SetDouble(key, (double)value);
  50. else throw new NotSupportedException($"{value.GetType().Name} is not supported.");
  51. }
  52. public T Deserialize<T>(string key)
  53. {
  54. var type = typeof (T);
  55. if (type == typeof (string))
  56. return (T)(object) xmlElement.GetAttribute(key);
  57. else if (type == typeof (Vector2))
  58. return (T) (object) xmlElement.GetVector2(key);
  59. else if (type == typeof (Vector3))
  60. return (T) (object) xmlElement.GetVector3(key);
  61. else if (type == typeof (Vector4))
  62. return (T) (object) xmlElement.GetVector4(key);
  63. else if (type == typeof (Quaternion))
  64. return (T) (object) xmlElement.GetQuaternion(key);
  65. else if (type == typeof (Color))
  66. return (T) (object) xmlElement.GetColor(key);
  67. else if (type == typeof (float))
  68. return (T) (object) xmlElement.GetFloat(key);
  69. else if (type == typeof (double))
  70. return (T) (object) xmlElement.GetDouble(key);
  71. else if (type == typeof (int))
  72. return (T) (object) xmlElement.GetInt(key);
  73. else if (type == typeof (uint))
  74. return (T) (object) xmlElement.GetUInt(key);
  75. else if (type == typeof (bool))
  76. return (T) (object) xmlElement.GetBool(key);
  77. else throw new NotSupportedException($"{type.Name} is not supported.");
  78. }
  79. }
  80. //TODO: binary serializer
  81. }