public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string parameterType = (string)parameter; JsonPrimitive jp = (JsonPrimitive)value; object result = null; switch (parameterType) { case "int": result = jp.ReadAs <int>(0); break; case "date": result = jp.ReadAs <DateTime>(DateTime.Now); break; default: result = jp.ReadAs <string>(); break; } return(result); }
public void NonNumberToStringConversionTest() { Dictionary <string, JsonPrimitive> allValues = new Dictionary <string, JsonPrimitive> { { new string(AnyInstance.AnyChar, 1), new JsonPrimitive(AnyInstance.AnyChar) }, { AnyInstance.AnyBool.ToString(CultureInfo.InvariantCulture).ToLowerInvariant(), new JsonPrimitive(AnyInstance.AnyBool) }, { AnyInstance.AnyGuid.ToString("D", CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyGuid) }, { AnyInstance.AnyDateTime.ToString(DateTimeFormat, CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyDateTime) }, { AnyInstance.AnyDateTimeOffset.ToString(DateTimeFormat, CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyDateTimeOffset) }, }; foreach (char escapedChar in "\r\n\t\u0000\uffff\u001f\\\"") { allValues.Add(new string(escapedChar, 1), new JsonPrimitive(escapedChar)); } foreach (string stringRepresentation in allValues.Keys) { JsonPrimitive jp = allValues[stringRepresentation]; Assert.AreEqual(stringRepresentation, (string)jp); Assert.AreEqual(stringRepresentation, jp.ReadAs <string>()); } }
public void NumberToStringConverstionTest() { Dictionary <string, JsonPrimitive> allNumbers = new Dictionary <string, JsonPrimitive> { { AnyInstance.AnyByte.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyByte) }, { AnyInstance.AnySByte.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnySByte) }, { AnyInstance.AnyShort.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyShort) }, { AnyInstance.AnyUShort.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyUShort) }, { AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyInt) }, { AnyInstance.AnyUInt.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyUInt) }, { AnyInstance.AnyLong.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyLong) }, { AnyInstance.AnyULong.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyULong) }, { AnyInstance.AnyDecimal.ToString(CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyDecimal) }, { AnyInstance.AnyDouble.ToString("R", CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyDouble) }, { AnyInstance.AnyFloat.ToString("R", CultureInfo.InvariantCulture), new JsonPrimitive(AnyInstance.AnyFloat) }, }; foreach (string stringRepresentation in allNumbers.Keys) { JsonPrimitive jp = allNumbers[stringRepresentation]; Assert.AreEqual(stringRepresentation, (string)jp); Assert.AreEqual(stringRepresentation, jp.ReadAs <string>()); } }
public void ReadAsTest() { JsonValue target = new JsonPrimitive(AnyInstance.AnyInt); Assert.AreEqual(AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), target.ReadAs(typeof(string))); Assert.AreEqual(AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), target.ReadAs <string>()); object value; double dblValue; Assert.IsTrue(target.TryReadAs(typeof(double), out value)); Assert.IsTrue(target.TryReadAs <double>(out dblValue)); Assert.AreEqual(Convert.ToDouble(AnyInstance.AnyInt, CultureInfo.InvariantCulture), (double)value); Assert.AreEqual(Convert.ToDouble(AnyInstance.AnyInt, CultureInfo.InvariantCulture), dblValue); Assert.IsFalse(target.TryReadAs(typeof(Guid), out value), "TryReadAs should have failed to read a double as a Guid"); Assert.IsNull(value, "expected from failed TryReadAs should be null!"); }