-Fixed casting dynamic base64 string to byte array

-Fixed formatting override on SerializeObject methods
-Added support for deserializing nested DataTables and arrays in DataTables
This commit is contained in:
James Newton-King
2013-12-07 16:44:02 +13:00
parent 145296f98a
commit d5f606c435
9 changed files with 213 additions and 48 deletions
@@ -23,6 +23,10 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Utilities;
#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
using System;
using System.Data;
@@ -92,31 +96,12 @@ namespace Newtonsoft.Json.Converters
reader.Read();
}
reader.Read();
while (reader.TokenType == JsonToken.StartObject)
{
DataRow dr = dt.NewRow();
if (reader.TokenType == JsonToken.StartArray)
reader.Read();
while (reader.TokenType == JsonToken.PropertyName)
{
string columnName = (string)reader.Value;
reader.Read();
if (!dt.Columns.Contains(columnName))
{
Type columnType = GetColumnDataType(reader.TokenType);
dt.Columns.Add(new DataColumn(columnName, columnType));
}
dr[columnName] = reader.Value ?? DBNull.Value;
reader.Read();
}
dr.EndEdit();
dt.Rows.Add(dr);
while (reader.TokenType != JsonToken.EndArray)
{
CreateRow(reader, dt);
reader.Read();
}
@@ -124,8 +109,75 @@ namespace Newtonsoft.Json.Converters
return dt;
}
private static Type GetColumnDataType(JsonToken tokenType)
private static void CreateRow(JsonReader reader, DataTable dt)
{
DataRow dr = dt.NewRow();
reader.Read();
while (reader.TokenType == JsonToken.PropertyName)
{
string columnName = (string)reader.Value;
reader.Read();
DataColumn column = dt.Columns[columnName];
if (column == null)
{
Type columnType = GetColumnDataType(reader);
column = new DataColumn(columnName, columnType);
dt.Columns.Add(column);
}
if (column.DataType == typeof(DataTable))
{
if (reader.TokenType == JsonToken.StartArray)
reader.Read();
DataTable nestedDt = new DataTable();
while (reader.TokenType != JsonToken.EndArray)
{
CreateRow(reader, nestedDt);
reader.Read();
}
dr[columnName] = nestedDt;
}
else if (column.DataType.IsArray)
{
if (reader.TokenType == JsonToken.StartArray)
reader.Read();
List<object> o = new List<object>();
while (reader.TokenType != JsonToken.EndArray)
{
o.Add(reader.Value);
reader.Read();
}
Array destinationArray = Array.CreateInstance(column.DataType.GetElementType(), o.Count);
Array.Copy(o.ToArray(), destinationArray, o.Count);
dr[columnName] = destinationArray;
}
else
{
dr[columnName] = reader.Value ?? DBNull.Value;
}
reader.Read();
}
dr.EndEdit();
dt.Rows.Add(dr);
}
private static Type GetColumnDataType(JsonReader reader)
{
JsonToken tokenType = reader.TokenType;
switch (tokenType)
{
case JsonToken.Integer:
@@ -140,8 +192,15 @@ namespace Newtonsoft.Json.Converters
return typeof(bool);
case JsonToken.Date:
return typeof(DateTime);
case JsonToken.StartArray:
reader.Read();
if (reader.TokenType == JsonToken.StartObject)
return typeof(DataTable); // nested datatable
Type arrayType = GetColumnDataType(reader);
return arrayType.MakeArrayType();
default:
throw new ArgumentOutOfRangeException();
throw new JsonException("Unexpected JSON token while reading DataTable: {0}".FormatWith(CultureInfo.InvariantCulture, tokenType));
}
}
+36 -5
View File
@@ -499,7 +499,7 @@ namespace Newtonsoft.Json
/// <returns>A JSON string representation of the object.</returns>
public static string SerializeObject(object value)
{
return SerializeObject(value, Formatting.None, (JsonSerializerSettings)null);
return SerializeObject(value, null, (JsonSerializerSettings)null);
}
/// <summary>
@@ -523,7 +523,11 @@ namespace Newtonsoft.Json
/// <returns>A JSON string representation of the object.</returns>
public static string SerializeObject(object value, params JsonConverter[] converters)
{
return SerializeObject(value, Formatting.None, converters);
JsonSerializerSettings settings = (converters != null && converters.Length > 0)
? new JsonSerializerSettings { Converters = converters }
: null;
return SerializeObject(value, null, settings);
}
/// <summary>
@@ -539,7 +543,7 @@ namespace Newtonsoft.Json
? new JsonSerializerSettings { Converters = converters }
: null;
return SerializeObject(value, formatting, settings);
return SerializeObject(value, null, formatting, settings);
}
/// <summary>
@@ -553,7 +557,28 @@ namespace Newtonsoft.Json
/// </returns>
public static string SerializeObject(object value, JsonSerializerSettings settings)
{
return SerializeObject(value, Formatting.None, settings);
return SerializeObject(value, null, settings);
}
/// <summary>
/// Serializes the specified object to a JSON string using a type, formatting and <see cref="JsonSerializerSettings"/>.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="settings">The <see cref="JsonSerializerSettings"/> used to serialize the object.
/// If this is null, default serialization settings will be is used.</param>
/// <param name="type">
/// The type of the value being serialized.
/// This parameter is used when <see cref="TypeNameHandling"/> is Auto to write out the type name if the type of the value does not match.
/// Specifing the type is optional.
/// </param>
/// <returns>
/// A JSON string representation of the object.
/// </returns>
public static string SerializeObject(object value, Type type, JsonSerializerSettings settings)
{
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
return SerializeObjectInternal(value, type, jsonSerializer);
}
/// <summary>
@@ -589,12 +614,18 @@ namespace Newtonsoft.Json
public static string SerializeObject(object value, Type type, Formatting formatting, JsonSerializerSettings settings)
{
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
jsonSerializer.Formatting = formatting;
return SerializeObjectInternal(value, type, jsonSerializer);
}
private static string SerializeObjectInternal(object value, Type type, JsonSerializer jsonSerializer)
{
StringBuilder sb = new StringBuilder(256);
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = formatting;
jsonWriter.Formatting = jsonSerializer.Formatting;
jsonSerializer.Serialize(jsonWriter, value, type);
}
@@ -397,7 +397,7 @@ namespace Newtonsoft.Json.Utilities
if (targetType == typeof(TimeSpan))
return ParseTimeSpan((string)initialValue);
if (targetType == typeof (byte[]))
return ParseBytes((string) initialValue);
return System.Convert.FromBase64String((string)initialValue);
if (typeof(Type).IsAssignableFrom(targetType))
return Type.GetType((string)initialValue, true);
}
@@ -441,12 +441,6 @@ namespace Newtonsoft.Json.Utilities
throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
}
private static object ParseBytes(string initialValue)
{
return System.Convert.FromBase64String(initialValue);
}
#endregion
#region TryConvert