public void ReadAsTest() { JsonValue target = new JsonPrimitive(AnyInstance.AnyInt); Assert.Equal(AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), target.ReadAs(typeof(string))); Assert.Equal(AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), target.ReadAs <string>()); object value; double dblValue; Assert.True(target.TryReadAs(typeof(double), out value)); Assert.True(target.TryReadAs <double>(out dblValue)); Assert.Equal(Convert.ToDouble(AnyInstance.AnyInt, CultureInfo.InvariantCulture), (double)value); Assert.Equal(Convert.ToDouble(AnyInstance.AnyInt, CultureInfo.InvariantCulture), dblValue); Assert.False(target.TryReadAs(typeof(Guid), out value), "TryReadAs should have failed to read a double as a Guid"); Assert.Null(value); }
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.Equal(stringRepresentation, (string)jp); Assert.Equal(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.Equal(stringRepresentation, (string)jp); Assert.Equal(stringRepresentation, jp.ReadAs <string>()); } }
public void ReadAsTest() { JsonValue target = new JsonPrimitive(AnyInstance.AnyInt); Assert.Equal(AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), target.ReadAs(typeof(string))); Assert.Equal(AnyInstance.AnyInt.ToString(CultureInfo.InvariantCulture), target.ReadAs<string>()); object value; double dblValue; Assert.True(target.TryReadAs(typeof(double), out value)); Assert.True(target.TryReadAs<double>(out dblValue)); Assert.Equal(Convert.ToDouble(AnyInstance.AnyInt, CultureInfo.InvariantCulture), (double)value); Assert.Equal(Convert.ToDouble(AnyInstance.AnyInt, CultureInfo.InvariantCulture), dblValue); Assert.False(target.TryReadAs(typeof(Guid), out value), "TryReadAs should have failed to read a double as a Guid"); Assert.Null(value); }