Browse Source

Improved empty collections and fields handling in extras structures. Fixes #105

Vicente Penades 4 years ago
parent
commit
714c4bffcd

+ 4 - 0
src/SharpGLTF.Core/IO/JsonContent.Impl.cs

@@ -380,6 +380,8 @@ namespace SharpGLTF.IO
                 }
             }
 
+            if (count == 0) { return Array.Empty<Object>(); }
+
             if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>))
             {
                 elementType = typeof(IDictionary);
@@ -491,6 +493,7 @@ namespace SharpGLTF.IO
         {
             foreach (DictionaryEntry entry in dict)
             {
+                if (entry.Value == null) continue;
                 yield return new JSONPROPERTY(_GetKey(entry.Key), _GetValue(entry.Value));
             }
         }
@@ -499,6 +502,7 @@ namespace SharpGLTF.IO
         {
             foreach (var entry in dict)
             {
+                if (entry.Value == null) continue;
                 yield return new JSONPROPERTY(_GetKey(entry.Key), _GetValue(entry.Value));
             }
         }

+ 2 - 0
tests/SharpGLTF.Core.Tests/Schema2/Authoring/BasicSceneCreationTests.cs

@@ -34,9 +34,11 @@ namespace SharpGLTF.Schema2.Authoring
             var dict = new Dictionary<string, object>();
 
             dict["author"] = "me";
+            dict["string_null"] = (string)null;
 
             dict["value1"] = 17;
             dict["array1"] = new List<int> { 1, 2, 3 };
+            dict["array_empty"] = new List<int>();
             dict["dict1"] = new Dictionary<string, object>
             {
                 ["A"] = 16,