JsonReflectable.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using SharpGLTF.IO;
  8. using SharpGLTF.Schema2;
  9. namespace SharpGLTF.Reflection
  10. {
  11. /// <summary>
  12. /// Extends <see cref="JsonSerializable"/> with reflection APIs
  13. /// </summary>
  14. /// <remarks>
  15. /// Typically, most objects found in glTF schemas are either enums or classes that inherit
  16. /// from <see cref="ExtraProperties"/> class, but that's not always the case. In these cases
  17. /// the base class should be <see cref="JsonReflectable"/> to support all features.
  18. /// </remarks>
  19. public abstract class JsonReflectable : JsonSerializable, IReflectionObject
  20. {
  21. #region reflection
  22. public const string SCHEMANAME = "Object";
  23. protected override string GetSchemaName() => SCHEMANAME;
  24. protected virtual IEnumerable<string> ReflectFieldsNames()
  25. {
  26. return Enumerable.Empty<string>();
  27. }
  28. protected virtual bool TryReflectField(string name, out FieldInfo value)
  29. {
  30. value = default;
  31. return false;
  32. }
  33. IEnumerable<FieldInfo> IReflectionObject.GetFields()
  34. {
  35. foreach (var name in ReflectFieldsNames())
  36. {
  37. if (TryReflectField(name, out var finfo)) yield return finfo;
  38. }
  39. }
  40. bool IReflectionObject.TryGetField(string name, out FieldInfo value)
  41. {
  42. return TryReflectField(name, out value);
  43. }
  44. #endregion
  45. #region serialization
  46. protected override void SerializeProperties(Utf8JsonWriter writer)
  47. {
  48. }
  49. protected override void DeserializeProperty(string jsonPropertyName, ref Utf8JsonReader reader)
  50. {
  51. reader.Skip();
  52. }
  53. #endregion
  54. }
  55. }