瀏覽代碼

Added JsonReflectable base class

Vicente Penades Armengot 6 月之前
父節點
當前提交
a097761e20
共有 2 個文件被更改,包括 76 次插入20 次删除
  1. 69 0
      src/SharpGLTF.Core/Reflection/JsonReflectable.cs
  2. 7 20
      src/SharpGLTF.Core/Schema2/gltf.ExtraProperties.cs

+ 69 - 0
src/SharpGLTF.Core/Reflection/JsonReflectable.cs

@@ -0,0 +1,69 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+
+using SharpGLTF.IO;
+using SharpGLTF.Schema2;
+
+namespace SharpGLTF.Reflection
+{
+    /// <summary>
+    /// Extends <see cref="JsonSerializable"/> with reflection APIs
+    /// </summary>
+    /// <remarks>
+    /// Typically, most objects found in glTF schemas are either enums or classes that inherit
+    /// from <see cref="ExtraProperties"/> class, but that's not always the case. In these cases
+    /// the base class should be <see cref="JsonReflectable"/> to support all features.
+    /// </remarks>
+    public abstract class JsonReflectable : JsonSerializable, IReflectionObject
+    {
+        #region reflection
+
+        public const string SCHEMANAME = "Object";
+
+        protected override string GetSchemaName() => SCHEMANAME;
+
+        protected virtual IEnumerable<string> ReflectFieldsNames()
+        {
+            return Enumerable.Empty<string>();
+        }
+
+        protected virtual bool TryReflectField(string name, out FieldInfo value)
+        {
+            value = default;
+            return false;
+        }
+
+        IEnumerable<FieldInfo> IReflectionObject.GetFields()
+        {
+            foreach (var name in ReflectFieldsNames())
+            {
+                if (TryReflectField(name, out var finfo)) yield return finfo;
+            }
+        }
+
+        bool IReflectionObject.TryGetField(string name, out FieldInfo value)
+        {
+            return TryReflectField(name, out value);
+        }
+
+        #endregion
+
+        #region serialization
+
+        protected override void SerializeProperties(Utf8JsonWriter writer)
+        {
+            
+        }        
+        
+        protected override void DeserializeProperty(string jsonPropertyName, ref Utf8JsonReader reader)
+        {
+            reader.Skip();
+        }
+
+        #endregion
+    }
+}

+ 7 - 20
src/SharpGLTF.Core/Schema2/gltf.ExtraProperties.cs

@@ -27,9 +27,8 @@ namespace SharpGLTF.Schema2
     /// Defines the <see cref="Extras"/> property for every glTF object.
     /// Defines the <see cref="Extras"/> property for every glTF object.
     /// </remarks>
     /// </remarks>
     public abstract class ExtraProperties
     public abstract class ExtraProperties
-        : JsonSerializable
+        : JsonReflectable
         , IExtraProperties
         , IExtraProperties
-        , IReflectionObject
     {
     {
         #region data
         #region data
 
 
@@ -70,36 +69,24 @@ namespace SharpGLTF.Schema2
 
 
         #region reflection
         #region reflection
 
 
-        public const string SCHEMANAME = "ExtraProperties";
+        public new const string SCHEMANAME = "ExtraProperties";
 
 
-        protected override string GetSchemaName() => SCHEMANAME;
+        protected override string GetSchemaName() => SCHEMANAME;        
 
 
-        IEnumerable<FieldInfo> Reflection.IReflectionObject.GetFields()
-        {
-            foreach (var name in ReflectFieldsNames())
-            {
-                if (TryReflectField(name, out var finfo)) yield return finfo;
-            }
-        }
-
-        bool Reflection.IReflectionObject.TryGetField(string name, out SharpGLTF.Reflection.FieldInfo value)
-        {
-            return TryReflectField(name ,out value);
-        }
-
-        protected virtual IEnumerable<string> ReflectFieldsNames()
+        protected override IEnumerable<string> ReflectFieldsNames()
         {
         {
             yield return "extensions";
             yield return "extensions";
             yield return "extras";
             yield return "extras";
+            foreach (var name in base.ReflectFieldsNames()) yield return name;
 
 
         }
         }
-        protected virtual bool TryReflectField(string name, out Reflection.FieldInfo value)
+        protected override bool TryReflectField(string name, out Reflection.FieldInfo value)
         {
         {
             switch (name)
             switch (name)
             {
             {
                 case "extensions": value = Reflection.FieldInfo.From("extensions", _extensions, exts => new _ExtensionsReflection(exts)); return true;
                 case "extensions": value = Reflection.FieldInfo.From("extensions", _extensions, exts => new _ExtensionsReflection(exts)); return true;
                 case "extras": value = Reflection.FieldInfo.From("extras", _extras, inst => inst); return true;
                 case "extras": value = Reflection.FieldInfo.From("extras", _extras, inst => inst); return true;
-                default: value = default; return false;
+                default: return base.TryReflectField(name, out value);
             }
             }
         }
         }