-Added additional ToString overloads to JValue

-Fixed poor error message when dynamic type cannot be created during deserialization
-Fixed error when setting a null value on a dynamic type during deserialization
-Fixed JsonTextReader.ReadAsDecimal not attempting to convert string values to decimal
This commit is contained in:
JamesNK
2011-02-24 10:14:08 +00:00
parent 1824aa4b59
commit 71446e9772
8 changed files with 169 additions and 11 deletions
@@ -182,6 +182,103 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("Child text!", n.DynamicChildObject.Text);
Assert.AreEqual(int.MinValue, n.DynamicChildObject.Integer);
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Unable to find a default constructor to use for type System.Dynamic.DynamicObject.")]
public void NoPublicDefaultConstructor()
{
var settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
var json = @"{
""contributors"": null
}";
JsonConvert.DeserializeObject<DynamicObject>(json, settings);
}
public class DictionaryDynamicObject : DynamicObject
{
public IDictionary<string, object> Values { get; private set; }
protected DictionaryDynamicObject()
{
Values = new Dictionary<string, object>();
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
Values[binder.Name] = value;
return true;
}
}
[Test]
public void AllowNonPublicDefaultConstructor()
{
var settings = new JsonSerializerSettings();
settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
var json = @"{
""contributors"": null,
""retweeted"": false,
""text"": ""Guys SX4 diesel is launched.what are your plans?catch us at #facebook http://bit.ly/dV3H1a #auto #car #maruti #india #delhi"",
""in_reply_to_user_id_str"": null,
""retweet_count"": 0,
""geo"": null,
""id_str"": ""40678260320768000"",
""in_reply_to_status_id"": null,
""source"": ""<a href=\""http://www.tweetdeck.com\"" rel=\""nofollow\"">TweetDeck</a>"",
""created_at"": ""Thu Feb 24 07:43:47 +0000 2011"",
""place"": null,
""coordinates"": null,
""truncated"": false,
""favorited"": false,
""user"": {
""profile_background_image_url"": ""http://a1.twimg.com/profile_background_images/206944715/twitter_bg.jpg"",
""url"": ""http://bit.ly/dcFwWC"",
""screen_name"": ""marutisuzukisx4"",
""verified"": false,
""friends_count"": 45,
""description"": ""This is the Official Maruti Suzuki SX4 Twitter ID! Men are Back - mail us on social (at) sx4bymaruti (dot) com"",
""follow_request_sent"": null,
""time_zone"": ""Chennai"",
""profile_text_color"": ""333333"",
""location"": ""India"",
""notifications"": null,
""profile_sidebar_fill_color"": ""efefef"",
""id_str"": ""196143889"",
""contributors_enabled"": false,
""lang"": ""en"",
""profile_background_tile"": false,
""created_at"": ""Tue Sep 28 12:55:15 +0000 2010"",
""followers_count"": 117,
""show_all_inline_media"": true,
""listed_count"": 1,
""geo_enabled"": true,
""profile_link_color"": ""009999"",
""profile_sidebar_border_color"": ""eeeeee"",
""protected"": false,
""name"": ""Maruti Suzuki SX4"",
""statuses_count"": 637,
""following"": null,
""profile_use_background_image"": true,
""profile_image_url"": ""http://a3.twimg.com/profile_images/1170694644/Slide1_normal.JPG"",
""id"": 196143889,
""is_translator"": false,
""utc_offset"": 19800,
""favourites_count"": 0,
""profile_background_color"": ""131516""
},
""in_reply_to_screen_name"": null,
""id"": 40678260320768000,
""in_reply_to_status_id_str"": null,
""in_reply_to_user_id"": null
}";
DictionaryDynamicObject foo = JsonConvert.DeserializeObject<DictionaryDynamicObject>(json, settings);
Assert.AreEqual(false, foo.Values["retweeted"]);
}
}
}
#endif
@@ -28,6 +28,7 @@ using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC && !NET20
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Web.Script.Serialization;
#endif
using System.Text;
@@ -4178,5 +4179,29 @@ keyword such as type of business.""
""Name"": null
}", json);
}
public class DeserializeStringConvert
{
public string Name { get; set; }
public int Age { get; set; }
public double Height { get; set; }
public decimal Price { get; set; }
}
[Test]
public void DeserializeStringEnglish()
{
string json = @"{
'Name': 'James Hughes',
'Age': '40',
'Height': '44.4',
'Price': '4'
}";
DeserializeStringConvert p = JsonConvert.DeserializeObject<DeserializeStringConvert>(json);
Assert.AreEqual(40, p.Age);
Assert.AreEqual(44.4, p.Height);
Assert.AreEqual(4d, p.Price);
}
}
}