diff options
author | JamesNK <james@newtonking.com> | 2010-09-19 14:36:17 +0400 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2010-09-19 14:36:17 +0400 |
commit | 8546dc4f620092053d1434b42df4dc9d05b2e08d (patch) | |
tree | 4f5eabdf6e7ba4086487f1ab1a4282efdcbd1d83 /Src | |
parent | 84c40bf6d0fe00899e89262e8dcbe1b5f45756c0 (diff) |
-Added DeserializeObject overload
Diffstat (limited to 'Src')
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs | 2 | ||||
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs | 49 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/JsonConvert.cs | 50 |
3 files changed, 82 insertions, 19 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 73e433b..9bbdbf4 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -331,7 +331,7 @@ Parameter name: arrayIndex")] {
string json = @"[new Date(1207285200000)]";
- JArray a = (JArray)JsonConvert.DeserializeObject(json, null);
+ JArray a = (JArray)JsonConvert.DeserializeObject(json);
JValue v = (JValue)a[0];
Assert.AreEqual(JsonConvert.ConvertJavaScriptTicksToDateTime(1207285200000), (DateTime)v);
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index bf0ddc6..1a1dd32 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -565,6 +565,55 @@ namespace Newtonsoft.Json.Tests.Serialization SearchDetails searchDetails = (SearchDetails) deserialized.Body;
// Json.NET
}
+
+ public class UrlStatus
+ {
+ public int Status { get; set; }
+ public string Url { get; set; }
+ }
+
+
+ [Test]
+ public void GenericDictionaryObject()
+ {
+ Dictionary<string, object> collection = new Dictionary<string, object>()
+ {
+ {"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}},
+ {"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}}
+ };
+
+ string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings
+ {
+ TypeNameHandling = TypeNameHandling.Objects,
+ TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
+ });
+
+ Assert.AreEqual(@"{
+ ""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"",
+ ""First"": {
+ ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"",
+ ""Status"": 404,
+ ""Url"": ""http://www.bing.com""
+ },
+ ""Second"": {
+ ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"",
+ ""Status"": 400,
+ ""Url"": ""http://www.google.com""
+ }
+}", json);
+
+ object c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
+ {
+ TypeNameHandling = TypeNameHandling.Objects,
+ TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
+ });
+
+ Assert.IsInstanceOfType(typeof(Dictionary<string, object>), c);
+
+ Dictionary<string, object> newCollection = (Dictionary<string, object>)c;
+ Assert.AreEqual(2, newCollection.Count);
+ Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url);
+ }
}
public class Message
diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index c824cad..54b9458 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -595,9 +595,9 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the specified object to a Json object.
+ /// Deserializes the JSON to a .NET object.
/// </summary>
- /// <param name="value">The object to deserialize.</param>
+ /// <param name="value">The JSON to deserialize.</param>
/// <returns>The deserialized object from the Json string.</returns>
public static object DeserializeObject(string value)
{
@@ -605,9 +605,23 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the specified object to a Json object.
+ /// Deserializes the JSON to a .NET object.
/// </summary>
- /// <param name="value">The object to deserialize.</param>
+ /// <param name="value">The JSON to deserialize.</param>
+ /// <param name="settings">
+ /// The <see cref="JsonSerializerSettings"/> used to deserialize the object.
+ /// If this is null, default serialization settings will be is used.
+ /// </param>
+ /// <returns>The deserialized object from the JSON string.</returns>
+ public static object DeserializeObject(string value, JsonSerializerSettings settings)
+ {
+ return DeserializeObject(value, null, settings);
+ }
+
+ /// <summary>
+ /// Deserializes the JSON to the specified .NET type.
+ /// </summary>
+ /// <param name="value">The JSON to deserialize.</param>
/// <param name="type">The <see cref="Type"/> of object being deserialized.</param>
/// <returns>The deserialized object from the Json string.</returns>
public static object DeserializeObject(string value, Type type)
@@ -616,10 +630,10 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the specified object to a Json object.
+ /// Deserializes the JSON to the specified .NET type.
/// </summary>
- /// <typeparam name="T">The type of the object to deserialize.</typeparam>
- /// <param name="value">The object to deserialize.</param>
+ /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
+ /// <param name="value">The JSON to deserialize.</param>
/// <returns>The deserialized object from the Json string.</returns>
public static T DeserializeObject<T>(string value)
{
@@ -627,14 +641,14 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the specified JSON to the given anonymous type.
+ /// Deserializes the JSON to the given anonymous type.
/// </summary>
/// <typeparam name="T">
/// The anonymous type to deserialize to. This can't be specified
/// traditionally and must be infered from the anonymous type passed
/// as a parameter.
/// </typeparam>
- /// <param name="value">The object to deserialize.</param>
+ /// <param name="value">The JSON to deserialize.</param>
/// <param name="anonymousTypeObject">The anonymous type object.</param>
/// <returns>The deserialized anonymous type from the JSON string.</returns>
public static T DeserializeAnonymousType<T>(string value, T anonymousTypeObject)
@@ -643,10 +657,10 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the JSON string to the specified type.
+ /// Deserializes the JSON to the specified .NET type.
/// </summary>
- /// <typeparam name="T">The type of the object to deserialize.</typeparam>
- /// <param name="value">The object to deserialize.</param>
+ /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
+ /// <param name="value">The JSON to deserialize.</param>
/// <param name="converters">Converters to use while deserializing.</param>
/// <returns>The deserialized object from the JSON string.</returns>
public static T DeserializeObject<T>(string value, params JsonConverter[] converters)
@@ -655,9 +669,9 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the JSON string to the specified type.
+ /// Deserializes the JSON to the specified .NET type.
/// </summary>
- /// <typeparam name="T">The type of the object to deserialize.</typeparam>
+ /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
/// <param name="value">The object to deserialize.</param>
/// <param name="settings">
/// The <see cref="JsonSerializerSettings"/> used to deserialize the object.
@@ -670,9 +684,9 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the JSON string to the specified type.
+ /// Deserializes the JSON to the specified .NET type.
/// </summary>
- /// <param name="value">The object to deserialize.</param>
+ /// <param name="value">The JSON to deserialize.</param>
/// <param name="type">The type of the object to deserialize.</param>
/// <param name="converters">Converters to use while deserializing.</param>
/// <returns>The deserialized object from the JSON string.</returns>
@@ -686,10 +700,10 @@ namespace Newtonsoft.Json }
/// <summary>
- /// Deserializes the JSON string to the specified type.
+ /// Deserializes the JSON to the specified .NET type.
/// </summary>
/// <param name="value">The JSON to deserialize.</param>
- /// <param name="type">The type of the object to deserialize.</param>
+ /// <param name="type">The type of the object to deserialize to.</param>
/// <param name="settings">
/// The <see cref="JsonSerializerSettings"/> used to deserialize the object.
/// If this is null, default serialization settings will be is used.
|