Skip to content

Commit

Permalink
Removed need for Nullable converters in Json serialization (dotnet/co…
Browse files Browse the repository at this point in the history
…refx#35754)

Added support for Nullable<T> properties where a Nullable converter is no longer required.
Enum converter has generic constraint to `Enum` type.

Commit migrated from dotnet/corefx@b0751dc
  • Loading branch information
TylerBrinkley authored and steveharter committed Mar 20, 2019
1 parent c1f6d75 commit f094cb0
Show file tree
Hide file tree
Showing 27 changed files with 387 additions and 806 deletions.
24 changes: 6 additions & 18 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,30 @@
<Compile Include="System\Text\Json\Serialization\ClassType.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultArrayConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultConverters.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\DefaultConvertersOfTValue.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterBoolean.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterBooleanNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterByte.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterByteNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterChar.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterCharNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDateTime.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDateTimeNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDecimal.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDecimalNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDouble.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDoubleNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInfoInt16.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInt16Nullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterEnum.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInt16.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInt32.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInt32Nullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInt64.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterInt64Nullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterSByte.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterSByteNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterSingle.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterSingleNullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterString.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt16.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt16Nullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt32.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt32Nullable.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt64.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterUInt64Nullable.cs" />
<Compile Include="System\Text\Json\Serialization\JsonClassInfo.cs" />
<Compile Include="System\Text\Json\Serialization\JsonClassInfo.AddProperty.cs" />
<Compile Include="System\Text\Json\Serialization\JsonEnumerableConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPropertyInfo.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPropertyInfoOfTClassTProperty.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPropertyInfoOfTProperty.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPropertyInfoNotNullable.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPropertyInfoNullable.cs" />
<Compile Include="System\Text\Json\Serialization\JsonPropertyInfoOfTClassTPropertyTUnderlying.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializer.Read.HandleArray.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializer.Read.Stream.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializer.Read.HandleValue.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,58 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Reflection;

namespace System.Text.Json.Serialization.Converters
{
internal static class DefaultConverters
{
private const int MaxTypeCode = 18;

private static readonly object[] s_Converters = new object[MaxTypeCode + 1] {
null, // Empty
null, // Object
null, // DBNull
new JsonValueConverterBoolean(),
new JsonValueConverterChar(),
new JsonValueConverterSByte(),
new JsonValueConverterByte(),
new JsonValueConverterInt16(),
new JsonValueConverterUInt16(),
new JsonValueConverterInt32(),
new JsonValueConverterUInt32(),
new JsonValueConverterInt64(),
new JsonValueConverterUInt64(),
new JsonValueConverterSingle(),
new JsonValueConverterDouble(),
new JsonValueConverterDecimal(),
new JsonValueConverterDateTime(),
null, // (not a value)
new JsonValueConverterString()
};

private static readonly object[] s_NullableConverters = new object[MaxTypeCode + 1]
{
null,
null,
null,
new JsonValueConverterBooleanNullable(),
new JsonValueConverterCharNullable(),
new JsonValueConverterSByteNullable(),
new JsonValueConverterByteNullable(),
new JsonValueConverterInt16Nullable(),
new JsonValueConverterUInt16Nullable(),
new JsonValueConverterInt32Nullable(),
new JsonValueConverterUInt32Nullable(),
new JsonValueConverterInt64Nullable(),
new JsonValueConverterUInt64Nullable(),
new JsonValueConverterSingleNullable(),
new JsonValueConverterDoubleNullable(),
new JsonValueConverterDecimalNullable(),
new JsonValueConverterDateTimeNullable(),
null,
new JsonValueConverterString()
};

internal static object GetDefaultPropertyValueConverter(Type propertyType, bool isNullable)
internal static object Create(Type type)
{
object converter = null;

int typeCode = (int)Type.GetTypeCode(propertyType);
if (typeCode <= MaxTypeCode)
if (type.IsEnum)
{
if (isNullable)
{
converter = s_NullableConverters[typeCode];
}
else
{
converter = s_Converters[typeCode];
}
return Activator.CreateInstance(
typeof(JsonValueConverterEnum<>).MakeGenericType(type),
BindingFlags.Instance | BindingFlags.NonPublic,
binder: null,
new object[] { false },
culture: null);
}

return converter;
TypeCode typeCode = Type.GetTypeCode(type);
switch (typeCode)
{
case TypeCode.Boolean:
return new JsonValueConverterBoolean();
case TypeCode.Char:
return new JsonValueConverterChar();
case TypeCode.SByte:
return new JsonValueConverterSByte();
case TypeCode.Byte:
return new JsonValueConverterByte();
case TypeCode.Int16:
return new JsonValueConverterInt16();
case TypeCode.UInt16:
return new JsonValueConverterUInt16();
case TypeCode.Int32:
return new JsonValueConverterInt32();
case TypeCode.UInt32:
return new JsonValueConverterUInt32();
case TypeCode.Int64:
return new JsonValueConverterInt64();
case TypeCode.UInt64:
return new JsonValueConverterUInt64();
case TypeCode.Single:
return new JsonValueConverterSingle();
case TypeCode.Double:
return new JsonValueConverterDouble();
case TypeCode.Decimal:
return new JsonValueConverterDecimal();
case TypeCode.DateTime:
return new JsonValueConverterDateTime();
case TypeCode.String:
return new JsonValueConverterString();
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json.Serialization.Policies;

namespace System.Text.Json.Serialization.Converters
{
internal static class DefaultConverters<TValue>
{
internal static readonly JsonValueConverter<TValue> s_converter = (JsonValueConverter<TValue>)DefaultConverters.Create(typeof(TValue));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit f094cb0

Please sign in to comment.