-Added support for reading byte arrays from JSON integer arrays

This commit is contained in:
JamesNK
2011-08-02 23:53:15 +00:00
parent 76fc1b3081
commit 18c9400054
3 changed files with 100 additions and 11 deletions
@@ -29,6 +29,7 @@ using System.Data.SqlTypes;
#endif
using System.Globalization;
using Newtonsoft.Json.Utilities;
using System.Collections.Generic;
namespace Newtonsoft.Json.Converters
{
@@ -105,14 +106,25 @@ namespace Newtonsoft.Json.Converters
return null;
}
if (reader.TokenType != JsonToken.String)
throw new Exception("Unexpected token parsing binary. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
byte[] data;
// current token is already at base64 string
// unable to call ReadAsBytes so do it the old fashion way
string encodedData = reader.Value.ToString();
byte[] data = Convert.FromBase64String(encodedData);
if (reader.TokenType == JsonToken.StartArray)
{
data = ReadByteArray(reader);
}
else if (reader.TokenType == JsonToken.String)
{
// current token is already at base64 string
// unable to call ReadAsBytes so do it the old fashion way
string encodedData = reader.Value.ToString();
data = Convert.FromBase64String(encodedData);
}
else
{
throw new Exception("Unexpected token parsing binary. Expected String or StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
#if !SILVERLIGHT && !PocketPC && !NET20
if (t.AssignableToTypeName(BinaryTypeName))
return Activator.CreateInstance(t, data);
@@ -124,6 +136,30 @@ namespace Newtonsoft.Json.Converters
throw new Exception("Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType));
}
private byte[] ReadByteArray(JsonReader reader)
{
List<byte> byteList = new List<byte>();
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.Integer:
byteList.Add(Convert.ToByte(reader.Value, CultureInfo.InvariantCulture));
break;
case JsonToken.EndArray:
return byteList.ToArray();
case JsonToken.Comment:
// skip
break;
default:
throw new Exception("Unexpected token when reading bytes: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
}
throw new Exception("Unexpected end when reading bytes.");
}
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>