@@ -609,6 +609,29 @@ namespace Newtonsoft.Json.Tests.Linq
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ExpicitConversionTest()
|
||||||
|
{
|
||||||
|
const string example = "Hello";
|
||||||
|
dynamic obj = new
|
||||||
|
{
|
||||||
|
data = Encoding.UTF8.GetBytes(example)
|
||||||
|
};
|
||||||
|
byte[] bytes;
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (TextWriter tw = new StreamWriter(ms))
|
||||||
|
{
|
||||||
|
JsonSerializer.Create().Serialize(tw,obj);
|
||||||
|
tw.Flush();
|
||||||
|
bytes = ms.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dynamic o = JObject.Parse(Encoding.UTF8.GetString(bytes));
|
||||||
|
var dataBytes = (byte[]) o.data;
|
||||||
|
Assert.AreEqual(example,Encoding.UTF8.GetString(dataBytes));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GetTypeCode()
|
public void GetTypeCode()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ using System.ComponentModel;
|
|||||||
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
|
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
#endif
|
#endif
|
||||||
|
using System.Text;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
#if NET20
|
#if NET20
|
||||||
@@ -395,6 +396,8 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return new Uri((string)initialValue, UriKind.RelativeOrAbsolute);
|
return new Uri((string)initialValue, UriKind.RelativeOrAbsolute);
|
||||||
if (targetType == typeof(TimeSpan))
|
if (targetType == typeof(TimeSpan))
|
||||||
return ParseTimeSpan((string)initialValue);
|
return ParseTimeSpan((string)initialValue);
|
||||||
|
if (targetType == typeof (byte[]))
|
||||||
|
return ParseBytes((string) initialValue);
|
||||||
if (typeof(Type).IsAssignableFrom(targetType))
|
if (typeof(Type).IsAssignableFrom(targetType))
|
||||||
return Type.GetType((string)initialValue, true);
|
return Type.GetType((string)initialValue, true);
|
||||||
}
|
}
|
||||||
@@ -438,6 +441,12 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
|
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
|
#endregion
|
||||||
|
|
||||||
#region TryConvert
|
#region TryConvert
|
||||||
|
|||||||
Reference in New Issue
Block a user