Procházet zdrojové kódy

Added Invalid Json Exception

Vicente Penades před 6 roky
rodič
revize
94c0c15099

+ 9 - 2
src/SharpGLTF.Core/Schema2/gltf.Serialization.cs

@@ -392,8 +392,15 @@ namespace SharpGLTF.Schema2
             {
             {
                 var root = new MODEL();
                 var root = new MODEL();
 
 
-                reader.Read();
-                root.Deserialize(reader);
+                try
+                {
+                    reader.Read();
+                    root.Deserialize(reader);
+                }
+                catch (JsonReaderException rex)
+                {
+                    throw new Validation.SchemaException(root, rex);
+                }
 
 
                 foreach (var buffer in root._buffers)
                 foreach (var buffer in root._buffers)
                 {
                 {

+ 9 - 0
src/SharpGLTF.Core/Validation/ModelException.cs

@@ -20,6 +20,12 @@ namespace SharpGLTF.Validation
             _Target = target;
             _Target = target;
         }
         }
 
 
+        internal ModelException(TARGET target, Exception ex)
+            : base(_CreateBaseMessage(target, ex.Message), ex)
+        {
+            _Target = target;
+        }
+
         private static string _CreateBaseMessage(TARGET target, String message)
         private static string _CreateBaseMessage(TARGET target, String message)
         {
         {
             if (target == null) return message;
             if (target == null) return message;
@@ -51,6 +57,9 @@ namespace SharpGLTF.Validation
     {
     {
         internal SchemaException(TARGET target, String message)
         internal SchemaException(TARGET target, String message)
             : base(target, message) { }
             : base(target, message) { }
+
+        internal SchemaException(TARGET target, Newtonsoft.Json.JsonReaderException rex)
+            : base(target, rex) { }
     }
     }
 
 
     /// <summary>
     /// <summary>

+ 1 - 0
tests/SharpGLTF.Tests/Assets/Invalid_Json.gltf

@@ -0,0 +1 @@
+{{{{invalid json]]]]

+ 20 - 0
tests/SharpGLTF.Tests/Schema2/LoadAndSave/LoadInvalidTests.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+using NUnit.Framework;
+
+namespace SharpGLTF.Schema2.LoadAndSave
+{
+    [TestFixture]
+    public class LoadInvalidTests
+    {
+        [Test]
+        public void TestLoadInvalidJson()
+        {
+            var path = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Assets", "Invalid_Json.gltf");
+
+            Assert.Throws<Validation.SchemaException>(() => ModelRoot.Load(path));
+        }
+    }
+}

+ 3 - 0
tests/SharpGLTF.Tests/SharpGLTF.Tests.csproj

@@ -28,6 +28,9 @@
     <None Update="Assets\API.Toolkit.1.0.0-alpha0005.txt">
     <None Update="Assets\API.Toolkit.1.0.0-alpha0005.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
     </None>
+    <None Update="Assets\Invalid_Json.gltf">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <None Update="Assets\Texture1.jpg">
     <None Update="Assets\Texture1.jpg">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
     </None>