public IEnumerator TestDataSerialization_BasicVariableUsage_DataSerializeCorrectly() { var sampleObject = new SampleObject() { Active = true, AgentName = "Backtrace-unity", IntNumber = 1, FloatNumber = 12.123f, DoubleNumber = 555.432d, LongNumber = 999 }; var jObject = new BacktraceJObject(); jObject.Add("AgentName", sampleObject.AgentName); jObject.Add("Active", sampleObject.Active); jObject.Add("IntNumber", sampleObject.IntNumber); jObject.Add("FloatNumber", sampleObject.FloatNumber); jObject.Add("DoubleNumber", sampleObject.DoubleNumber); jObject.Add("LongNumber", sampleObject.LongNumber); var json = jObject.ToJson(); var jsonObject = JsonUtility.FromJson <SampleObject>(json); Assert.AreEqual(sampleObject.AgentName, jsonObject.AgentName); Assert.AreEqual(sampleObject.Active, jsonObject.Active); Assert.AreEqual(sampleObject.IntNumber, jsonObject.IntNumber); Assert.AreEqual(sampleObject.FloatNumber, jsonObject.FloatNumber); Assert.AreEqual(sampleObject.DoubleNumber, jsonObject.DoubleNumber); Assert.AreEqual(sampleObject.LongNumber, jsonObject.LongNumber); yield return(null); }
public IEnumerator TestDataSerialization_ShouldSerializeArray_DataSerializeCorrectly() { var sampleObject = new SampleObject() { StringArray = new string[] { "foo", "bar" }, NumberArray = new int[] { 1, 2, 3, 4 } }; var jObject = new BacktraceJObject(); jObject.Add("StringArray", sampleObject.StringArray); jObject.Add("NumberArray", sampleObject.NumberArray); var json = jObject.ToJson(); var deserializedObject = JsonUtility.FromJson <SampleObject>(json); for (int i = 0; i < sampleObject.StringArray.Length; i++) { Assert.AreEqual(sampleObject.StringArray[i], deserializedObject.StringArray[i]); } for (int i = 0; i < sampleObject.NumberArray.Length; i++) { Assert.AreEqual(sampleObject.NumberArray[i], deserializedObject.NumberArray[i]); } yield return(null); }
public IEnumerator TestDataSerialization_WithComplexValues_DataSerializeCorrectly() { var sampleObject = new SampleObject() { Active = true, AgentName = "Backtrace-unity", LongNumber = 999, NumberArray = new int[] { 1, 2, 3, 4 }, StringArray = new string[] { string.Empty, null, "foo", "bar" }, StringList = new List <string> { string.Empty, null, "foo", "bar" } }; var jObject = new BacktraceJObject(); jObject.Add("AgentName", sampleObject.AgentName); jObject.Add("Active", sampleObject.Active); jObject.Add("LongNumber", sampleObject.LongNumber); jObject.Add("NumberArray", sampleObject.NumberArray); jObject.Add("StringArray", sampleObject.StringArray); jObject.Add("StringList", sampleObject.StringList); var json = jObject.ToJson(); var jsonObject = JsonUtility.FromJson <SampleObject>(json); Assert.AreEqual(sampleObject.AgentName, jsonObject.AgentName); // validate number array for (int i = 0; i < sampleObject.NumberArray.Length; i++) { Assert.AreEqual(jsonObject.NumberArray[i], sampleObject.NumberArray[i]); } // validate string array for (int i = 0; i < sampleObject.StringArray.Length; i++) { // handle empty strings var expectedValue = string.IsNullOrEmpty(sampleObject.StringArray[i]) ? string.Empty : sampleObject.StringArray[i]; Assert.AreEqual(jsonObject.StringArray[i], expectedValue); } // validate string list for (int i = 0; i < sampleObject.StringList.Count; i++) { var expectedValue = string.IsNullOrEmpty(sampleObject.StringList[i]) ? string.Empty : sampleObject.StringList[i]; Assert.AreEqual(jsonObject.StringList[i], expectedValue); } yield return(null); }
public IEnumerator TestDataSerialization_ShouldEscapeInvalidStringValues_DataSerializeCorrectly() { var invalidValue = string.Format("\"{0}\"", "foo"); var sampleObject = new SampleObject() { AgentName = invalidValue }; var jObject = new BacktraceJObject(); jObject.Add("AgentName", invalidValue); var json = jObject.ToJson(); var deserializedObject = JsonUtility.FromJson <SampleObject>(json); Assert.AreEqual(sampleObject.AgentName, deserializedObject.AgentName); yield return(null); }
public IEnumerator TestStringEscapingInSerialization_WithComplexValues_DataSerializeCorrectly() { // this test should validate if we can escape correctly strings from user predefined values var sampleObject = new SampleObject() { StringArray = new string[] { "\"\\!@#$%^&*()+_=-{}{[]:\\\n\r\f\r ", "\b\t\\\n\"''" }, StringList = new List <string>() { "\"\\!@#$%^&*()+_=-{}{[]:\\\n\r\f\r ", "\b\t\\\n\"''" }, NumberArray = null }; var jObject = new BacktraceJObject(); jObject.Add("StringArray", sampleObject.StringArray); jObject.Add("StringList", sampleObject.StringList); jObject.Add("NumberArray", sampleObject.NumberArray); var json = jObject.ToJson(); var jsonObject = JsonUtility.FromJson <SampleObject>(json); Assert.IsNotNull(jsonObject); Assert.IsEmpty(jsonObject.NumberArray); for (int i = 0; i < sampleObject.StringArray.Length; i++) { // handle empty strings Assert.AreEqual(jsonObject.StringArray[i], sampleObject.StringArray[i]); } // validate string list for (int i = 0; i < sampleObject.StringList.Count; i++) { Assert.AreEqual(jsonObject.StringList[i], sampleObject.StringList[i]); } yield return(null); }
public IEnumerator TestStringEscapingInSerialization_WithPrimitiveValues_DataSerializeCorrectly() { // this test should validate if we can escape correctly strings from user predefined values var sampleObject = new SampleObject() { AgentName = "\"\\!@#$%^&*()+_=-{}{[]:\\\n\r\f\r ", TestString = "\b\t\\\n\"''" }; var jObject = new BacktraceJObject(); jObject.Add("AgentName", sampleObject.AgentName); jObject.Add("TestString", sampleObject.TestString); var json = jObject.ToJson(); var jsonObject = JsonUtility.FromJson <SampleObject>(json); Assert.IsNotNull(jsonObject); Assert.AreEqual(sampleObject.AgentName, jsonObject.AgentName); Assert.AreEqual(sampleObject.TestString, jsonObject.TestString); yield return(null); }
public IEnumerator TestDataSerialization_WithOnlyComplexValues_DataSerializeCorrectly() { // this test should validate if we can start analysing new data type without previous data types var sampleObject = new SampleObject() { NumberArray = new int[] { 1, 2, 3, 4 } }; var jObject = new BacktraceJObject(); jObject.Add("NumberArray", sampleObject.NumberArray); var json = jObject.ToJson(); var jsonObject = JsonUtility.FromJson <SampleObject>(json); // validate number array for (int i = 0; i < sampleObject.NumberArray.Length; i++) { Assert.AreEqual(jsonObject.NumberArray[i], sampleObject.NumberArray[i]); } yield return(null); }
public IEnumerator TestDataSerialization_ShouldSerializeInnerJObject_DataSerializeCorrectly() { var sampleObject = new SampleObject() { StringArray = new string[] { "foo", "bar" }, NumberArray = new int[] { 1, 2, 3, 4 } }; var jObject = new BacktraceJObject(); jObject.Add("StringArray", sampleObject.StringArray); jObject.Add("NumberArray", sampleObject.NumberArray); var inner = new BacktraceJObject(); inner.Add("foo", "bar"); jObject.Add("inner", inner); var json = jObject.ToJson(); Assert.IsNotEmpty(json); yield return(null); }
public IEnumerator TestDataSerialization_WithUserPredefinedValues_DataSerializeCorrectly() { // this test should validate if we can start analysing new data type without previous data types var sampleObject = new SampleObject() { AgentName = "Backtrace-unity", TestString = "Test string" }; var jObject = new BacktraceJObject(new Dictionary <string, string>() { { "AgentName", sampleObject.AgentName }, { "TestString", sampleObject.TestString } }); var json = jObject.ToJson(); var jsonObject = JsonUtility.FromJson <SampleObject>(json); Assert.IsNotNull(jsonObject); Assert.AreEqual(sampleObject.AgentName, jsonObject.AgentName); Assert.AreEqual(sampleObject.TestString, jsonObject.TestString); yield return(null); }
public IEnumerator TestDataSerialization_ShouldSerializeList_DataSerializeCorrectly() { var sampleObject = new SampleObject() { StringList = new List <string>() { "foo", "bar" } }; var jObject = new BacktraceJObject(); jObject.Add("StringList", sampleObject.StringList); var json = jObject.ToJson(); var deserializedObject = JsonUtility.FromJson <SampleObject>(json); for (int i = 0; i < sampleObject.StringList.Count; i++) { Assert.AreEqual(sampleObject.StringList.ElementAt(i), deserializedObject.StringList.ElementAt(i)); } yield return(null); }