IComponentSerializer.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 is string)
  25. xmlElement.SetString(key, (string)value);
  26. else if (value is Vector2)
  27. xmlElement.SetVector2(key, (Vector2)value);
  28. else if (value is Vector3)
  29. xmlElement.SetVector3(key, (Vector3)value);
  30. else if (value is Vector4)
  31. xmlElement.SetVector4(key, (Vector4)value);
  32. else if (value is IntRect)
  33. xmlElement.SetIntRect(key, (IntRect)value);
  34. else if (value is Quaternion)
  35. xmlElement.SetQuaternion(key, (Quaternion)value);
  36. else if (value is Color)
  37. xmlElement.SetColor(key, (Color)value);
  38. else if (value is float)
  39. xmlElement.SetFloat(key, (float)value);
  40. else if (value is int)
  41. xmlElement.SetInt(key, (int)value);
  42. else if (value is uint)
  43. xmlElement.SetUInt(key, (uint)value);
  44. else if (value is bool)
  45. xmlElement.SetBool(key, (bool)value);
  46. else if (value is double)
  47. xmlElement.SetDouble(key, (double)value);
  48. else throw new NotSupportedException($"{value.GetType().Name} is not supported.");
  49. }
  50. public T Deserialize<T>(string key)
  51. {
  52. var type = typeof (T);
  53. if (type == typeof (string))
  54. return (T)(object) xmlElement.GetAttribute(key);
  55. else if (type == typeof (Vector2))
  56. return (T) (object) xmlElement.GetVector2(key);
  57. else if (type == typeof (Vector3))
  58. return (T) (object) xmlElement.GetVector3(key);
  59. else if (type == typeof (Vector4))
  60. return (T) (object) xmlElement.GetVector4(key);
  61. else if (type == typeof (Quaternion))
  62. return (T) (object) xmlElement.GetQuaternion(key);
  63. else if (type == typeof (Color))
  64. return (T) (object) xmlElement.GetColor(key);
  65. else if (type == typeof (float))
  66. return (T) (object) xmlElement.GetFloat(key);
  67. else if (type == typeof (double))
  68. return (T) (object) xmlElement.GetDouble(key);
  69. else if (type == typeof (int))
  70. return (T) (object) xmlElement.GetInt(key);
  71. else if (type == typeof (uint))
  72. return (T) (object) xmlElement.GetUInt(key);
  73. else if (type == typeof (bool))
  74. return (T) (object) xmlElement.GetBool(key);
  75. else throw new NotSupportedException($"{type.Name} is not supported.");
  76. }
  77. }
  78. //TODO: binary serializer
  79. }