Browse Source

When generating a field that uses an unknown type, System.Text.Json.Nodes.JsonNode will be used instead of plain System.Object

vpenades 2 years ago
parent
commit
af2fb9e7e5

+ 75 - 78
build/SharpGLTF.CodeGen/CodeGen/EmitCSharp.cs

@@ -95,9 +95,7 @@ namespace SharpGLTF.CodeGen
 
 
         private string _SanitizeName(string name)
         private string _SanitizeName(string name)
         {
         {
-            name = name.Replace(" ", "");
-
-            return name;
+            return name.Replace(" ", "");
         }
         }
 
 
         private _RuntimeType _UseType(SchemaType stype)
         private _RuntimeType _UseType(SchemaType stype)
@@ -186,118 +184,117 @@ namespace SharpGLTF.CodeGen
 
 
         private string _GetRuntimeName(SchemaType type, _RuntimeField extra)
         private string _GetRuntimeName(SchemaType type, _RuntimeField extra)
         {
         {
-            if (type is ObjectType anyType) return typeof(Object).Name;
-            if (type is StringType strType) return typeof(String).Name;            
-
-            if (type is BlittableType blitType)
+            switch (type)
             {
             {
-                var tname = blitType.DataType.Name;
+                case ObjectType anyType: return anyType.PersistentName;
 
 
-                return blitType.IsNullable ? $"{tname}?" : tname;
-            }
+                case StringType strType: return strType.PersistentName;
 
 
-            if (type is ArrayType arrayType)
-            {
-                var container = extra?.CollectionContainer;
-                if (string.IsNullOrWhiteSpace(container)) container = _DefaultCollectionContainer;
+                case BlittableType blitType:
+                    {
+                        var tname = blitType.DataType.Name;
 
 
-                return container.Replace("TItem",_GetRuntimeName(arrayType.ItemType));
-            }            
+                        return blitType.IsNullable ? $"{tname}?" : tname;
+                    }
 
 
-            if (type is DictionaryType dictType)
-            {
-                var key = this._GetRuntimeName(dictType.KeyType);
-                var val = this._GetRuntimeName(dictType.ValueType);                
+                case ArrayType arrayType:
+                    {
+                        var container = extra?.CollectionContainer;
+                        if (string.IsNullOrWhiteSpace(container)) container = _DefaultCollectionContainer;
 
 
-                return $"Dictionary<{key},{val}>";
-            }
+                        return container.Replace("TItem", _GetRuntimeName(arrayType.ItemType));
+                    }
 
 
-            if (type is EnumType enumType)
-            {
-                return _UseType(enumType).RuntimeName;
-            }
+                case DictionaryType dictType:
+                    {
+                        var key = _GetRuntimeName(dictType.KeyType);
+                        var val = _GetRuntimeName(dictType.ValueType);
 
 
-            if (type is ClassType classType)
-            {
-                return _UseType(classType).RuntimeName;
-            }
+                        return $"Dictionary<{key},{val}>";
+                    }
+
+                case EnumType enumType: return _UseType(enumType).RuntimeName;
 
 
-            throw new NotImplementedException();
+                case ClassType classType: return _UseType(classType).RuntimeName;
+
+                default: throw new NotImplementedException();
+            }
         }
         }
 
 
         private string _GetConstantRuntimeName(SchemaType type)
         private string _GetConstantRuntimeName(SchemaType type)
         {
         {
-            if (type is StringType strType) return $"const {typeof(String).Name}";
-
-            if (type is BlittableType blitType)
+            switch (type)
             {
             {
-                var tname = blitType.DataType.Name;
+                case StringType strType: return $"const {typeof(string).Name}";
 
 
-                if (blitType.DataType == typeof(Int32)) return $"const {tname}";
-                if (blitType.DataType == typeof(Single)) return $"const {tname}";
-                if (blitType.DataType == typeof(Double)) return $"const {tname}";
+                case BlittableType blitType:
+                    {
+                        var tname = blitType.DataType.Name;
 
 
-                return $"static readonly {tname}";
-            }
+                        if (blitType.DataType == typeof(int)) return $"const {tname}";
+                        if (blitType.DataType == typeof(float)) return $"const {tname}";
+                        if (blitType.DataType == typeof(double)) return $"const {tname}";
 
 
-            if (type is EnumType enumType)
-            {
-                return $"const {_UseType(enumType).RuntimeName}";
-            }
+                        return $"static readonly {tname}";
+                    }
 
 
-            if (type is ArrayType aType)
-            {
-                return $"static readonly {_UseType(aType).RuntimeName}";
-            }
+                case EnumType enumType: return $"const {_UseType(enumType).RuntimeName}";
 
 
-            throw new NotImplementedException();
+                case ArrayType aType: return $"static readonly {_UseType(aType).RuntimeName}";
+
+                default: throw new NotImplementedException();
+            }
         }
         }
 
 
         private Object _GetConstantRuntimeValue(SchemaType type, Object value)
         private Object _GetConstantRuntimeValue(SchemaType type, Object value)
         {
         {
             if (value == null) throw new ArgumentNullException();
             if (value == null) throw new ArgumentNullException();
 
 
-            if (type is StringType stype)
+            switch (type)
             {
             {
-                if (value is String) return value;
+                case StringType stype: if (value is string) return value;
 
 
-                return value == null ? null : System.Convert.ChangeType(value, typeof(string), System.Globalization.CultureInfo.InvariantCulture);
-            }
+                    return value == null
+                        ? null
+                        : Convert.ChangeType(value, typeof(string), System.Globalization.CultureInfo.InvariantCulture);
 
 
-            if (type is BlittableType btype)
-            {
-                if (btype.DataType == typeof(Boolean).GetTypeInfo())
-                {
-                    if (value is Boolean) return value;
+                case BlittableType btype:
+                    {
+                        if (btype.DataType == typeof(bool).GetTypeInfo())
+                        {
+                            if (value is bool) return value;
 
 
-                    var str = value as String;
+                            var str = value as string;
 
 
-                    if (str.ToLowerInvariant() == "false") return false;
-                    if (str.ToLowerInvariant() == "true") return true;
-                    throw new NotImplementedException();
-                }
+                            if (str.ToLowerInvariant() == "false") return false;
+                            if (str.ToLowerInvariant() == "true") return true;
+                            throw new NotImplementedException();
+                        }
 
 
-                if (value is String) return value;
+                        if (value is string) return value;
 
 
-                return value == null ? null : System.Convert.ChangeType(value, btype.DataType.AsType(), System.Globalization.CultureInfo.InvariantCulture);
-            }
+                        return value == null
+                            ? null
+                            : Convert.ChangeType(value, btype.DataType.AsType(), System.Globalization.CultureInfo.InvariantCulture);
+                    }
 
 
-            if (type is EnumType etype)
-            {
-                var etypeName = _GetRuntimeName(type);
+                case EnumType etype:
+                    {
+                        var etypeName = _GetRuntimeName(type);
 
 
-                if (value is String) return $"{etypeName}.{value}";
-                else return $"({etypeName}){value}";
-            }
+                        if (value is string) return $"{etypeName}.{value}";
+                        else return $"({etypeName}){value}";
+                    }
 
 
-            if (type is ArrayType aType)
-            {
-                var atypeName = _GetRuntimeName(type);
+                case ArrayType aType:
+                    {
+                        var atypeName = _GetRuntimeName(type);
 
 
-                return value.ToString();
-            }
+                        return value.ToString();
+                    }
 
 
-            throw new NotImplementedException();
+                default: throw new NotImplementedException();
+            }
         }        
         }        
 
 
         #endregion
         #endregion

