Ver código fonte

Fixes for obfuscated builds

Brad Robinson 11 anos atrás
pai
commit
76c8aa017d

+ 17 - 0
PetaJson.cs

@@ -308,12 +308,14 @@ namespace PetaJson
     }
 
     // Called before loading via reflection
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public interface IJsonLoading
     {
         void OnJsonLoading(IJsonReader r);
     }
 
     // Called after loading via reflection
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public interface IJsonLoaded
     {
         void OnJsonLoaded(IJsonReader r);
@@ -321,18 +323,21 @@ namespace PetaJson
 
     // Called for each field while loading from reflection
     // Return true if handled
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public interface IJsonLoadField
     {
         bool OnJsonField(IJsonReader r, string key);
     }
 
     // Called when about to write using reflection
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public interface IJsonWriting
     {
         void OnJsonWriting(IJsonWriter w);
     }
 
     // Called after written using reflection
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public interface IJsonWritten
     {
         void OnJsonWritten(IJsonWriter w);
@@ -352,6 +357,7 @@ namespace PetaJson
     }
 
     // Passed to registered parsers
+    [Obfuscation(Exclude=true, ApplyToMembers=true)]
     public interface IJsonReader
     {
         object Parse(Type type);
@@ -368,6 +374,7 @@ namespace PetaJson
     }
 
     // Passed to registered formatters
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public interface IJsonWriter
     {
         void WriteStringLiteral(string str);
@@ -731,6 +738,11 @@ namespace PetaJson
                 if (into == null)
                     return;
 
+                if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.Null)
+                {
+                    throw new InvalidOperationException("can't parse null into existing instance");
+                }
+
                 var type = into.GetType();
 
                 // Existing parse into handler?
@@ -2402,6 +2414,7 @@ namespace PetaJson
             {
                 object GetValue();
             }
+            [Obfuscation(Exclude = true, ApplyToMembers = true)]
             class PseudoBox<T> : IPseudoBox where T : struct
             {
                 public T value = default(T);
@@ -2777,6 +2790,7 @@ namespace PetaJson
             }
 
             // Helper to fetch a literal bool from an IJsonReader
+            [Obfuscation(Exclude = true)]
             public static bool GetLiteralBool(IJsonReader r)
             {
                 switch (r.GetLiteralKind())
@@ -2793,6 +2807,7 @@ namespace PetaJson
             }
 
             // Helper to fetch a literal character from an IJsonReader
+            [Obfuscation(Exclude = true)]
             public static char GetLiteralChar(IJsonReader r)
             {
                 if (r.GetLiteralKind() != LiteralKind.String)
@@ -2805,6 +2820,7 @@ namespace PetaJson
             }
 
             // Helper to fetch a literal string from an IJsonReader
+            [Obfuscation(Exclude = true)]
             public static string GetLiteralString(IJsonReader r)
             {
                 switch (r.GetLiteralKind())
@@ -2816,6 +2832,7 @@ namespace PetaJson
             }
 
             // Helper to fetch a literal number from an IJsonReader (returns the raw string)
+            [Obfuscation(Exclude = true)]
             public static string GetLiteralNumber(IJsonReader r)
             {
                 switch (r.GetLiteralKind())

+ 14 - 12
TestCases/TestAbstractTypes.cs

@@ -5,12 +5,13 @@ using System.Text;
 using PetaJson;
 using PetaTest;
 using System.IO;
+using System.Reflection;
 
 namespace TestCases
 {
     abstract class Shape : IJsonWriting
     {
-        [Json] public string Color;
+        [Json("color")] public string Color;
 
         // Override OnJsonWriting to write out the derived class type
         void IJsonWriting.OnJsonWriting(IJsonWriter w)
@@ -22,17 +23,18 @@ namespace TestCases
 
     class Rectangle : Shape
     {
-        [Json]
+        [Json("cornerRadius")]
         public float CornerRadius;
     }
 
     class Ellipse : Shape
     {
-        [Json]
+        [Json("filled")]
         public bool Filled;
     }
 
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class TestAbstractTypes
     {
         static TestAbstractTypes()
@@ -50,13 +52,14 @@ namespace TestCases
                 // Read the next literal (which better be a string) and instantiate the object
                 return reader.ReadLiteral(literal =>
                 {
-                    switch ((string)literal)
-                    {
-                        case "Rectangle": return new Rectangle();
-                        case "Ellipse": return new Ellipse();
-                        default:
-                            throw new InvalidDataException(string.Format("Unknown shape kind: '{0}'", literal));
-                    }
+                    var className = (string)literal;
+                    if (className == typeof(Rectangle).Name)
+                        return new Rectangle();
+
+                    if (className == typeof(Ellipse).Name)
+                        return new Ellipse();
+
+                    throw new InvalidDataException(string.Format("Unknown shape kind: '{0}'", literal));
                 });
             });
         }
@@ -75,8 +78,7 @@ namespace TestCases
             Console.WriteLine(json);
 
             // Check the object kinds were written out
-            Assert.Contains(json, "\"kind\": \"Rectangle\"");
-            Assert.Contains(json, "\"kind\": \"Ellipse\"");
+            Assert.Contains(json, "\"kind\":");
 
             // Reload the list
             var shapes2 = Json.Parse<List<Shape>>(json);

+ 2 - 0
TestCases/TestCustomFormat.cs

@@ -6,6 +6,7 @@ using PetaTest;
 using PetaJson;
 using System.IO;
 using System.Globalization;
+using System.Reflection;
 
 namespace TestCases
 {
@@ -16,6 +17,7 @@ namespace TestCases
     }
 
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public class TestCustomFormat
     {
         static TestCustomFormat()

+ 3 - 0
TestCases/TestNullableTypes.cs

@@ -4,9 +4,11 @@ using System.Linq;
 using System.Text;
 using PetaTest;
 using PetaJson;
+using System.Reflection;
 
 namespace TestCases
 {
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class NullableContainer
     {
         public int? Field;
@@ -14,6 +16,7 @@ namespace TestCases
     }
 
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public class TestNullableTypes
     {
         [Test]

+ 2 - 0
TestCases/TestOptions.cs

@@ -4,10 +4,12 @@ using System.Linq;
 using System.Text;
 using PetaTest;
 using PetaJson;
+using System.Reflection;
 
 namespace TestCases
 {
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public class TestOptions
     {
         [Test]

+ 2 - 0
TestCases/TestPrimitiveTypes.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using PetaTest;
 using PetaJson;
+using System.Reflection;
 
 namespace TestCases
 {
@@ -49,6 +50,7 @@ namespace TestCases
     }
 
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public class TestPrimitiveTypes
     {
         void Compare(AllTypes all, AllTypes all2)

+ 2 - 0
TestCases/TestsEvents.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using PetaTest;
 using PetaJson;
+using System.Reflection;
 
 namespace TestCases
 {
@@ -45,6 +46,7 @@ namespace TestCases
 
 
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public class TestsEvents
     {
         [Test]

+ 3 - 1
TestCases/TestsGeneral.cs

@@ -4,11 +4,13 @@ using System.Linq;
 using System.Text;
 using PetaTest;
 using PetaJson;
+using System.Reflection;
 
 namespace TestCases
 {
 	[TestFixture]
-	public class TestsGeneral
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
+    public class TestsGeneral
 	{
 		[Test]
 		public void Format_Null()

+ 47 - 0
TestCases/TestsReflection.cs

@@ -4,9 +4,11 @@ using System.Linq;
 using System.Text;
 using PetaTest;
 using PetaJson;
+using System.Reflection;
 
 namespace TestCases
 {
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class ModelNotDecorated
     {
         public string Field1;
@@ -15,6 +17,7 @@ namespace TestCases
         public string Prop2 { get; set; }
     }
 
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class ModelInclude
     {
         [Json] public string Field1;
@@ -23,6 +26,7 @@ namespace TestCases
         public string Prop2 { get; set; }
     }
 
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class ModelExclude
     {
         public string Field1;
@@ -37,6 +41,7 @@ namespace TestCases
         public string Prop3 { get; set; }
     }
 
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class ModelRenamedMembers
     {
         [Json("Field1")] public string Field1;
@@ -46,6 +51,7 @@ namespace TestCases
     }
 
     [Json]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class InstanceObject
     {
         public int IntVal1;
@@ -55,6 +61,7 @@ namespace TestCases
     }
 
     [Json]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     class ModelKeepInstance
     {
         [Json(KeepInstance=true)]
@@ -62,6 +69,15 @@ namespace TestCases
     }
 
     [Json]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
+    class ModelWithInstance
+    {
+        [Json]
+        public InstanceObject InstObj;
+    }
+
+    [Json]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     struct ModelStruct
     {
         public int IntField;
@@ -69,6 +85,7 @@ namespace TestCases
     }
 
     [TestFixture]
+    [Obfuscation(Exclude = true, ApplyToMembers = true)]
     public class TestsReflection
     {
         [Test]
@@ -220,5 +237,35 @@ namespace TestCases
             var o3 = new ModelStruct();
             Assert.Throws<InvalidOperationException>(() => Json.ParseInto(json, o3));
         }
+
+        [Test]
+        public void NullClassMember()
+        {
+            var m = new ModelWithInstance();
+            var json = Json.Format(m);
+
+            Assert.Contains(json, "null");
+
+            m.InstObj = new InstanceObject();
+
+            Json.ParseInto(json, m);
+            Assert.IsNull(m.InstObj);
+        }
+
+        [Test]
+        public void NullClass()
+        {
+            // Save null
+            var json = Json.Format(null);
+            Assert.AreEqual(json, "null");
+
+            // Load null
+            var m = Json.Parse<ModelWithInstance>("null");
+            Assert.IsNull(m);
+
+            // Should fail to parse null into an existing instance
+            m = new ModelWithInstance();
+            Assert.Throws<JsonParseException>(() => Json.ParseInto("null", m));
+        }
     }
 }