-Added support for serializing custom IConvertible values
-Refactored away dependency on IConvertible
This commit is contained in:
@@ -63,6 +63,16 @@ namespace Newtonsoft.Json.Tests
|
||||
writer.Close();
|
||||
Assert.IsTrue(ms.CanRead);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteIConvertable()
|
||||
{
|
||||
var sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
writer.WriteValue(new ConvertibleInt(1));
|
||||
|
||||
Assert.AreEqual("1", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValueFormatting()
|
||||
@@ -1260,7 +1270,103 @@ _____'propertyName': NaN
|
||||
if (appendDelimiters)
|
||||
writer.Write(delimiter);
|
||||
}
|
||||
}
|
||||
|
||||
public struct ConvertibleInt : IConvertible
|
||||
{
|
||||
private readonly int _value;
|
||||
|
||||
public ConvertibleInt(int value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
public TypeCode GetTypeCode()
|
||||
{
|
||||
return TypeCode.Int32;
|
||||
}
|
||||
|
||||
public bool ToBoolean(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public byte ToByte(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public char ToChar(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public DateTime ToDateTime(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public decimal ToDecimal(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public double ToDouble(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public short ToInt16(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public int ToInt32(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public long ToInt64(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public sbyte ToSByte(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public float ToSingle(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToString(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object ToType(Type conversionType, IFormatProvider provider)
|
||||
{
|
||||
if (conversionType == typeof(int))
|
||||
return _value;
|
||||
|
||||
throw new Exception("Type not supported: " + conversionType.FullName);
|
||||
}
|
||||
|
||||
public ushort ToUInt16(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public uint ToUInt32(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ulong ToUInt64(IFormatProvider provider)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ namespace Newtonsoft.Json.Tests
|
||||
DataContractJsonSerializer
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void SerializeSimpleObject()
|
||||
|
||||
@@ -5762,6 +5762,46 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
});
|
||||
}
|
||||
|
||||
public class ConvertableIntTestClass
|
||||
{
|
||||
public ConvertibleInt Integer { get; set; }
|
||||
public ConvertibleInt? NullableInteger1 { get; set; }
|
||||
public ConvertibleInt? NullableInteger2 { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeIConvertible()
|
||||
{
|
||||
ConvertableIntTestClass c = new ConvertableIntTestClass
|
||||
{
|
||||
Integer = new ConvertibleInt(1),
|
||||
NullableInteger1 = new ConvertibleInt(2),
|
||||
NullableInteger2 = null
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Integer"": 1,
|
||||
""NullableInteger1"": 2,
|
||||
""NullableInteger2"": null
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeIConvertible()
|
||||
{
|
||||
string json = @"{
|
||||
""Integer"": 1,
|
||||
""NullableInteger1"": 2,
|
||||
""NullableInteger2"": null
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error converting value 1 to type 'Newtonsoft.Json.Tests.ConvertibleInt'. Path 'Integer', line 2, position 15.",
|
||||
() => JsonConvert.DeserializeObject<ConvertableIntTestClass>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeNullableWidgetStruct()
|
||||
{
|
||||
@@ -6352,7 +6392,7 @@ Parameter name: value",
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTimeOffset_MS()
|
||||
{
|
||||
IDictionary<DateTimeOffset, int> dic1 = new Dictionary<DateTimeOffset, int>
|
||||
IDictionary<DateTimeOffset?, int> dic1 = new Dictionary<DateTimeOffset?, int>
|
||||
{
|
||||
{new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero), 1},
|
||||
{new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero), 2}
|
||||
@@ -6368,7 +6408,7 @@ Parameter name: value",
|
||||
""\/Date(1386850332000+0000)\/"": 2
|
||||
}", json);
|
||||
|
||||
IDictionary<DateTimeOffset, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTimeOffset, int>>(json);
|
||||
IDictionary<DateTimeOffset?, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTimeOffset?, int>>(json);
|
||||
|
||||
Assert.AreEqual(2, dic2.Count);
|
||||
Assert.AreEqual(1, dic2[new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero)]);
|
||||
|
||||
Reference in New Issue
Block a user