-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
@@ -30,9 +30,6 @@ using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Serialization;
#if NETFX_CORE
using IConvertible = Newtonsoft.Json.Utilities.Convertible;
#endif
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
@@ -321,8 +318,10 @@ namespace Newtonsoft.Json.Schema
if (keyType != null)
{
JsonContract keyContract = ContractResolver.ResolveContract(keyType);
// can be converted to a string
if (ConvertUtils.IsConvertible(keyType))
if (keyContract.ContractType == JsonContractType.Primitive)
{
CurrentSchema.AdditionalProperties = GenerateInternal(valueType, Required.Default, false);
}
@@ -421,38 +420,44 @@ namespace Newtonsoft.Json.Schema
type = Nullable.GetUnderlyingType(type);
}
TypeCode typeCode = ConvertUtils.GetTypeCode(type);
PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);
switch (typeCode)
{
case TypeCode.Empty:
case TypeCode.Object:
case PrimitiveTypeCode.Empty:
case PrimitiveTypeCode.Object:
return schemaType | JsonSchemaType.String;
#if !(NETFX_CORE || PORTABLE)
case TypeCode.DBNull:
case PrimitiveTypeCode.DBNull:
return schemaType | JsonSchemaType.Null;
#endif
case TypeCode.Boolean:
case PrimitiveTypeCode.Boolean:
return schemaType | JsonSchemaType.Boolean;
case TypeCode.Char:
case PrimitiveTypeCode.Char:
return schemaType | JsonSchemaType.String;
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case PrimitiveTypeCode.SByte:
case PrimitiveTypeCode.Byte:
case PrimitiveTypeCode.Int16:
case PrimitiveTypeCode.UInt16:
case PrimitiveTypeCode.Int32:
case PrimitiveTypeCode.UInt32:
case PrimitiveTypeCode.Int64:
case PrimitiveTypeCode.UInt64:
case PrimitiveTypeCode.BigInteger:
return schemaType | JsonSchemaType.Integer;
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
case PrimitiveTypeCode.Single:
case PrimitiveTypeCode.Double:
case PrimitiveTypeCode.Decimal:
return schemaType | JsonSchemaType.Float;
// convert to string?
case TypeCode.DateTime:
case PrimitiveTypeCode.DateTime:
case PrimitiveTypeCode.DateTimeOffset:
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;
default:
throw new JsonException("Unexpected type code '{0}' for type '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeCode, type));