-Added support for serializing custom IConvertible values

-Refactored away dependency on IConvertible
This commit is contained in:
James Newton-King
2013-04-01 12:38:45 +13:00
parent 08c7055d91
commit b5e7005735
17 changed files with 704 additions and 957 deletions
@@ -64,6 +64,16 @@ namespace Newtonsoft.Json.Tests
Assert.IsTrue(ms.CanRead); 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] [Test]
public void ValueFormatting() public void ValueFormatting()
{ {
@@ -1260,7 +1270,103 @@ _____'propertyName': NaN
if (appendDelimiters) if (appendDelimiters)
writer.Write(delimiter); 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 DataContractJsonSerializer
} }
#endregion #endregion
[Test] [Test]
public void SerializeSimpleObject() 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] [Test]
public void SerializeNullableWidgetStruct() public void SerializeNullableWidgetStruct()
{ {
@@ -6352,7 +6392,7 @@ Parameter name: value",
[Test] [Test]
public void DateTimeDictionaryKey_DateTimeOffset_MS() 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(2000, 12, 12, 12, 12, 12, TimeSpan.Zero), 1},
{new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero), 2} {new DateTimeOffset(2013, 12, 12, 12, 12, 12, TimeSpan.Zero), 2}
@@ -6368,7 +6408,7 @@ Parameter name: value",
""\/Date(1386850332000+0000)\/"": 2 ""\/Date(1386850332000+0000)\/"": 2
}", json); }", 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(2, dic2.Count);
Assert.AreEqual(1, dic2[new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero)]); Assert.AreEqual(1, dic2[new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero)]);
@@ -70,6 +70,7 @@ namespace Newtonsoft.Json.Converters
if (char.IsNumber(enumName[0]) || enumName[0] == '-') if (char.IsNumber(enumName[0]) || enumName[0] == '-')
{ {
// enum value has no name so write number
writer.WriteValue(value); writer.WriteValue(value);
} }
else else
+51 -103
View File
@@ -32,6 +32,7 @@ using System.Numerics;
#if !(NET20 || NET35 || SILVERLIGHT) #if !(NET20 || NET35 || SILVERLIGHT)
using System.Threading.Tasks; using System.Threading.Tasks;
#endif #endif
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
using System.Xml; using System.Xml;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
@@ -39,9 +40,6 @@ using System.Text;
#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE) #if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE)
using System.Xml.Linq; using System.Xml.Linq;
#endif #endif
#if NETFX_CORE || PORTABLE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
namespace Newtonsoft.Json namespace Newtonsoft.Json
{ {
@@ -625,128 +623,78 @@ namespace Newtonsoft.Json
if (value == null) if (value == null)
return Null; return Null;
IConvertible convertible = ConvertUtils.ToConvertible(value); PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(value);
if (convertible != null) switch (typeCode)
{ {
switch (convertible.GetTypeCode()) case PrimitiveTypeCode.String:
{ return ToString((string) value);
case TypeCode.String: case PrimitiveTypeCode.Char:
return ToString(convertible.ToString(CultureInfo.InvariantCulture)); return ToString((char) value);
case TypeCode.Char: case PrimitiveTypeCode.Boolean:
return ToString(convertible.ToChar(CultureInfo.InvariantCulture)); return ToString((bool) value);
case TypeCode.Boolean: case PrimitiveTypeCode.SByte:
return ToString(convertible.ToBoolean(CultureInfo.InvariantCulture)); return ToString((sbyte) value);
case TypeCode.SByte: case PrimitiveTypeCode.Int16:
return ToString(convertible.ToSByte(CultureInfo.InvariantCulture)); return ToString((short) value);
case TypeCode.Int16: case PrimitiveTypeCode.UInt16:
return ToString(convertible.ToInt16(CultureInfo.InvariantCulture)); return ToString((ushort) value);
case TypeCode.UInt16: case PrimitiveTypeCode.Int32:
return ToString(convertible.ToUInt16(CultureInfo.InvariantCulture)); return ToString((int) value);
case TypeCode.Int32: case PrimitiveTypeCode.Byte:
return ToString(convertible.ToInt32(CultureInfo.InvariantCulture)); return ToString((byte) value);
case TypeCode.Byte: case PrimitiveTypeCode.UInt32:
return ToString(convertible.ToByte(CultureInfo.InvariantCulture)); return ToString((uint) value);
case TypeCode.UInt32: case PrimitiveTypeCode.Int64:
return ToString(convertible.ToUInt32(CultureInfo.InvariantCulture)); return ToString((long) value);
case TypeCode.Int64: case PrimitiveTypeCode.UInt64:
return ToString(convertible.ToInt64(CultureInfo.InvariantCulture)); return ToString((ulong) value);
case TypeCode.UInt64: case PrimitiveTypeCode.Single:
return ToString(convertible.ToUInt64(CultureInfo.InvariantCulture)); return ToString((float) value);
case TypeCode.Single: case PrimitiveTypeCode.Double:
return ToString(convertible.ToSingle(CultureInfo.InvariantCulture)); return ToString((double) value);
case TypeCode.Double: case PrimitiveTypeCode.DateTime:
return ToString(convertible.ToDouble(CultureInfo.InvariantCulture)); return ToString((DateTime) value);
case TypeCode.DateTime: case PrimitiveTypeCode.Decimal:
return ToString(convertible.ToDateTime(CultureInfo.InvariantCulture)); return ToString((decimal) value);
case TypeCode.Decimal:
return ToString(convertible.ToDecimal(CultureInfo.InvariantCulture));
#if !(NETFX_CORE || PORTABLE) #if !(NETFX_CORE || PORTABLE)
case TypeCode.DBNull: case PrimitiveTypeCode.DBNull:
return Null; return Null;
#endif #endif
}
}
#if !NET20 #if !NET20
else if (value is DateTimeOffset) case PrimitiveTypeCode.DateTimeOffset:
{
return ToString((DateTimeOffset) value); return ToString((DateTimeOffset) value);
}
#endif #endif
else if (value is Guid) case PrimitiveTypeCode.Guid:
{
return ToString((Guid) value); return ToString((Guid) value);
} case PrimitiveTypeCode.Uri:
else if (value is Uri)
{
return ToString((Uri) value); return ToString((Uri) value);
} case PrimitiveTypeCode.TimeSpan:
else if (value is TimeSpan)
{
return ToString((TimeSpan) value); return ToString((TimeSpan) value);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
else if (value is BigInteger) case PrimitiveTypeCode.BigInteger:
{
return ToString((BigInteger) value); return ToString((BigInteger) value);
}
#endif #endif
}
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
} }
private static bool IsJsonPrimitiveTypeCode(TypeCode typeCode) internal static bool IsJsonPrimitiveType(Type t)
{ {
switch (typeCode) PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(t);
{
case TypeCode.String: if (typeCode != PrimitiveTypeCode.Empty && typeCode != PrimitiveTypeCode.Object)
case TypeCode.Char:
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.Byte:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.DateTime:
case TypeCode.Decimal:
#if !(NETFX_CORE || PORTABLE)
case TypeCode.DBNull:
#endif
return true; return true;
default:
if (typeof(IConvertible).IsAssignableFrom(t)
|| (ReflectionUtils.IsNullableType(t) && typeof(IConvertible).IsAssignableFrom(Nullable.GetUnderlyingType(t))))
{
return !typeof(JToken).IsAssignableFrom(t);
}
return false; return false;
} }
}
internal static bool IsJsonPrimitiveType(Type type)
{
if (ReflectionUtils.IsNullableType(type))
type = Nullable.GetUnderlyingType(type);
#if !NET20
if (type == typeof (DateTimeOffset))
return true;
#endif
if (type == typeof (byte[]))
return true;
if (type == typeof (Uri))
return true;
if (type == typeof (TimeSpan))
return true;
if (type == typeof (Guid))
return true;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (type == typeof (BigInteger))
return true;
#endif
return IsJsonPrimitiveTypeCode(ConvertUtils.GetTypeCode(type));
}
internal static bool TryParseDateTime(string s, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt) internal static bool TryParseDateTime(string s, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt)
{ {
+1 -3
View File
@@ -621,9 +621,7 @@ namespace Newtonsoft.Json
if (Value != null) if (Value != null)
{ {
string s; string s;
if (ConvertUtils.IsConvertible(Value)) if (Value is IFormattable)
s = ConvertUtils.ToConvertible(Value).ToString(Culture);
else if (Value is IFormattable)
s = ((IFormattable)Value).ToString(null, Culture); s = ((IFormattable)Value).ToString(null, Culture);
else else
s = Value.ToString(); s = Value.ToString();
+18 -10
View File
@@ -47,6 +47,7 @@ namespace Newtonsoft.Json
private int _indentation; private int _indentation;
private char _quoteChar; private char _quoteChar;
private bool _quoteName; private bool _quoteName;
private bool[] _charEscapeFlags;
private Base64Encoder Base64Encoder private Base64Encoder Base64Encoder
{ {
@@ -86,6 +87,7 @@ namespace Newtonsoft.Json
throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and ""."); throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
_quoteChar = value; _quoteChar = value;
UpdateCharEscapeFlags();
} }
} }
@@ -121,6 +123,8 @@ namespace Newtonsoft.Json
_quoteName = true; _quoteName = true;
_indentChar = ' '; _indentChar = ' ';
_indentation = 2; _indentation = 2;
UpdateCharEscapeFlags();
} }
/// <summary> /// <summary>
@@ -209,7 +213,7 @@ namespace Newtonsoft.Json
{ {
InternalWritePropertyName(name); InternalWritePropertyName(name);
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName, GetCharEscapeFlags(), StringEscapeHandling); JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName, _charEscapeFlags, StringEscapeHandling);
_writer.Write(':'); _writer.Write(':');
} }
@@ -225,7 +229,7 @@ namespace Newtonsoft.Json
if (escape) if (escape)
{ {
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName, GetCharEscapeFlags(), StringEscapeHandling); JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName, _charEscapeFlags, StringEscapeHandling);
} }
else else
{ {
@@ -241,15 +245,19 @@ namespace Newtonsoft.Json
_writer.Write(':'); _writer.Write(':');
} }
private bool[] GetCharEscapeFlags() internal override void OnStringEscapeHandlingChanged()
{
UpdateCharEscapeFlags();
}
private void UpdateCharEscapeFlags()
{ {
if (StringEscapeHandling == StringEscapeHandling.EscapeHtml) if (StringEscapeHandling == StringEscapeHandling.EscapeHtml)
return JavaScriptUtils.HtmlCharEscapeFlags; _charEscapeFlags = JavaScriptUtils.HtmlCharEscapeFlags;
else if (_quoteChar == '"')
if (_quoteChar == '"') _charEscapeFlags = JavaScriptUtils.DoubleQuoteCharEscapeFlags;
return JavaScriptUtils.DoubleQuoteCharEscapeFlags; else
_charEscapeFlags = JavaScriptUtils.SingleQuoteCharEscapeFlags;
return JavaScriptUtils.SingleQuoteCharEscapeFlags;
} }
/// <summary> /// <summary>
@@ -335,7 +343,7 @@ namespace Newtonsoft.Json
if (value == null) if (value == null)
WriteValueInternal(JsonConvert.Null, JsonToken.Null); WriteValueInternal(JsonConvert.Null, JsonToken.Null);
else else
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true, GetCharEscapeFlags(), StringEscapeHandling); JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true, _charEscapeFlags, StringEscapeHandling);
} }
/// <summary> /// <summary>
+162 -170
View File
@@ -31,9 +31,6 @@ using System.Numerics;
#endif #endif
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
using System.Globalization; using System.Globalization;
#if NETFX_CORE || PORTABLE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
#if NET20 #if NET20
using Newtonsoft.Json.Utilities.LinqBridge; using Newtonsoft.Json.Utilities.LinqBridge;
#else #else
@@ -250,7 +247,16 @@ namespace Newtonsoft.Json
public StringEscapeHandling StringEscapeHandling public StringEscapeHandling StringEscapeHandling
{ {
get { return _stringEscapeHandling; } get { return _stringEscapeHandling; }
set { _stringEscapeHandling = value; } set
{
_stringEscapeHandling = value;
OnStringEscapeHandlingChanged();
}
}
internal virtual void OnStringEscapeHandlingChanged()
{
// hacky but there is a calculated value that relies on StringEscapeHandling
} }
/// <summary> /// <summary>
@@ -1254,176 +1260,11 @@ namespace Newtonsoft.Json
/// </summary> /// </summary>
/// <param name="value">The <see cref="Object"/> value to write.</param> /// <param name="value">The <see cref="Object"/> value to write.</param>
public virtual void WriteValue(object value) public virtual void WriteValue(object value)
{
WriteValueInternal(value, false);
}
/// <summary>
/// Writes a <see cref="Object"/> value.
/// An error will raised if the value cannot be written as a single JSON token.
/// </summary>
/// <param name="value">The <see cref="Object"/> value to write.</param>
/// <param name="nullable">A flag indicating whether the value is nullable.</param>
public virtual void WriteValue(object value, bool nullable)
{
WriteValueInternal(value, nullable);
}
private void WriteValueInternal(object value, bool nullable)
{ {
if (value == null) if (value == null)
{
WriteNull(); WriteNull();
return;
}
IConvertible convertible = ConvertUtils.ToConvertible(value);
if (convertible != null)
{
switch (convertible.GetTypeCode())
{
case TypeCode.String:
WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
return;
case TypeCode.Char:
if (nullable)
WriteValue((char?)convertible.ToChar(CultureInfo.InvariantCulture));
else else
WriteValue(convertible.ToChar(CultureInfo.InvariantCulture)); WriteValue(this, ConvertUtils.GetTypeCode(value), value);
return;
case TypeCode.Boolean:
if (nullable)
WriteValue((bool?)convertible.ToBoolean(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
return;
case TypeCode.SByte:
if (nullable)
WriteValue((sbyte?)convertible.ToSByte(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
return;
case TypeCode.Int16:
if (nullable)
WriteValue((short?)convertible.ToInt16(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
return;
case TypeCode.UInt16:
if (nullable)
WriteValue((ushort?)convertible.ToUInt16(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
return;
case TypeCode.Int32:
if (nullable)
WriteValue((int?)convertible.ToInt32(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
return;
case TypeCode.Byte:
if (nullable)
WriteValue((byte?)convertible.ToByte(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
return;
case TypeCode.UInt32:
if (nullable)
WriteValue((uint?)convertible.ToUInt32(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
return;
case TypeCode.Int64:
if (nullable)
WriteValue((long?)convertible.ToInt64(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
return;
case TypeCode.UInt64:
if (nullable)
WriteValue((ulong?)convertible.ToUInt64(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
return;
case TypeCode.Single:
if (nullable)
WriteValue((float?)convertible.ToSingle(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
return;
case TypeCode.Double:
if (nullable)
WriteValue((double?)convertible.ToDouble(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
return;
case TypeCode.DateTime:
if (nullable)
WriteValue((DateTime?)convertible.ToDateTime(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
return;
case TypeCode.Decimal:
if (nullable)
WriteValue((decimal?)convertible.ToDecimal(CultureInfo.InvariantCulture));
else
WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
return;
#if !(NETFX_CORE || PORTABLE)
case TypeCode.DBNull:
WriteNull();
return;
#endif
}
}
#if !NET20
else if (value is DateTimeOffset)
{
if (nullable)
WriteValue((DateTimeOffset?)value);
else
WriteValue((DateTimeOffset)value);
return;
}
#endif
else if (value is byte[])
{
WriteValue((byte[])value);
return;
}
else if (value is Guid)
{
if (nullable)
WriteValue((Guid?)value);
else
WriteValue((Guid)value);
return;
}
else if (value is Uri)
{
WriteValue((Uri)value);
return;
}
else if (value is TimeSpan)
{
if (nullable)
WriteValue((TimeSpan?)value);
else
WriteValue((TimeSpan)value);
return;
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
else if (value is BigInteger)
{
if (nullable)
WriteValue((BigInteger?)value);
else
WriteValue((BigInteger)value);
return;
}
#endif
throw JsonWriterException.Create(this, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
} }
#endregion #endregion
@@ -1469,5 +1310,156 @@ namespace Newtonsoft.Json
if (_currentState != State.Closed) if (_currentState != State.Closed)
Close(); Close();
} }
internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
{
switch (typeCode)
{
case PrimitiveTypeCode.Char:
writer.WriteValue((char)value);
break;
case PrimitiveTypeCode.CharNullable:
writer.WriteValue((value == null) ? (char?)null : (char)value);
break;
case PrimitiveTypeCode.Boolean:
writer.WriteValue((bool)value);
break;
case PrimitiveTypeCode.BooleanNullable:
writer.WriteValue((value == null) ? (bool?)null : (bool)value);
break;
case PrimitiveTypeCode.SByte:
writer.WriteValue((sbyte)value);
break;
case PrimitiveTypeCode.SByteNullable:
writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value);
break;
case PrimitiveTypeCode.Int16:
writer.WriteValue((short)value);
break;
case PrimitiveTypeCode.Int16Nullable:
writer.WriteValue((value == null) ? (short?)null : (short)value);
break;
case PrimitiveTypeCode.UInt16:
writer.WriteValue((ushort)value);
break;
case PrimitiveTypeCode.UInt16Nullable:
writer.WriteValue((value == null) ? (ushort?)null : (ushort)value);
break;
case PrimitiveTypeCode.Int32:
writer.WriteValue((int)value);
break;
case PrimitiveTypeCode.Int32Nullable:
writer.WriteValue((value == null) ? (int?)null : (int)value);
break;
case PrimitiveTypeCode.Byte:
writer.WriteValue((byte)value);
break;
case PrimitiveTypeCode.ByteNullable:
writer.WriteValue((value == null) ? (byte?)null : (byte)value);
break;
case PrimitiveTypeCode.UInt32:
writer.WriteValue((uint)value);
break;
case PrimitiveTypeCode.UInt32Nullable:
writer.WriteValue((value == null) ? (uint?)null : (uint)value);
break;
case PrimitiveTypeCode.Int64:
writer.WriteValue((long)value);
break;
case PrimitiveTypeCode.Int64Nullable:
writer.WriteValue((value == null) ? (long?)null : (long)value);
break;
case PrimitiveTypeCode.UInt64:
writer.WriteValue((ulong)value);
break;
case PrimitiveTypeCode.UInt64Nullable:
writer.WriteValue((value == null) ? (ulong?)null : (ulong)value);
break;
case PrimitiveTypeCode.Single:
writer.WriteValue((float)value);
break;
case PrimitiveTypeCode.SingleNullable:
writer.WriteValue((value == null) ? (float?)null : (float)value);
break;
case PrimitiveTypeCode.Double:
writer.WriteValue((double)value);
break;
case PrimitiveTypeCode.DoubleNullable:
writer.WriteValue((value == null) ? (double?)null : (double)value);
break;
case PrimitiveTypeCode.DateTime:
writer.WriteValue((DateTime)value);
break;
case PrimitiveTypeCode.DateTimeNullable:
writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value);
break;
#if !NET20
case PrimitiveTypeCode.DateTimeOffset:
writer.WriteValue((DateTimeOffset)value);
break;
case PrimitiveTypeCode.DateTimeOffsetNullable:
writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value);
break;
#endif
case PrimitiveTypeCode.Decimal:
writer.WriteValue((decimal)value);
break;
case PrimitiveTypeCode.DecimalNullable:
writer.WriteValue((value == null) ? (decimal?)null : (decimal)value);
break;
case PrimitiveTypeCode.Guid:
writer.WriteValue((Guid)value);
break;
case PrimitiveTypeCode.GuidNullable:
writer.WriteValue((value == null) ? (Guid?)null : (Guid)value);
break;
case PrimitiveTypeCode.TimeSpan:
writer.WriteValue((TimeSpan)value);
break;
case PrimitiveTypeCode.TimeSpanNullable:
writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value);
break;
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
case PrimitiveTypeCode.BigInteger:
writer.WriteValue((BigInteger)value);
break;
case PrimitiveTypeCode.BigIntegerNullable:
writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value);
break;
#endif
case PrimitiveTypeCode.Uri:
writer.WriteValue((Uri)value);
break;
case PrimitiveTypeCode.String:
writer.WriteValue((string)value);
break;
case PrimitiveTypeCode.Bytes:
writer.WriteValue((byte[])value);
break;
#if !(PORTABLE || NETFX_CORE)
case PrimitiveTypeCode.DBNull:
writer.WriteNull();
break;
#endif
default:
#if !(PORTABLE || NETFX_CORE)
if (value is IConvertible)
{
// the value is a non-standard IConvertible
// convert to the underlying value and retry
IConvertible convertable = (IConvertible)value;
TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable);
object convertedValue = convertable.ToType(typeInformation.Type, CultureInfo.InvariantCulture);
WriteValue(writer, typeInformation.TypeCode, convertedValue);
}
else
#endif
{
throw JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
}
break;
}
}
} }
} }
+43 -78
View File
@@ -1636,126 +1636,91 @@ namespace Newtonsoft.Json.Linq
private object ToObject(Type objectType, bool isNullable) private object ToObject(Type objectType, bool isNullable)
{ {
TypeCode typeCode = ConvertUtils.GetTypeCode(objectType); PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(objectType);
switch (typeCode) switch (typeCode)
{ {
case TypeCode.Object: case PrimitiveTypeCode.BooleanNullable:
if (!isNullable && ReflectionUtils.IsNullableType(objectType))
return ToObject(Nullable.GetUnderlyingType(objectType), true);
// fall back to standard JsonSerializer
break;
case TypeCode.Boolean:
if (isNullable)
return (bool?) this; return (bool?) this;
case PrimitiveTypeCode.Boolean:
return (bool) this; return (bool) this;
case TypeCode.Char: case PrimitiveTypeCode.CharNullable:
if (isNullable)
return (char?) this; return (char?) this;
case PrimitiveTypeCode.Char:
return (char) this; return (char) this;
case TypeCode.SByte: case PrimitiveTypeCode.SByte:
if (isNullable)
return (sbyte?) this; return (sbyte?) this;
case PrimitiveTypeCode.SByteNullable:
return (sbyte) this; return (sbyte) this;
case TypeCode.Byte: case PrimitiveTypeCode.ByteNullable:
if (isNullable)
return (byte?) this; return (byte?) this;
case PrimitiveTypeCode.Byte:
return (byte) this; return (byte) this;
case TypeCode.Int16: case PrimitiveTypeCode.Int16Nullable:
if (isNullable)
return (short?) this; return (short?) this;
case PrimitiveTypeCode.Int16:
return (short) this; return (short) this;
case TypeCode.UInt16: case PrimitiveTypeCode.UInt16Nullable:
if (isNullable)
return (ushort?) this; return (ushort?) this;
case PrimitiveTypeCode.UInt16:
return (ushort) this; return (ushort) this;
case TypeCode.Int32: case PrimitiveTypeCode.Int32Nullable:
if (isNullable)
return (int?) this; return (int?) this;
case PrimitiveTypeCode.Int32:
return (int) this; return (int) this;
case TypeCode.UInt32: case PrimitiveTypeCode.UInt32Nullable:
if (isNullable)
return (uint?) this; return (uint?) this;
case PrimitiveTypeCode.UInt32:
return (uint) this; return (uint) this;
case TypeCode.Int64: case PrimitiveTypeCode.Int64Nullable:
if (isNullable)
return (long?) this; return (long?) this;
case PrimitiveTypeCode.Int64:
return (long) this; return (long) this;
case TypeCode.UInt64: case PrimitiveTypeCode.UInt64Nullable:
if (isNullable)
return (ulong?) this; return (ulong?) this;
case PrimitiveTypeCode.UInt64:
return (ulong) this; return (ulong) this;
case TypeCode.Single: case PrimitiveTypeCode.SingleNullable:
if (isNullable)
return (float?) this; return (float?) this;
case PrimitiveTypeCode.Single:
return (float) this; return (float) this;
case TypeCode.Double: case PrimitiveTypeCode.DoubleNullable:
if (isNullable)
return (double?) this; return (double?) this;
case PrimitiveTypeCode.Double:
return (double) this; return (double) this;
case TypeCode.Decimal: case PrimitiveTypeCode.DecimalNullable:
if (isNullable)
return (decimal?) this; return (decimal?) this;
case PrimitiveTypeCode.Decimal:
return (decimal) this; return (decimal) this;
case TypeCode.DateTime: case PrimitiveTypeCode.DateTimeNullable:
if (isNullable)
return (DateTime?) this; return (DateTime?) this;
case PrimitiveTypeCode.DateTime:
return (DateTime) this; return (DateTime) this;
case TypeCode.String:
return (string) this;
}
#if !NET20 #if !NET20
if (objectType == typeof (DateTimeOffset)) case PrimitiveTypeCode.DateTimeOffsetNullable:
{
if (isNullable)
return (DateTimeOffset?) this; return (DateTimeOffset?) this;
case PrimitiveTypeCode.DateTimeOffset:
return (DateTimeOffset) this; return (DateTimeOffset) this;
}
#endif #endif
if (objectType == typeof (Guid)) case PrimitiveTypeCode.String:
{ return (string) this;
if (isNullable) case PrimitiveTypeCode.GuidNullable:
return (Guid?) this; return (Guid?) this;
case PrimitiveTypeCode.Guid:
return (Guid) this; return (Guid) this;
} case PrimitiveTypeCode.Uri:
if (objectType == typeof (Uri))
{
return (Uri) this; return (Uri) this;
} case PrimitiveTypeCode.TimeSpanNullable:
if (objectType == typeof (TimeSpan)) return (TimeSpan?)this;
{ case PrimitiveTypeCode.TimeSpan:
if (isNullable) return (TimeSpan)this;
return (TimeSpan?) this;
return (TimeSpan) this;
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (objectType == typeof(BigInteger)) case PrimitiveTypeCode.BigIntegerNullable:
{
if (isNullable)
return (BigInteger?)this; return (BigInteger?)this;
case PrimitiveTypeCode.BigInteger:
return (BigInteger)this; return (BigInteger)this;
}
#endif #endif
}
return ToObject(objectType, new JsonSerializer()); return ToObject(objectType, new JsonSerializer());
} }
@@ -30,9 +30,6 @@ using System.Collections.Generic;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
#if NETFX_CORE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
#if NET20 #if NET20
using Newtonsoft.Json.Utilities.LinqBridge; using Newtonsoft.Json.Utilities.LinqBridge;
#else #else
@@ -321,8 +318,10 @@ namespace Newtonsoft.Json.Schema
if (keyType != null) if (keyType != null)
{ {
JsonContract keyContract = ContractResolver.ResolveContract(keyType);
// can be converted to a string // can be converted to a string
if (ConvertUtils.IsConvertible(keyType)) if (keyContract.ContractType == JsonContractType.Primitive)
{ {
CurrentSchema.AdditionalProperties = GenerateInternal(valueType, Required.Default, false); CurrentSchema.AdditionalProperties = GenerateInternal(valueType, Required.Default, false);
} }
@@ -421,38 +420,44 @@ namespace Newtonsoft.Json.Schema
type = Nullable.GetUnderlyingType(type); type = Nullable.GetUnderlyingType(type);
} }
TypeCode typeCode = ConvertUtils.GetTypeCode(type); PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);
switch (typeCode) switch (typeCode)
{ {
case TypeCode.Empty: case PrimitiveTypeCode.Empty:
case TypeCode.Object: case PrimitiveTypeCode.Object:
return schemaType | JsonSchemaType.String; return schemaType | JsonSchemaType.String;
#if !(NETFX_CORE || PORTABLE) #if !(NETFX_CORE || PORTABLE)
case TypeCode.DBNull: case PrimitiveTypeCode.DBNull:
return schemaType | JsonSchemaType.Null; return schemaType | JsonSchemaType.Null;
#endif #endif
case TypeCode.Boolean: case PrimitiveTypeCode.Boolean:
return schemaType | JsonSchemaType.Boolean; return schemaType | JsonSchemaType.Boolean;
case TypeCode.Char: case PrimitiveTypeCode.Char:
return schemaType | JsonSchemaType.String; return schemaType | JsonSchemaType.String;
case TypeCode.SByte: case PrimitiveTypeCode.SByte:
case TypeCode.Byte: case PrimitiveTypeCode.Byte:
case TypeCode.Int16: case PrimitiveTypeCode.Int16:
case TypeCode.UInt16: case PrimitiveTypeCode.UInt16:
case TypeCode.Int32: case PrimitiveTypeCode.Int32:
case TypeCode.UInt32: case PrimitiveTypeCode.UInt32:
case TypeCode.Int64: case PrimitiveTypeCode.Int64:
case TypeCode.UInt64: case PrimitiveTypeCode.UInt64:
case PrimitiveTypeCode.BigInteger:
return schemaType | JsonSchemaType.Integer; return schemaType | JsonSchemaType.Integer;
case TypeCode.Single: case PrimitiveTypeCode.Single:
case TypeCode.Double: case PrimitiveTypeCode.Double:
case TypeCode.Decimal: case PrimitiveTypeCode.Decimal:
return schemaType | JsonSchemaType.Float; return schemaType | JsonSchemaType.Float;
// convert to string? // convert to string?
case TypeCode.DateTime: case PrimitiveTypeCode.DateTime:
case PrimitiveTypeCode.DateTimeOffset:
return schemaType | JsonSchemaType.String; return schemaType | JsonSchemaType.String;
case TypeCode.String: case PrimitiveTypeCode.String:
case PrimitiveTypeCode.Uri:
case PrimitiveTypeCode.Guid:
case PrimitiveTypeCode.TimeSpan:
case PrimitiveTypeCode.Bytes:
return schemaType | JsonSchemaType.String; return schemaType | JsonSchemaType.String;
default: default:
throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type)); throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));
@@ -781,7 +781,7 @@ namespace Newtonsoft.Json.Serialization
{ {
Type t = ReflectionUtils.EnsureNotNullableType(objectType); Type t = ReflectionUtils.EnsureNotNullableType(objectType);
if (JsonConvert.IsJsonPrimitiveType(t)) if (JsonConvert.IsJsonPrimitiveType(objectType))
return CreatePrimitiveContract(objectType); return CreatePrimitiveContract(objectType);
if (JsonTypeReflector.GetJsonObjectAttribute(t) != null) if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
@@ -28,9 +28,6 @@ using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
#if NETFX_CORE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
namespace Newtonsoft.Json.Serialization namespace Newtonsoft.Json.Serialization
{ {
@@ -24,8 +24,8 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) #if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
using System.Globalization;
using System.Numerics; using System.Numerics;
#endif #endif
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
@@ -37,57 +37,6 @@ namespace Newtonsoft.Json.Serialization
/// </summary> /// </summary>
public class JsonPrimitiveContract : JsonContract public class JsonPrimitiveContract : JsonContract
{ {
private static readonly Dictionary<Type, PrimitiveTypeCode> TypeCodeMap =
new Dictionary<Type, PrimitiveTypeCode>
{
{ typeof(char), PrimitiveTypeCode.Char },
{ typeof(char?), PrimitiveTypeCode.CharNullable },
{ typeof(bool), PrimitiveTypeCode.Boolean },
{ typeof(bool?), PrimitiveTypeCode.BooleanNullable },
{ typeof(sbyte), PrimitiveTypeCode.SByte },
{ typeof(sbyte?), PrimitiveTypeCode.SByteNullable },
{ typeof(short), PrimitiveTypeCode.Int16 },
{ typeof(short?), PrimitiveTypeCode.Int16Nullable },
{ typeof(ushort), PrimitiveTypeCode.UInt16 },
{ typeof(ushort?), PrimitiveTypeCode.UInt16Nullable },
{ typeof(int), PrimitiveTypeCode.Int32 },
{ typeof(int?), PrimitiveTypeCode.Int32Nullable },
{ typeof(byte), PrimitiveTypeCode.Byte },
{ typeof(byte?), PrimitiveTypeCode.ByteNullable },
{ typeof(uint), PrimitiveTypeCode.UInt32 },
{ typeof(uint?), PrimitiveTypeCode.UInt32Nullable },
{ typeof(long), PrimitiveTypeCode.Int64 },
{ typeof(long?), PrimitiveTypeCode.Int64Nullable },
{ typeof(ulong), PrimitiveTypeCode.UInt64 },
{ typeof(ulong?), PrimitiveTypeCode.UInt64Nullable },
{ typeof(float), PrimitiveTypeCode.Single },
{ typeof(float?), PrimitiveTypeCode.SingleNullable },
{ typeof(double), PrimitiveTypeCode.Double },
{ typeof(double?), PrimitiveTypeCode.DoubleNullable },
{ typeof(DateTime), PrimitiveTypeCode.DateTime },
{ typeof(DateTime?), PrimitiveTypeCode.DateTimeNullable },
#if !NET20
{ typeof(DateTimeOffset), PrimitiveTypeCode.DateTimeOffset },
{ typeof(DateTimeOffset?), PrimitiveTypeCode.DateTimeOffsetNullable },
#endif
{ typeof(decimal), PrimitiveTypeCode.Decimal },
{ typeof(decimal?), PrimitiveTypeCode.DecimalNullable },
{ typeof(Guid), PrimitiveTypeCode.Guid },
{ typeof(Guid?), PrimitiveTypeCode.GuidNullable },
{ typeof(TimeSpan), PrimitiveTypeCode.TimeSpan },
{ typeof(TimeSpan?), PrimitiveTypeCode.TimeSpanNullable },
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
{ typeof(BigInteger), PrimitiveTypeCode.BigInteger },
{ typeof(BigInteger?), PrimitiveTypeCode.BigIntegerNullable },
#endif
{ typeof(Uri), PrimitiveTypeCode.Uri },
{ typeof(string), PrimitiveTypeCode.String },
{ typeof(byte[]), PrimitiveTypeCode.Bytes },
#if !(PORTABLE || NETFX_CORE)
{ typeof(DBNull), PrimitiveTypeCode.DBNull }
#endif
};
internal PrimitiveTypeCode TypeCode { get; set; } internal PrimitiveTypeCode TypeCode { get; set; }
/// <summary> /// <summary>
@@ -99,256 +48,7 @@ namespace Newtonsoft.Json.Serialization
{ {
ContractType = JsonContractType.Primitive; ContractType = JsonContractType.Primitive;
// get the underlying enum value TypeCode = ConvertUtils.GetTypeCode(underlyingType);
Type t;
if (ReflectionUtils.IsNullableType(underlyingType))
t = Nullable.GetUnderlyingType(underlyingType).IsEnum() ? ReflectionUtils.MakeGenericType(typeof(Nullable<>), Enum.GetUnderlyingType(Nullable.GetUnderlyingType(underlyingType))) : underlyingType;
else
t = underlyingType.IsEnum() ? Enum.GetUnderlyingType(underlyingType) : underlyingType;
PrimitiveTypeCode typeCode;
TypeCodeMap.TryGetValue(t, out typeCode);
TypeCode = typeCode;
} }
internal void WriteValue(JsonWriter writer, object value)
{
switch (TypeCode)
{
case PrimitiveTypeCode.Char:
writer.WriteValue((char)value);
break;
case PrimitiveTypeCode.CharNullable:
if (value != null)
writer.WriteValue((char)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Boolean:
writer.WriteValue((bool)value);
break;
case PrimitiveTypeCode.BooleanNullable:
if (value != null)
writer.WriteValue((bool)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.SByte:
writer.WriteValue((sbyte)value);
break;
case PrimitiveTypeCode.SByteNullable:
if (value != null)
writer.WriteValue((sbyte)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Int16:
writer.WriteValue((short)value);
break;
case PrimitiveTypeCode.Int16Nullable:
if (value != null)
writer.WriteValue((short)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.UInt16:
writer.WriteValue((ushort)value);
break;
case PrimitiveTypeCode.UInt16Nullable:
if (value != null)
writer.WriteValue((ushort)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Int32:
writer.WriteValue((int)value);
break;
case PrimitiveTypeCode.Int32Nullable:
if (value != null)
writer.WriteValue((int)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Byte:
writer.WriteValue((byte)value);
break;
case PrimitiveTypeCode.ByteNullable:
if (value != null)
writer.WriteValue((byte)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.UInt32:
writer.WriteValue((uint)value);
break;
case PrimitiveTypeCode.UInt32Nullable:
if (value != null)
writer.WriteValue((uint)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Int64:
writer.WriteValue((long)value);
break;
case PrimitiveTypeCode.Int64Nullable:
if (value != null)
writer.WriteValue((long)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.UInt64:
writer.WriteValue((ulong)value);
break;
case PrimitiveTypeCode.UInt64Nullable:
if (value != null)
writer.WriteValue((ulong)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Single:
writer.WriteValue((float)value);
break;
case PrimitiveTypeCode.SingleNullable:
// use nullable WriteValue to handle NaN, Infinity with FloatFormatHandling
if (value != null)
writer.WriteValue((float?)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Double:
writer.WriteValue((double)value);
break;
case PrimitiveTypeCode.DoubleNullable:
// use nullable WriteValue to handle NaN, Infinity with FloatFormatHandling
if (value != null)
writer.WriteValue((double?)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.DateTime:
writer.WriteValue((DateTime)value);
break;
case PrimitiveTypeCode.DateTimeNullable:
if (value != null)
writer.WriteValue((DateTime)value);
else
writer.WriteNull();
break;
#if !NET20
case PrimitiveTypeCode.DateTimeOffset:
writer.WriteValue((DateTimeOffset)value);
break;
case PrimitiveTypeCode.DateTimeOffsetNullable:
if (value != null)
writer.WriteValue((DateTimeOffset)value);
else
writer.WriteNull();
break;
#endif
case PrimitiveTypeCode.Decimal:
writer.WriteValue((decimal)value);
break;
case PrimitiveTypeCode.DecimalNullable:
if (value != null)
writer.WriteValue((decimal)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.Guid:
writer.WriteValue((Guid)value);
break;
case PrimitiveTypeCode.GuidNullable:
if (value != null)
writer.WriteValue((Guid)value);
else
writer.WriteNull();
break;
case PrimitiveTypeCode.TimeSpan:
writer.WriteValue((TimeSpan)value);
break;
case PrimitiveTypeCode.TimeSpanNullable:
if (value != null)
writer.WriteValue((TimeSpan)value);
else
writer.WriteNull();
break;
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
case PrimitiveTypeCode.BigInteger:
writer.WriteValue((BigInteger)value);
break;
case PrimitiveTypeCode.BigIntegerNullable:
if (value != null)
writer.WriteValue((BigInteger)value);
else
writer.WriteNull();
break;
#endif
case PrimitiveTypeCode.Uri:
writer.WriteValue((Uri)value);
break;
case PrimitiveTypeCode.String:
writer.WriteValue((string)value);
break;
case PrimitiveTypeCode.Bytes:
writer.WriteValue((byte[])value);
break;
#if !(PORTABLE || NETFX_CORE)
case PrimitiveTypeCode.DBNull:
writer.WriteNull();
break;
#endif
default:
throw new ArgumentOutOfRangeException("Could not write type: " + UnderlyingType);
}
}
}
internal enum PrimitiveTypeCode
{
Unknown,
Char,
CharNullable,
Boolean,
BooleanNullable,
SByte,
SByteNullable,
Int16,
Int16Nullable,
UInt16,
UInt16Nullable,
Int32,
Int32Nullable,
Byte,
ByteNullable,
UInt32,
UInt32Nullable,
Int64,
Int64Nullable,
UInt64,
UInt64Nullable,
Single,
SingleNullable,
Double,
DoubleNullable,
DateTime,
DateTimeNullable,
#if !NET20
DateTimeOffset,
DateTimeOffsetNullable,
#endif
Decimal,
DecimalNullable,
Guid,
GuidNullable,
TimeSpan,
TimeSpanNullable,
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
BigInteger,
BigIntegerNullable,
#endif
Uri,
String,
Bytes,
DBNull
} }
} }
@@ -693,11 +693,13 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (contract.IsConvertable) if (contract.IsConvertable)
{ {
JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract;
if (contract.NonNullableUnderlyingType.IsEnum()) if (contract.NonNullableUnderlyingType.IsEnum())
{ {
if (value is string) if (value is string)
return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true); return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true);
else if (ConvertUtils.IsInteger(value)) else if (ConvertUtils.IsInteger(primitiveContract.TypeCode))
return Enum.ToObject(contract.NonNullableUnderlyingType, value); return Enum.ToObject(contract.NonNullableUnderlyingType, value);
} }
@@ -706,6 +708,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
return ConvertUtils.FromBigInteger((BigInteger)value, targetType); return ConvertUtils.FromBigInteger((BigInteger)value, targetType);
#endif #endif
// this won't work when converting to a custom IConvertible
return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture); return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture);
} }
@@ -966,6 +969,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
contract.ItemContract = GetContractSafe(contract.DictionaryValueType); contract.ItemContract = GetContractSafe(contract.DictionaryValueType);
JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty); JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty);
PrimitiveTypeCode keyTypeCode = (contract.KeyContract is JsonPrimitiveContract) ? ((JsonPrimitiveContract)contract.KeyContract).TypeCode : PrimitiveTypeCode.Empty;
bool finished = false; bool finished = false;
do do
@@ -979,11 +983,13 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
try try
{ {
// this is for correctly reading ISO and MS formatted dictionary keys // this is for correctly reading ISO and MS formatted dictionary keys
if (contract.DictionaryKeyType == typeof(DateTime) && JsonConvert.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTime, reader.DateTimeZoneHandling, out keyValue)) if ((keyTypeCode == PrimitiveTypeCode.DateTime || keyTypeCode == PrimitiveTypeCode.DateTimeNullable)
&& JsonConvert.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTime, reader.DateTimeZoneHandling, out keyValue))
{ {
} }
#if !NET20 #if !NET20
else if (contract.DictionaryKeyType == typeof(DateTimeOffset) && JsonConvert.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTimeOffset, reader.DateTimeZoneHandling, out keyValue)) else if ((keyTypeCode == PrimitiveTypeCode.DateTimeOffset || keyTypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
&& JsonConvert.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTimeOffset, reader.DateTimeZoneHandling, out keyValue))
{ {
} }
#endif #endif
@@ -94,14 +94,14 @@ namespace Newtonsoft.Json.Serialization
WriteTypeProperty(writer, contract.CreatedType); WriteTypeProperty(writer, contract.CreatedType);
writer.WritePropertyName(JsonTypeReflector.ValuePropertyName); writer.WritePropertyName(JsonTypeReflector.ValuePropertyName);
contract.WriteValue(writer, value); JsonWriter.WriteValue(writer, contract.TypeCode, value);
writer.WriteEndObject(); writer.WriteEndObject();
return; return;
} }
} }
contract.WriteValue(writer, value); JsonWriter.WriteValue(writer, contract.TypeCode, value);
} }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
@@ -827,12 +827,15 @@ To fix this error either change the environment to be fully trusted, change the
if (contract.ItemContract == null) if (contract.ItemContract == null)
contract.ItemContract = Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object)); contract.ItemContract = Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));
if (contract.KeyContract == null)
contract.KeyContract = Serializer.ContractResolver.ResolveContract(contract.DictionaryKeyType ?? typeof(object));
int initialDepth = writer.Top; int initialDepth = writer.Top;
foreach (DictionaryEntry entry in values) foreach (DictionaryEntry entry in values)
{ {
bool escape; bool escape;
string propertyName = GetPropertyName(writer, entry, out escape); string propertyName = GetPropertyName(writer, entry, contract.KeyContract, out escape);
propertyName = (contract.PropertyNameResolver != null) propertyName = (contract.PropertyNameResolver != null)
? contract.PropertyNameResolver(propertyName) ? contract.PropertyNameResolver(propertyName)
@@ -874,13 +877,16 @@ To fix this error either change the environment to be fully trusted, change the
OnSerialized(writer, contract, underlyingDictionary); OnSerialized(writer, contract, underlyingDictionary);
} }
private string GetPropertyName(JsonWriter writer, DictionaryEntry entry, out bool escape) private string GetPropertyName(JsonWriter writer, DictionaryEntry entry, JsonContract contract, out bool escape)
{ {
object key = entry.Key; object key = entry.Key;
string propertyName; string propertyName;
if (key is DateTime) JsonPrimitiveContract primitiveContract = contract as JsonPrimitiveContract;
if (primitiveContract != null)
{
if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTime || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeNullable)
{ {
escape = false; escape = false;
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture); StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
@@ -888,7 +894,7 @@ To fix this error either change the environment to be fully trusted, change the
return sw.ToString(); return sw.ToString();
} }
#if !NET20 #if !NET20
else if (key is DateTimeOffset) else if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffset || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
{ {
escape = false; escape = false;
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture); StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
@@ -896,11 +902,12 @@ To fix this error either change the environment to be fully trusted, change the
return sw.ToString(); return sw.ToString();
} }
#endif #endif
else if (ConvertUtils.IsConvertible(key)) else
{ {
escape = true; escape = true;
return Convert.ToString(key, CultureInfo.InvariantCulture); return Convert.ToString(key, CultureInfo.InvariantCulture);
} }
}
else if (TryConvertToString(key, key.GetType(), out propertyName)) else if (TryConvertToString(key, key.GetType(), out propertyName))
{ {
escape = true; escape = true;
+169 -168
View File
@@ -24,6 +24,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.ComponentModel; using System.ComponentModel;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
@@ -38,181 +39,176 @@ using Newtonsoft.Json.Utilities.LinqBridge;
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
using System.Data.SqlTypes; using System.Data.SqlTypes;
#endif #endif
#if NETFX_CORE || PORTABLE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
namespace Newtonsoft.Json.Utilities namespace Newtonsoft.Json.Utilities
{ {
#if NETFX_CORE || PORTABLE internal enum PrimitiveTypeCode
internal class Convertible
{ {
private object _underlyingValue; Empty,
Object,
public Convertible(object o) Char,
{ CharNullable,
_underlyingValue = o; Boolean,
} BooleanNullable,
SByte,
public TypeCode GetTypeCode() SByteNullable,
{ Int16,
return ConvertUtils.GetTypeCode(_underlyingValue); Int16Nullable,
} UInt16,
UInt16Nullable,
public bool ToBoolean(IFormatProvider provider) Int32,
{ Int32Nullable,
return Convert.ToBoolean(_underlyingValue, provider); Byte,
} ByteNullable,
public byte ToByte(IFormatProvider provider) UInt32,
{ UInt32Nullable,
return Convert.ToByte(_underlyingValue, provider); Int64,
} Int64Nullable,
public char ToChar(IFormatProvider provider) UInt64,
{ UInt64Nullable,
return Convert.ToChar(_underlyingValue, provider); Single,
} SingleNullable,
public DateTime ToDateTime(IFormatProvider provider) Double,
{ DoubleNullable,
return Convert.ToDateTime(_underlyingValue, provider); DateTime,
} DateTimeNullable,
public decimal ToDecimal(IFormatProvider provider) #if !NET20
{ DateTimeOffset,
return Convert.ToDecimal(_underlyingValue, provider); DateTimeOffsetNullable,
}
public double ToDouble(IFormatProvider provider)
{
return Convert.ToDouble(_underlyingValue, provider);
}
public short ToInt16(IFormatProvider provider)
{
return Convert.ToInt16(_underlyingValue, provider);
}
public int ToInt32(IFormatProvider provider)
{
return Convert.ToInt32(_underlyingValue, provider);
}
public long ToInt64(IFormatProvider provider)
{
return Convert.ToInt64(_underlyingValue, provider);
}
public sbyte ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(_underlyingValue, provider);
}
public float ToSingle(IFormatProvider provider)
{
return Convert.ToSingle(_underlyingValue, provider);
}
public string ToString(IFormatProvider provider)
{
return Convert.ToString(_underlyingValue, provider);
}
public object ToType(Type conversionType, IFormatProvider provider)
{
return Convert.ChangeType(_underlyingValue, conversionType, provider);
}
public ushort ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(_underlyingValue, provider);
}
public uint ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(_underlyingValue, provider);
}
public ulong ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(_underlyingValue, provider);
}
}
#endif #endif
Decimal,
DecimalNullable,
Guid,
GuidNullable,
TimeSpan,
TimeSpanNullable,
#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
BigInteger,
BigIntegerNullable,
#endif
Uri,
String,
Bytes,
DBNull
}
internal class TypeInformation
{
public Type Type { get; set; }
public PrimitiveTypeCode TypeCode { get; set; }
}
internal static class ConvertUtils internal static class ConvertUtils
{ {
public static TypeCode GetTypeCode(this IConvertible convertible) private static readonly Dictionary<Type, PrimitiveTypeCode> TypeCodeMap =
new Dictionary<Type, PrimitiveTypeCode>
{ {
#if !(NETFX_CORE || PORTABLE) { typeof(char), PrimitiveTypeCode.Char },
return convertible.GetTypeCode(); { typeof(char?), PrimitiveTypeCode.CharNullable },
#else { typeof(bool), PrimitiveTypeCode.Boolean },
return GetTypeCode((object)convertible); { typeof(bool?), PrimitiveTypeCode.BooleanNullable },
{ typeof(sbyte), PrimitiveTypeCode.SByte },
{ typeof(sbyte?), PrimitiveTypeCode.SByteNullable },
{ typeof(short), PrimitiveTypeCode.Int16 },
{ typeof(short?), PrimitiveTypeCode.Int16Nullable },
{ typeof(ushort), PrimitiveTypeCode.UInt16 },
{ typeof(ushort?), PrimitiveTypeCode.UInt16Nullable },
{ typeof(int), PrimitiveTypeCode.Int32 },
{ typeof(int?), PrimitiveTypeCode.Int32Nullable },
{ typeof(byte), PrimitiveTypeCode.Byte },
{ typeof(byte?), PrimitiveTypeCode.ByteNullable },
{ typeof(uint), PrimitiveTypeCode.UInt32 },
{ typeof(uint?), PrimitiveTypeCode.UInt32Nullable },
{ typeof(long), PrimitiveTypeCode.Int64 },
{ typeof(long?), PrimitiveTypeCode.Int64Nullable },
{ typeof(ulong), PrimitiveTypeCode.UInt64 },
{ typeof(ulong?), PrimitiveTypeCode.UInt64Nullable },
{ typeof(float), PrimitiveTypeCode.Single },
{ typeof(float?), PrimitiveTypeCode.SingleNullable },
{ typeof(double), PrimitiveTypeCode.Double },
{ typeof(double?), PrimitiveTypeCode.DoubleNullable },
{ typeof(DateTime), PrimitiveTypeCode.DateTime },
{ typeof(DateTime?), PrimitiveTypeCode.DateTimeNullable },
#if !NET20
{ typeof(DateTimeOffset), PrimitiveTypeCode.DateTimeOffset },
{ typeof(DateTimeOffset?), PrimitiveTypeCode.DateTimeOffsetNullable },
#endif #endif
} { typeof(decimal), PrimitiveTypeCode.Decimal },
{ typeof(decimal?), PrimitiveTypeCode.DecimalNullable },
public static TypeCode GetTypeCode(object o) { typeof(Guid), PrimitiveTypeCode.Guid },
{ { typeof(Guid?), PrimitiveTypeCode.GuidNullable },
#if !(NETFX_CORE || PORTABLE) { typeof(TimeSpan), PrimitiveTypeCode.TimeSpan },
return System.Convert.GetTypeCode(o); { typeof(TimeSpan?), PrimitiveTypeCode.TimeSpanNullable },
#else #if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
return GetTypeCode(o.GetType()); { typeof(BigInteger), PrimitiveTypeCode.BigInteger },
{ typeof(BigInteger?), PrimitiveTypeCode.BigIntegerNullable },
#endif #endif
} { typeof(Uri), PrimitiveTypeCode.Uri },
{ typeof(string), PrimitiveTypeCode.String },
{ typeof(byte[]), PrimitiveTypeCode.Bytes },
#if !(PORTABLE || NETFX_CORE)
{ typeof(DBNull), PrimitiveTypeCode.DBNull }
#endif
};
public static TypeCode GetTypeCode(Type t) private static readonly List<TypeInformation> PrimitiveTypeCodes = new List<TypeInformation>
{ {
#if !(NETFX_CORE || PORTABLE) new TypeInformation { Type = typeof(object), TypeCode = PrimitiveTypeCode.Empty },
return Type.GetTypeCode(t); new TypeInformation { Type = typeof(object), TypeCode = PrimitiveTypeCode.Object },
#else new TypeInformation { Type = typeof(object), TypeCode = PrimitiveTypeCode.DBNull },
if (t == typeof(bool)) new TypeInformation { Type = typeof(bool), TypeCode = PrimitiveTypeCode.Boolean },
return TypeCode.Boolean; new TypeInformation { Type = typeof(char), TypeCode = PrimitiveTypeCode.Char },
if (t == typeof(byte)) new TypeInformation { Type = typeof(sbyte), TypeCode = PrimitiveTypeCode.SByte },
return TypeCode.Byte; new TypeInformation { Type = typeof(byte), TypeCode = PrimitiveTypeCode.Byte },
if (t == typeof(char)) new TypeInformation { Type = typeof(short), TypeCode = PrimitiveTypeCode.Int16 },
return TypeCode.Char; new TypeInformation { Type = typeof(ushort), TypeCode = PrimitiveTypeCode.UInt16 },
if (t == typeof(DateTime)) new TypeInformation { Type = typeof(int), TypeCode = PrimitiveTypeCode.Int32 },
return TypeCode.DateTime; new TypeInformation { Type = typeof(uint), TypeCode = PrimitiveTypeCode.UInt32 },
if (t == typeof(decimal)) new TypeInformation { Type = typeof(long), TypeCode = PrimitiveTypeCode.Int64 },
return TypeCode.Decimal; new TypeInformation { Type = typeof(ulong), TypeCode = PrimitiveTypeCode.UInt64 },
if (t == typeof(double)) new TypeInformation { Type = typeof(float), TypeCode = PrimitiveTypeCode.Single },
return TypeCode.Double; new TypeInformation { Type = typeof(double), TypeCode = PrimitiveTypeCode.Double },
if (t == typeof(short)) new TypeInformation { Type = typeof(decimal), TypeCode = PrimitiveTypeCode.Decimal },
return TypeCode.Int16; new TypeInformation { Type = typeof(DateTime), TypeCode = PrimitiveTypeCode.DateTime },
if (t == typeof(int)) new TypeInformation { Type = typeof(object), TypeCode = PrimitiveTypeCode.Empty }, // no 17 in TypeCode for some reason
return TypeCode.Int32; new TypeInformation { Type = typeof(string), TypeCode = PrimitiveTypeCode.String }
if (t == typeof(long)) };
return TypeCode.Int64;
if (t == typeof(sbyte)) public static PrimitiveTypeCode GetTypeCode(Type t)
return TypeCode.SByte; {
if (t == typeof(float)) PrimitiveTypeCode typeCode;
return TypeCode.Single; if (TypeCodeMap.TryGetValue(t, out typeCode))
if (t == typeof(string)) return typeCode;
return TypeCode.String;
if (t == typeof(ushort))
return TypeCode.UInt16;
if (t == typeof(uint))
return TypeCode.UInt32;
if (t == typeof(ulong))
return TypeCode.UInt64;
if (t.IsEnum()) if (t.IsEnum())
return GetTypeCode(Enum.GetUnderlyingType(t)); return GetTypeCode(Enum.GetUnderlyingType(t));
return TypeCode.Object; // performance?
#endif if (ReflectionUtils.IsNullableType(t))
}
public static IConvertible ToConvertible(object o)
{ {
#if !(NETFX_CORE || PORTABLE) Type nonNullable = Nullable.GetUnderlyingType(t);
return o as IConvertible; if (nonNullable.IsEnum())
#else
if (!IsConvertible(o))
return null;
return new IConvertible(o);
#endif
}
public static bool IsConvertible(object o)
{ {
#if !(NETFX_CORE || PORTABLE) Type nullableUnderlyingType = ReflectionUtils.MakeGenericType(typeof (Nullable<>), Enum.GetUnderlyingType(nonNullable));
return o is IConvertible; return GetTypeCode(nullableUnderlyingType);
#else
if (o == null)
return false;
return (
o is bool || o is byte || o is char || o is DateTime || o is decimal || o is double || o is short || o is int ||
o is long || o is sbyte || o is float || o is string || o is ushort || o is uint || o is ulong || o is Enum);
#endif
} }
}
return PrimitiveTypeCode.Object;
}
public static PrimitiveTypeCode GetTypeCode(object o)
{
return GetTypeCode(o.GetType());
}
#if !(NETFX_CORE || PORTABLE)
public static TypeInformation GetTypeInformation(IConvertible convertable)
{
TypeInformation typeInformation = PrimitiveTypeCodes[(int) convertable.GetTypeCode()];
return typeInformation;
}
#endif
public static bool IsConvertible(Type t) public static bool IsConvertible(Type t)
{ {
@@ -328,10 +324,15 @@ namespace Newtonsoft.Json.Utilities
return (float) i; return (float) i;
if (targetType == typeof (ulong)) if (targetType == typeof (ulong))
return (ulong) i; return (ulong) i;
if (IsConvertible(targetType))
return System.Convert.ChangeType((long) i, targetType, CultureInfo.InvariantCulture);
throw new InvalidOperationException("Can not convert from BigInteger to {0}.".FormatWith(CultureInfo.InvariantCulture, targetType)); try
{
return System.Convert.ChangeType((long)i, targetType, CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
throw new InvalidOperationException("Can not convert from BigInteger to {0}.".FormatWith(CultureInfo.InvariantCulture, targetType), ex);
}
} }
#endif #endif
@@ -357,7 +358,7 @@ namespace Newtonsoft.Json.Utilities
return initialValue; return initialValue;
// use Convert.ChangeType if both types are IConvertible // use Convert.ChangeType if both types are IConvertible
if (ConvertUtils.IsConvertible(initialValue) && ConvertUtils.IsConvertible(targetType)) if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
{ {
if (targetType.IsEnum()) if (targetType.IsEnum())
{ {
@@ -550,14 +551,14 @@ namespace Newtonsoft.Json.Utilities
{ {
switch (GetTypeCode(value)) switch (GetTypeCode(value))
{ {
case TypeCode.SByte: case PrimitiveTypeCode.SByte:
case TypeCode.Byte: case PrimitiveTypeCode.Byte:
case TypeCode.Int16: case PrimitiveTypeCode.Int16:
case TypeCode.UInt16: case PrimitiveTypeCode.UInt16:
case TypeCode.Int32: case PrimitiveTypeCode.Int32:
case TypeCode.UInt32: case PrimitiveTypeCode.UInt32:
case TypeCode.Int64: case PrimitiveTypeCode.Int64:
case TypeCode.UInt64: case PrimitiveTypeCode.UInt64:
return true; return true;
default: default:
return false; return false;
@@ -25,15 +25,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics;
using System.Reflection; using System.Reflection;
using System.Collections; using System.Collections;
using System.Globalization; using System.Globalization;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters;
using System.Text; using System.Text;
#if NETFX_CORE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
#if NETFX_CORE || PORTABLE #if NETFX_CORE || PORTABLE
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider; using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
#endif #endif
@@ -70,29 +68,6 @@ namespace Newtonsoft.Json.Utilities
get { return _underlyingObject; } get { return _underlyingObject; }
} }
} }
#endif
#if (NETFX_CORE || PORTABLE)
internal enum TypeCode
{
Empty,
Object,
String,
Char,
Boolean,
SByte,
Int16,
UInt16,
Int32,
Byte,
UInt32,
Int64,
UInt64,
Single,
Double,
DateTime,
Decimal
}
[Flags] [Flags]
internal enum BindingFlags internal enum BindingFlags
@@ -985,36 +960,34 @@ namespace Newtonsoft.Json.Utilities
switch (ConvertUtils.GetTypeCode(type)) switch (ConvertUtils.GetTypeCode(type))
{ {
case TypeCode.Boolean: case PrimitiveTypeCode.Boolean:
return false; return false;
case TypeCode.Char: case PrimitiveTypeCode.Char:
case TypeCode.SByte: case PrimitiveTypeCode.SByte:
case TypeCode.Byte: case PrimitiveTypeCode.Byte:
case TypeCode.Int16: case PrimitiveTypeCode.Int16:
case TypeCode.UInt16: case PrimitiveTypeCode.UInt16:
case TypeCode.Int32: case PrimitiveTypeCode.Int32:
case TypeCode.UInt32: case PrimitiveTypeCode.UInt32:
return 0; return 0;
case TypeCode.Int64: case PrimitiveTypeCode.Int64:
case TypeCode.UInt64: case PrimitiveTypeCode.UInt64:
return 0L; return 0L;
case TypeCode.Single: case PrimitiveTypeCode.Single:
return 0f; return 0f;
case TypeCode.Double: case PrimitiveTypeCode.Double:
return 0.0; return 0.0;
case TypeCode.Decimal: case PrimitiveTypeCode.Decimal:
return 0m; return 0m;
case TypeCode.DateTime: case PrimitiveTypeCode.DateTime:
return new DateTime(); return new DateTime();
} case PrimitiveTypeCode.BigInteger:
return new BigInteger();
if (type == typeof(Guid)) case PrimitiveTypeCode.Guid:
return new Guid(); return new Guid();
case PrimitiveTypeCode.DateTimeOffset:
#if !NET20
if (type == typeof(DateTimeOffset))
return new DateTimeOffset(); return new DateTimeOffset();
#endif }
if (IsNullable(type)) if (IsNullable(type))
return null; return null;