-Refactor
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
@@ -746,12 +747,12 @@ namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
|
||||
jsonWriter.WriteValue(int.MaxValue);
|
||||
jsonWriter.WriteValue(int.MinValue);
|
||||
jsonWriter.WriteValue(0);
|
||||
jsonWriter.WriteValue(-0);
|
||||
jsonWriter.WriteValue(9L);
|
||||
jsonWriter.WriteValue(9UL);
|
||||
jsonWriter.WriteValue(int.MaxValue);
|
||||
jsonWriter.WriteValue(int.MinValue);
|
||||
jsonWriter.WriteValue(long.MaxValue);
|
||||
jsonWriter.WriteValue(long.MinValue);
|
||||
jsonWriter.WriteValue(ulong.MaxValue);
|
||||
@@ -763,12 +764,12 @@ namespace Newtonsoft.Json.Tests
|
||||
Console.WriteLine(sb.ToString());
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
0,
|
||||
0,
|
||||
9,
|
||||
9,
|
||||
2147483647,
|
||||
-2147483648,
|
||||
0,
|
||||
0,
|
||||
9,
|
||||
9,
|
||||
9223372036854775807,
|
||||
-9223372036854775808,
|
||||
18446744073709551615,
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
BsonArray value = (BsonArray)t;
|
||||
_writer.Write(value.CalculatedSize);
|
||||
int index = 0;
|
||||
ulong index = 0;
|
||||
foreach (BsonToken c in value)
|
||||
{
|
||||
_writer.Write((sbyte)c.Type);
|
||||
@@ -261,7 +261,7 @@ namespace Newtonsoft.Json.Bson
|
||||
BsonArray value = (BsonArray)t;
|
||||
|
||||
int size = 4;
|
||||
int index = 0;
|
||||
ulong index = 0;
|
||||
foreach (BsonToken c in value)
|
||||
{
|
||||
size += 1;
|
||||
|
||||
@@ -686,61 +686,22 @@ namespace Newtonsoft.Json
|
||||
_writeBuffer = new char[64];
|
||||
}
|
||||
|
||||
private void WriteIntegerValue(int value)
|
||||
{
|
||||
EnsureWriteBuffer();
|
||||
|
||||
if (value >= 0 && value <= 9)
|
||||
{
|
||||
_writer.Write((char)(48 + value));
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
uint uvalue = (value < 0) ? (uint)-value : (uint)value;
|
||||
|
||||
do
|
||||
{
|
||||
_writeBuffer[length++] = (char)(48 + (uvalue % 10));
|
||||
uvalue /= 10;
|
||||
} while (uvalue != 0);
|
||||
|
||||
if (value < 0)
|
||||
_writeBuffer[length++] = '-';
|
||||
|
||||
Array.Reverse(_writeBuffer, 0, length);
|
||||
|
||||
_writer.Write(_writeBuffer, 0, length);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteIntegerValue(long value)
|
||||
{
|
||||
EnsureWriteBuffer();
|
||||
|
||||
if (value >= 0 && value <= 9)
|
||||
{
|
||||
_writer.Write((char)(48 + value));
|
||||
_writer.Write((char)('0' + value));
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
ulong uvalue = (value < 0) ? (ulong)-value : (ulong)value;
|
||||
|
||||
do
|
||||
{
|
||||
_writeBuffer[length++] = (char)(48 + (uvalue % 10));
|
||||
uvalue /= 10;
|
||||
} while (uvalue != 0);
|
||||
|
||||
if (value < 0)
|
||||
_writeBuffer[length++] = '-';
|
||||
_writer.Write('-');
|
||||
|
||||
Array.Reverse(_writeBuffer, 0, length);
|
||||
|
||||
_writer.Write(_writeBuffer, 0, length);
|
||||
WriteIntegerValue(uvalue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -750,20 +711,19 @@ namespace Newtonsoft.Json
|
||||
|
||||
if (uvalue <= 9)
|
||||
{
|
||||
_writer.Write((char)(48 + uvalue));
|
||||
_writer.Write((char)('0' + uvalue));
|
||||
}
|
||||
else
|
||||
{
|
||||
int totalLength = MathUtils.IntLength(uvalue);
|
||||
int length = 0;
|
||||
|
||||
do
|
||||
{
|
||||
_writeBuffer[length++] = (char)(48 + (uvalue % 10));
|
||||
_writeBuffer[totalLength - ++length] = (char)('0' + (uvalue % 10));
|
||||
uvalue /= 10;
|
||||
} while (uvalue != 0);
|
||||
|
||||
Array.Reverse(_writeBuffer, 0, length);
|
||||
|
||||
_writer.Write(_writeBuffer, 0, length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
internal ReadType InternalReadType;
|
||||
internal JsonContractType ContractType;
|
||||
internal bool IsReadOnlyOrFixedSize;
|
||||
internal bool IsInstantiable;
|
||||
|
||||
private List<SerializationCallback> _onDeserializedCallbacks;
|
||||
private IList<SerializationCallback> _onDeserializingCallbacks;
|
||||
@@ -280,6 +281,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
UnderlyingType = underlyingType;
|
||||
|
||||
IsSealed = underlyingType.IsSealed();
|
||||
IsInstantiable = !(underlyingType.IsInterface() || underlyingType.IsAbstract());
|
||||
|
||||
IsNullable = ReflectionUtils.IsNullable(underlyingType);
|
||||
NonNullableUnderlyingType = (IsNullable && ReflectionUtils.IsNullableType(underlyingType)) ? Nullable.GetUnderlyingType(underlyingType) : underlyingType;
|
||||
|
||||
@@ -362,11 +362,13 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (ReadSpecialProperties(reader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
|
||||
return newValue;
|
||||
|
||||
if (!HasDefinedType(objectType))
|
||||
return CreateJObject(reader);
|
||||
if (contract == null || objectType == typeof(object) || objectType == typeof(IDynamicMetaObjectProvider))
|
||||
{
|
||||
if (!HasDefinedType(objectType))
|
||||
return CreateJObject(reader);
|
||||
|
||||
if (contract == null)
|
||||
throw JsonSerializationException.Create(reader, "Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
}
|
||||
|
||||
switch (contract.ContractType)
|
||||
{
|
||||
@@ -1634,7 +1636,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
{
|
||||
object newObject = null;
|
||||
|
||||
if (objectContract.UnderlyingType.IsInterface() || objectContract.UnderlyingType.IsAbstract())
|
||||
if (!objectContract.IsInstantiable)
|
||||
throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType));
|
||||
|
||||
if (objectContract.OverrideConstructor != null)
|
||||
|
||||
@@ -31,15 +31,36 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal static class MathUtils
|
||||
{
|
||||
public static int IntLength(int i)
|
||||
public static int IntLength(ulong i)
|
||||
{
|
||||
if (i < 0)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
if (i < 10000000000)
|
||||
{
|
||||
if (i < 10) return 1;
|
||||
if (i < 100) return 2;
|
||||
if (i < 1000) return 3;
|
||||
if (i < 10000) return 4;
|
||||
if (i < 100000) return 5;
|
||||
if (i < 1000000) return 6;
|
||||
if (i < 10000000) return 7;
|
||||
if (i < 100000000) return 8;
|
||||
if (i < 1000000000) return 9;
|
||||
|
||||
if (i == 0)
|
||||
return 1;
|
||||
return 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i < 100000000000) return 11;
|
||||
if (i < 1000000000000) return 12;
|
||||
if (i < 10000000000000) return 13;
|
||||
if (i < 100000000000000) return 14;
|
||||
if (i < 1000000000000000) return 15;
|
||||
if (i < 10000000000000000) return 16;
|
||||
if (i < 100000000000000000) return 17;
|
||||
if (i < 1000000000000000000) return 18;
|
||||
if (i < 10000000000000000000) return 19;
|
||||
|
||||
return (int)Math.Floor(Math.Log10(i)) + 1;
|
||||
return 20;
|
||||
}
|
||||
}
|
||||
|
||||
public static char IntToHex(int n)
|
||||
|
||||
Reference in New Issue
Block a user