+ 21 - 1
build/SharpGLTF.CodeGen/SchemaReflection/SchemaTypes.cs

@@ -9,6 +9,17 @@ namespace SharpGLTF.SchemaReflection
     /// <summary>
     /// <summary>
     /// Base class for all schema Types
     /// Base class for all schema Types
     /// </summary>
     /// </summary>
+    /// <remarks>
+    /// Derived classes:
+    /// - <see cref="StringType"/><br/>
+    /// - <see cref="ObjectType"/><br/>
+    /// - <see cref="BlittableType"/><br/>
+    /// - <see cref="EnumType"/><br/>
+    /// - <see cref="ArrayType"/><br/>
+    /// - <see cref="DictionaryType"/><br/>
+    /// - <see cref="ClassType"/><br/>
+    /// - <see cref="ReferenceType"/><br/>
+    /// </remarks>
     public abstract partial class SchemaType
     public abstract partial class SchemaType
     {
     {
         #region constructor
         #region constructor
@@ -59,6 +70,12 @@ namespace SharpGLTF.SchemaReflection
         #endregion
         #endregion
     }
     }
 
 
+    /// <summary>
+    /// Represents an undefined <see cref="Object"/> type.
+    /// </summary>
+    /// <remarks>
+    /// undefined objects are serialized/deserialized as <see cref="System.Text.Json.Nodes.JsonNode"/>
+    /// </remarks>
     [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
     [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
     public sealed class ObjectType : SchemaType
     public sealed class ObjectType : SchemaType
     {
     {
@@ -70,7 +87,7 @@ namespace SharpGLTF.SchemaReflection
 
 
         #region properties
         #region properties
 
 
-        public override string PersistentName => typeof(Object).Name;
+        public override string PersistentName => typeof(System.Text.Json.Nodes.JsonNode).FullName;
 
 
         #endregion
         #endregion
     }
     }
@@ -219,6 +236,9 @@ namespace SharpGLTF.SchemaReflection
         #endregion       
         #endregion       
     }
     }
 
 
+    /// <summary>
+    /// Represents a field property within a <see cref="ClassType"/>
+    /// </summary>
     [System.Diagnostics.DebuggerDisplay("{FieldType.PersistentName} {PersistentName}")]
     [System.Diagnostics.DebuggerDisplay("{FieldType.PersistentName} {PersistentName}")]
     public sealed class FieldInfo
     public sealed class FieldInfo
     {
     {