From 4a1c8d3a801a9b15110477d1123d45620e796461 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Fri, 11 Jun 2010 13:11:44 +0000 Subject: [PATCH] -Fix issue when deserializing string to byte data -Fix converting a JValue to a compatible type -Allow JsonConverterAttribute to be added to interfaces/structs/enums --- Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs | 7 ++++ .../Serialization/TypeNameHandlingTests.cs | 33 +++++++++++++++++++ Src/Newtonsoft.Json/JsonConverterAttribute.cs | 2 +- Src/Newtonsoft.Json/Linq/Extensions.cs | 3 ++ .../JsonSerializerInternalReader.cs | 8 +++-- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs index 0f158f7f..17f27da7 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs @@ -190,5 +190,12 @@ namespace Newtonsoft.Json.Tests.Linq JValue v = new JValue((object)null); int i = (int) v; } + + [Test] + public void ConvertValueToCompatibleType() + { + IComparable c = (new JValue(1).Value()); + Assert.AreEqual(1, c); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index a3e844f2..3ffc80bb 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -33,6 +33,7 @@ using Newtonsoft.Json.Tests.TestObjects; using NUnit.Framework; using Newtonsoft.Json.Utilities; using System.Net; +using System.Runtime.Serialization; namespace Newtonsoft.Json.Tests.Serialization { @@ -426,5 +427,37 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(4, nested[3]); Assert.AreEqual(5, nested[4]); } + +#if !SILVERLIGHT && !PocketPC + [Test] + public void DeserializeUsingCustomBinder() + { + string json = @"{ + ""$id"": ""1"", + ""$type"": ""Newtonsoft.Json.Tests.TestObjects.Employee"", + ""Name"": ""Name!"" +}"; + + object p = JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Objects, + Binder = new CustomSerializationBinder() + }); + + Assert.IsInstanceOfType(typeof(Person), p); + + Person person = (Person)p; + + Assert.AreEqual("Name!", person.Name); + } + + public class CustomSerializationBinder : SerializationBinder + { + public override Type BindToType(string assemblyName, string typeName) + { + return typeof (Person); + } + } +#endif } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonConverterAttribute.cs b/Src/Newtonsoft.Json/JsonConverterAttribute.cs index 55a0d8d5..e28fd79b 100644 --- a/Src/Newtonsoft.Json/JsonConverterAttribute.cs +++ b/Src/Newtonsoft.Json/JsonConverterAttribute.cs @@ -10,7 +10,7 @@ namespace Newtonsoft.Json /// /// Instructs the to use the specified when serializing the member or class. /// - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false)] + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum, AllowMultiple = false)] public sealed class JsonConverterAttribute : Attribute { private readonly Type _converterType; diff --git a/Src/Newtonsoft.Json/Linq/Extensions.cs b/Src/Newtonsoft.Json/Linq/Extensions.cs index b0b9ff0b..716c5c3c 100644 --- a/Src/Newtonsoft.Json/Linq/Extensions.cs +++ b/Src/Newtonsoft.Json/Linq/Extensions.cs @@ -267,6 +267,9 @@ namespace Newtonsoft.Json.Linq if (value == null) throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T))); + if (value.Value is U) + return (U)value.Value; + Type targetType = typeof(U); if (ReflectionUtils.IsNullableType(targetType)) diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 57410b28..cfe8b891 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -214,8 +214,8 @@ namespace Newtonsoft.Json.Serialization { switch (reader.TokenType) { - // populate a typed object or generic dictionary/array - // depending upon whether an objectType was supplied + // populate a typed object or generic dictionary/array + // depending upon whether an objectType was supplied case JsonToken.StartObject: return CreateObject(reader, objectType, contract, member, existingValue); case JsonToken.StartArray: @@ -233,6 +233,10 @@ namespace Newtonsoft.Json.Serialization ReflectionUtils.IsNullableType(objectType)) return null; + // string that needs to be returned as a byte array should be base 64 decoded + if (objectType == typeof(byte[])) + return Convert.FromBase64String((string)reader.Value); + return EnsureType(reader.Value, objectType); case JsonToken.StartConstructor: case JsonToken.EndConstructor: