-Refactoring
This commit is contained in:
@@ -23,7 +23,10 @@
|
|||||||
// OTHER DEALINGS IN THE SOFTWARE.
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Newtonsoft.Json.Bson;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -272,6 +275,47 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
Assert.AreEqual(t1.TableName, t2.TableName);
|
Assert.AreEqual(t1.TableName, t2.TableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RoundtripBsonBytes()
|
||||||
|
{
|
||||||
|
Guid g = new Guid("EDE9A599-A7D9-44A9-9243-7C287049DD20");
|
||||||
|
|
||||||
|
var table = new DataTable();
|
||||||
|
table.Columns.Add("data", typeof(byte[]));
|
||||||
|
table.Columns.Add("id", typeof(Guid));
|
||||||
|
table.Rows.Add(Encoding.UTF8.GetBytes("Hello world!"), g);
|
||||||
|
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
|
||||||
|
MemoryStream ms = new MemoryStream();
|
||||||
|
BsonWriter bw = new BsonWriter(ms);
|
||||||
|
|
||||||
|
serializer.Serialize(bw, table);
|
||||||
|
|
||||||
|
JToken o = JToken.ReadFrom(new BsonReader(new MemoryStream(ms.ToArray())) { ReadRootValueAsArray = true });
|
||||||
|
Assert.AreEqual(@"[
|
||||||
|
{
|
||||||
|
""data"": ""SGVsbG8gd29ybGQh"",
|
||||||
|
""id"": ""ede9a599-a7d9-44a9-9243-7c287049dd20""
|
||||||
|
}
|
||||||
|
]", o.ToString());
|
||||||
|
|
||||||
|
DataTable deserializedDataTable = serializer.Deserialize<DataTable>(new BsonReader(new MemoryStream(ms.ToArray())) { ReadRootValueAsArray = true });
|
||||||
|
|
||||||
|
Assert.AreEqual(string.Empty, deserializedDataTable.TableName);
|
||||||
|
Assert.AreEqual(2, deserializedDataTable.Columns.Count);
|
||||||
|
Assert.AreEqual("data", deserializedDataTable.Columns[0].ColumnName);
|
||||||
|
Assert.AreEqual(typeof(byte[]), deserializedDataTable.Columns[0].DataType);
|
||||||
|
Assert.AreEqual("id", deserializedDataTable.Columns[1].ColumnName);
|
||||||
|
Assert.AreEqual(typeof(Guid), deserializedDataTable.Columns[1].DataType);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, deserializedDataTable.Rows.Count);
|
||||||
|
|
||||||
|
DataRow dr1 = deserializedDataTable.Rows[0];
|
||||||
|
CollectionAssert.AreEquivalent(Encoding.UTF8.GetBytes("Hello world!"), (byte[])dr1["data"]);
|
||||||
|
Assert.AreEqual(g, (Guid)dr1["id"]);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void SerializeDataTableWithNull()
|
public void SerializeDataTableWithNull()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
|
|
||||||
dr[columnName] = nestedDt;
|
dr[columnName] = nestedDt;
|
||||||
}
|
}
|
||||||
else if (column.DataType.IsArray)
|
else if (column.DataType.IsArray && column.DataType != typeof(byte[]))
|
||||||
{
|
{
|
||||||
if (reader.TokenType == JsonToken.StartArray)
|
if (reader.TokenType == JsonToken.StartArray)
|
||||||
reader.Read();
|
reader.Read();
|
||||||
@@ -184,6 +184,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
case JsonToken.Float:
|
case JsonToken.Float:
|
||||||
case JsonToken.String:
|
case JsonToken.String:
|
||||||
case JsonToken.Date:
|
case JsonToken.Date:
|
||||||
|
case JsonToken.Bytes:
|
||||||
return reader.ValueType;
|
return reader.ValueType;
|
||||||
case JsonToken.Null:
|
case JsonToken.Null:
|
||||||
case JsonToken.Undefined:
|
case JsonToken.Undefined:
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ namespace Newtonsoft.Json.Schema
|
|||||||
{
|
{
|
||||||
CurrentSchema.Enum = new List<JToken>();
|
CurrentSchema.Enum = new List<JToken>();
|
||||||
|
|
||||||
EnumValues<long> enumValues = EnumUtils.GetNamesAndValues<long>(type);
|
IList<EnumValue<long>> enumValues = EnumUtils.GetNamesAndValues<long>(type);
|
||||||
foreach (EnumValue<long> enumValue in enumValues)
|
foreach (EnumValue<long> enumValue in enumValues)
|
||||||
{
|
{
|
||||||
JToken value = JToken.FromObject(enumValue.Value);
|
JToken value = JToken.FromObject(enumValue.Value);
|
||||||
|
|||||||
@@ -916,9 +916,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
|
ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
|
||||||
if (constructorInfo != null)
|
if (constructorInfo != null)
|
||||||
{
|
{
|
||||||
MethodCall<object, object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructorInfo);
|
ObjectConstructor<object> creator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(constructorInfo);
|
||||||
|
|
||||||
contract.ISerializableCreator = (args => methodCall(null, args));
|
contract.ISerializableCreator = creator;
|
||||||
}
|
}
|
||||||
|
|
||||||
return contract;
|
return contract;
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
private readonly Type _genericCollectionDefinitionType;
|
private readonly Type _genericCollectionDefinitionType;
|
||||||
|
|
||||||
private Type _genericWrapperType;
|
private Type _genericWrapperType;
|
||||||
private MethodCall<object, object> _genericWrapperCreator;
|
private ObjectConstructor<object> _genericWrapperCreator;
|
||||||
private Func<object> _genericTemporaryCollectionCreator;
|
private Func<object> _genericTemporaryCollectionCreator;
|
||||||
|
|
||||||
internal bool IsArray { get; private set; }
|
internal bool IsArray { get; private set; }
|
||||||
@@ -222,10 +222,10 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
constructorArgument = _genericCollectionDefinitionType;
|
constructorArgument = _genericCollectionDefinitionType;
|
||||||
|
|
||||||
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument });
|
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument });
|
||||||
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
|
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(genericWrapperConstructor);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (IWrappedCollection)_genericWrapperCreator(null, list);
|
return (IWrappedCollection)_genericWrapperCreator(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IList CreateTemporaryCollection()
|
internal IList CreateTemporaryCollection()
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
private readonly Type _genericCollectionDefinitionType;
|
private readonly Type _genericCollectionDefinitionType;
|
||||||
|
|
||||||
private Type _genericWrapperType;
|
private Type _genericWrapperType;
|
||||||
private MethodCall<object, object> _genericWrapperCreator;
|
private ObjectConstructor<object> _genericWrapperCreator;
|
||||||
|
|
||||||
private Func<object> _genericTemporaryDictionaryCreator;
|
private Func<object> _genericTemporaryDictionaryCreator;
|
||||||
|
|
||||||
@@ -166,10 +166,10 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
_genericWrapperType = typeof(DictionaryWrapper<,>).MakeGenericType(DictionaryKeyType, DictionaryValueType);
|
_genericWrapperType = typeof(DictionaryWrapper<,>).MakeGenericType(DictionaryKeyType, DictionaryValueType);
|
||||||
|
|
||||||
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
|
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
|
||||||
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
|
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParametrizedConstructor(genericWrapperConstructor);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (IWrappedDictionary)_genericWrapperCreator(null, dictionary);
|
return (IWrappedDictionary)_genericWrapperCreator(dictionary);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IDictionary CreateTemporaryDictionary()
|
internal IDictionary CreateTemporaryDictionary()
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
Type underlyingType = Enum.GetUnderlyingType(value.GetType());
|
Type underlyingType = Enum.GetUnderlyingType(value.GetType());
|
||||||
|
|
||||||
ulong num = Convert.ToUInt64(value, CultureInfo.InvariantCulture);
|
ulong num = Convert.ToUInt64(value, CultureInfo.InvariantCulture);
|
||||||
EnumValues<ulong> enumNameValues = GetNamesAndValues<T>();
|
IList<EnumValue<ulong>> enumNameValues = GetNamesAndValues<T>();
|
||||||
IList<T> selectedFlagsValues = new List<T>();
|
IList<T> selectedFlagsValues = new List<T>();
|
||||||
|
|
||||||
foreach (EnumValue<ulong> enumNameValue in enumNameValues)
|
foreach (EnumValue<ulong> enumNameValue in enumNameValues)
|
||||||
@@ -66,7 +66,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
/// Gets a dictionary of the names and values of an Enum type.
|
/// Gets a dictionary of the names and values of an Enum type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static EnumValues<ulong> GetNamesAndValues<T>() where T : struct
|
public static IList<EnumValue<ulong>> GetNamesAndValues<T>() where T : struct
|
||||||
{
|
{
|
||||||
return GetNamesAndValues<ulong>(typeof(T));
|
return GetNamesAndValues<ulong>(typeof(T));
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="enumType">The enum type to get names and values for.</param>
|
/// <param name="enumType">The enum type to get names and values for.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static EnumValues<TUnderlyingType> GetNamesAndValues<TUnderlyingType>(Type enumType) where TUnderlyingType : struct
|
public static IList<EnumValue<TUnderlyingType>> GetNamesAndValues<TUnderlyingType>(Type enumType) where TUnderlyingType : struct
|
||||||
{
|
{
|
||||||
if (enumType == null)
|
if (enumType == null)
|
||||||
throw new ArgumentNullException("enumType");
|
throw new ArgumentNullException("enumType");
|
||||||
@@ -86,7 +86,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
IList<object> enumValues = GetValues(enumType);
|
IList<object> enumValues = GetValues(enumType);
|
||||||
IList<string> enumNames = GetNames(enumType);
|
IList<string> enumNames = GetNames(enumType);
|
||||||
|
|
||||||
EnumValues<TUnderlyingType> nameValues = new EnumValues<TUnderlyingType>();
|
IList<EnumValue<TUnderlyingType>> nameValues = new List<EnumValue<TUnderlyingType>>();
|
||||||
|
|
||||||
for (int i = 0; i < enumValues.Count; i++)
|
for (int i = 0; i < enumValues.Count; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,37 +1,37 @@
|
|||||||
#region License
|
//#region License
|
||||||
// Copyright (c) 2007 James Newton-King
|
//// Copyright (c) 2007 James Newton-King
|
||||||
//
|
////
|
||||||
// Permission is hereby granted, free of charge, to any person
|
//// Permission is hereby granted, free of charge, to any person
|
||||||
// obtaining a copy of this software and associated documentation
|
//// obtaining a copy of this software and associated documentation
|
||||||
// files (the "Software"), to deal in the Software without
|
//// files (the "Software"), to deal in the Software without
|
||||||
// restriction, including without limitation the rights to use,
|
//// restriction, including without limitation the rights to use,
|
||||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
//// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
// copies of the Software, and to permit persons to whom the
|
//// copies of the Software, and to permit persons to whom the
|
||||||
// Software is furnished to do so, subject to the following
|
//// Software is furnished to do so, subject to the following
|
||||||
// conditions:
|
//// conditions:
|
||||||
//
|
////
|
||||||
// The above copyright notice and this permission notice shall be
|
//// The above copyright notice and this permission notice shall be
|
||||||
// included in all copies or substantial portions of the Software.
|
//// included in all copies or substantial portions of the Software.
|
||||||
//
|
////
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
//// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
//// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
//// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
//// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
//// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
//// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
// OTHER DEALINGS IN THE SOFTWARE.
|
//// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#endregion
|
//#endregion
|
||||||
|
|
||||||
using System.Collections.ObjectModel;
|
//using System.Collections.ObjectModel;
|
||||||
|
|
||||||
namespace Newtonsoft.Json.Utilities
|
//namespace Newtonsoft.Json.Utilities
|
||||||
{
|
//{
|
||||||
internal class EnumValues<T> : KeyedCollection<string, EnumValue<T>> where T : struct
|
// internal class EnumValues<T> : KeyedCollection<string, EnumValue<T>> where T : struct
|
||||||
{
|
// {
|
||||||
protected override string GetKeyForItem(EnumValue<T> item)
|
// protected override string GetKeyForItem(EnumValue<T> item)
|
||||||
{
|
// {
|
||||||
return item.Name;
|
// return item.Name;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
@@ -71,5 +71,4 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user