void JsonTimeStampHandlesAllFormats() { string expectedJson, actualJson; DateTime expectedTime; ObjWithTimes actualObj = new ObjWithTimes(); for (int i = 0; i < _examples.Length; i++) { // Define the time deserialization expectation UUnitAssert.True(DateTime.TryParseExact(_examples[i], Util._defaultDateTimeFormats, CultureInfo.CurrentCulture, DateTimeStyles.RoundtripKind, out expectedTime), "Index: " + i + "/" + _examples.Length + ", " + _examples[i]); // De-serialize the time using json expectedJson = "{\"timestamp\":\"" + _examples[i] + "\"}"; // We are provided a json string with every random time format actualObj = SimpleJson.DeserializeObject <ObjWithTimes>(expectedJson, Util.ApiSerializerStrategy); actualJson = SimpleJson.SerializeObject(actualObj, Util.ApiSerializerStrategy); if (i == Util.DEFAULT_UTC_OUTPUT_INDEX) // This is the only case where the json input will match the json output { UUnitAssert.StringEquals(expectedJson, actualJson); } // Verify that the times match double diff = (expectedTime - actualObj.timestamp).TotalSeconds; // We expect that we have parsed the time correctly according to expectations UUnitAssert.True(diff < 1, "\nActual time: " + actualObj.timestamp + " vs expected time: " + expectedTime + ", diff: " + diff + "\nActual json: " + actualJson + " vs expected json: " + expectedJson ); } }
void EnumConversionTest_Serialize() { string expectedJson, actualJson; EnumConversionTestClass expectedObj = new EnumConversionTestClass(), actualObj = new EnumConversionTestClass(); expectedObj.enumList = new List <testRegion>() { testRegion.USEast, testRegion.USCentral, testRegion.Japan }; expectedObj.enumArray = new testRegion[] { testRegion.USEast, testRegion.USCentral, testRegion.Japan }; expectedObj.enumValue = testRegion.Australia; expectedJson = "{\"enumList\":[\"USEast\",\"USCentral\",\"Japan\"],\"enumArray\":[\"USEast\",\"USCentral\",\"Japan\"],\"enumValue\":\"Australia\"}"; JsonConvert.PopulateObject(expectedJson, actualObj, Util.JsonSettings); actualJson = JsonConvert.SerializeObject(actualObj, Util.JsonFormatting, Util.JsonSettings); UUnitAssert.StringEquals(expectedJson.Replace(" ", "").Replace("\n", ""), actualJson.Replace(" ", "").Replace("\n", "")); UUnitAssert.ObjEquals(expectedObj, actualObj); }
void EnumConversionTest_Serialize() { string expectedJson, actualJson; EnumConversionTestClass expectedObj = new EnumConversionTestClass(), actualObj; expectedObj.enumList = new List <testRegion>() { testRegion.USEast, testRegion.USCentral, testRegion.Japan }; expectedObj.enumArray = new testRegion[] { testRegion.USEast, testRegion.USCentral, testRegion.Japan }; expectedObj.enumValue = testRegion.Australia; expectedObj.optEnumValue = null; expectedJson = "{\"enumList\":[\"USEast\",\"USCentral\",\"Japan\"],\"enumArray\":[\"USEast\",\"USCentral\",\"Japan\"],\"enumValue\":\"Australia\",\"optEnumValue\":null}"; actualObj = SimpleJson.DeserializeObject <EnumConversionTestClass>(expectedJson, Util.ApiSerializerStrategy); actualJson = SimpleJson.SerializeObject(actualObj, Util.ApiSerializerStrategy); UUnitAssert.StringEquals(expectedJson.Replace(" ", "").Replace("\n", ""), actualJson.Replace(" ", "").Replace("\n", "")); UUnitAssert.ObjEquals(expectedObj, actualObj); }