-Refactor

This commit is contained in:
James Newton-King
2013-04-25 04:12:26 +12:00
parent cba158e687
commit 405de98daf
6 changed files with 50 additions and 64 deletions
@@ -25,6 +25,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization; using System.Globalization;
#if NET20 #if NET20
using Newtonsoft.Json.Utilities.LinqBridge; using Newtonsoft.Json.Utilities.LinqBridge;
@@ -746,12 +747,12 @@ namespace Newtonsoft.Json.Tests
{ {
jsonWriter.WriteStartArray(); jsonWriter.WriteStartArray();
jsonWriter.WriteValue(int.MaxValue);
jsonWriter.WriteValue(int.MinValue);
jsonWriter.WriteValue(0); jsonWriter.WriteValue(0);
jsonWriter.WriteValue(-0); jsonWriter.WriteValue(-0);
jsonWriter.WriteValue(9L); jsonWriter.WriteValue(9L);
jsonWriter.WriteValue(9UL); jsonWriter.WriteValue(9UL);
jsonWriter.WriteValue(int.MaxValue);
jsonWriter.WriteValue(int.MinValue);
jsonWriter.WriteValue(long.MaxValue); jsonWriter.WriteValue(long.MaxValue);
jsonWriter.WriteValue(long.MinValue); jsonWriter.WriteValue(long.MinValue);
jsonWriter.WriteValue(ulong.MaxValue); jsonWriter.WriteValue(ulong.MaxValue);
@@ -763,12 +764,12 @@ namespace Newtonsoft.Json.Tests
Console.WriteLine(sb.ToString()); Console.WriteLine(sb.ToString());
Assert.AreEqual(@"[ Assert.AreEqual(@"[
0,
0,
9,
9,
2147483647, 2147483647,
-2147483648, -2147483648,
0,
0,
9,
9,
9223372036854775807, 9223372036854775807,
-9223372036854775808, -9223372036854775808,
18446744073709551615, 18446744073709551615,
+2 -2
View File
@@ -88,7 +88,7 @@ namespace Newtonsoft.Json.Bson
{ {
BsonArray value = (BsonArray)t; BsonArray value = (BsonArray)t;
_writer.Write(value.CalculatedSize); _writer.Write(value.CalculatedSize);
int index = 0; ulong index = 0;
foreach (BsonToken c in value) foreach (BsonToken c in value)
{ {
_writer.Write((sbyte)c.Type); _writer.Write((sbyte)c.Type);
@@ -261,7 +261,7 @@ namespace Newtonsoft.Json.Bson
BsonArray value = (BsonArray)t; BsonArray value = (BsonArray)t;
int size = 4; int size = 4;
int index = 0; ulong index = 0;
foreach (BsonToken c in value) foreach (BsonToken c in value)
{ {
size += 1; size += 1;
+6 -46
View File
@@ -686,61 +686,22 @@ namespace Newtonsoft.Json
_writeBuffer = new char[64]; _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) private void WriteIntegerValue(long value)
{ {
EnsureWriteBuffer(); EnsureWriteBuffer();
if (value >= 0 && value <= 9) if (value >= 0 && value <= 9)
{ {
_writer.Write((char)(48 + value)); _writer.Write((char)('0' + value));
} }
else else
{ {
int length = 0;
ulong uvalue = (value < 0) ? (ulong)-value : (ulong)value; ulong uvalue = (value < 0) ? (ulong)-value : (ulong)value;
do
{
_writeBuffer[length++] = (char)(48 + (uvalue % 10));
uvalue /= 10;
} while (uvalue != 0);
if (value < 0) if (value < 0)
_writeBuffer[length++] = '-'; _writer.Write('-');
Array.Reverse(_writeBuffer, 0, length); WriteIntegerValue(uvalue);
_writer.Write(_writeBuffer, 0, length);
} }
} }
@@ -750,20 +711,19 @@ namespace Newtonsoft.Json
if (uvalue <= 9) if (uvalue <= 9)
{ {
_writer.Write((char)(48 + uvalue)); _writer.Write((char)('0' + uvalue));
} }
else else
{ {
int totalLength = MathUtils.IntLength(uvalue);
int length = 0; int length = 0;
do do
{ {
_writeBuffer[length++] = (char)(48 + (uvalue % 10)); _writeBuffer[totalLength - ++length] = (char)('0' + (uvalue % 10));
uvalue /= 10; uvalue /= 10;
} while (uvalue != 0); } while (uvalue != 0);
Array.Reverse(_writeBuffer, 0, length);
_writer.Write(_writeBuffer, 0, length); _writer.Write(_writeBuffer, 0, length);
} }
} }
@@ -76,6 +76,7 @@ namespace Newtonsoft.Json.Serialization
internal ReadType InternalReadType; internal ReadType InternalReadType;
internal JsonContractType ContractType; internal JsonContractType ContractType;
internal bool IsReadOnlyOrFixedSize; internal bool IsReadOnlyOrFixedSize;
internal bool IsInstantiable;
private List<SerializationCallback> _onDeserializedCallbacks; private List<SerializationCallback> _onDeserializedCallbacks;
private IList<SerializationCallback> _onDeserializingCallbacks; private IList<SerializationCallback> _onDeserializingCallbacks;
@@ -280,6 +281,7 @@ namespace Newtonsoft.Json.Serialization
UnderlyingType = underlyingType; UnderlyingType = underlyingType;
IsSealed = underlyingType.IsSealed(); IsSealed = underlyingType.IsSealed();
IsInstantiable = !(underlyingType.IsInterface() || underlyingType.IsAbstract());
IsNullable = ReflectionUtils.IsNullable(underlyingType); IsNullable = ReflectionUtils.IsNullable(underlyingType);
NonNullableUnderlyingType = (IsNullable && ReflectionUtils.IsNullableType(underlyingType)) ? Nullable.GetUnderlyingType(underlyingType) : 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)) if (ReadSpecialProperties(reader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
return newValue; return newValue;
if (!HasDefinedType(objectType)) if (contract == null || objectType == typeof(object) || objectType == typeof(IDynamicMetaObjectProvider))
return CreateJObject(reader); {
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)); throw JsonSerializationException.Create(reader, "Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType));
}
switch (contract.ContractType) switch (contract.ContractType)
{ {
@@ -1634,7 +1636,7 @@ To fix this error either change the environment to be fully trusted, change the
{ {
object newObject = null; 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)); 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) if (objectContract.OverrideConstructor != null)
+27 -6
View File
@@ -31,15 +31,36 @@ namespace Newtonsoft.Json.Utilities
{ {
internal static class MathUtils internal static class MathUtils
{ {
public static int IntLength(int i) public static int IntLength(ulong i)
{ {
if (i < 0) if (i < 10000000000)
throw new ArgumentOutOfRangeException(); {
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 10;
return 1; }
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) public static char IntToHex(int n)