-Documentation

-Fixed JObject loading JSON with comments
-Fixed error when using a Specified property with no setter
-Fixed transient constructor error on Windows Phone 7
-Fixed deserializing null values into nullable generic dictionaries
This commit is contained in:
JamesNK
2011-10-01 04:41:09 +00:00
parent 7d3ce867da
commit 3baf856f92
10 changed files with 118 additions and 45 deletions
@@ -1591,6 +1591,12 @@ Parameter name: arrayIndex")]
Assert.AreEqual(false, prop4.ShouldSerializeValue(o));
}
#endif
[Test]
public void ParseEmptyObjectWithComment()
{
JObject o = JObject.Parse("{ /* A Comment */ }");
Assert.AreEqual(0, o.Count);
}
[Test]
public void FromObjectTimeSpan()
@@ -41,5 +41,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.14003")]
[assembly: AssemblyFileVersion("4.0.2.14201")]
#endif
@@ -3426,6 +3426,7 @@ keyword such as type of business.""
public int Age { get; set; }
public int Weight { get; set; }
public int Height { get; set; }
public int FavoriteNumber { get; set; }
// dummy. should never be used because it isn't of type bool
[JsonIgnore]
@@ -3444,6 +3445,14 @@ keyword such as type of business.""
[JsonIgnore]
[System.Xml.Serialization.XmlIgnoreAttribute]
public bool HeightSpecified;
[JsonIgnore]
public bool FavoriteNumberSpecified
{
// get only example
get { return FavoriteNumber != 0; }
}
}
[Test]
@@ -3465,18 +3474,21 @@ keyword such as type of business.""
Assert.IsFalse(deserialized.NameSpecified);
Assert.IsFalse(deserialized.WeightSpecified);
Assert.IsFalse(deserialized.HeightSpecified);
Assert.IsFalse(deserialized.FavoriteNumberSpecified);
Assert.AreEqual(27, deserialized.Age);
c.NameSpecified = true;
c.WeightSpecified = true;
c.HeightSpecified = true;
c.FavoriteNumber = 23;
json = JsonConvert.SerializeObject(c, Formatting.Indented);
Assert.AreEqual(@"{
""Name"": ""James"",
""Age"": 27,
""Weight"": 0,
""Height"": 0
""Height"": 0,
""FavoriteNumber"": 23
}", json);
deserialized = JsonConvert.DeserializeObject<SpecifiedTestClass>(json);
@@ -3484,7 +3496,9 @@ keyword such as type of business.""
Assert.IsTrue(deserialized.NameSpecified);
Assert.IsTrue(deserialized.WeightSpecified);
Assert.IsTrue(deserialized.HeightSpecified);
Assert.IsTrue(deserialized.FavoriteNumberSpecified);
Assert.AreEqual(27, deserialized.Age);
Assert.AreEqual(23, deserialized.FavoriteNumber);
}
// [Test]
@@ -4844,6 +4858,31 @@ keyword such as type of business.""
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri);
}
[Test]
public void NullableValueGenericDictionary()
{
IDictionary<string, int?> v1 = new Dictionary<string, int?>
{
{ "First", 1 },
{ "Second", null },
{ "Third", 3 }
};
string json = JsonConvert.SerializeObject(v1, Formatting.Indented);
Assert.AreEqual(@"{
""First"": 1,
""Second"": null,
""Third"": 3
}", json);
IDictionary<string, int?> v2 = JsonConvert.DeserializeObject<IDictionary<string, int?>>(json);
Assert.AreEqual(3, v2.Count);
Assert.AreEqual(1, v2["First"]);
Assert.AreEqual(null, v2["Second"]);
Assert.AreEqual(3, v2["Third"]);
}
}
public class UriGuidTimeSpanTestClass
+9 -10
View File
@@ -142,6 +142,15 @@ namespace Newtonsoft.Json.Linq
return (t != null && ContentsEqual(t));
}
internal override void InsertItem(int index, JToken item)
{
// don't add comments to JObject, no name to reference comment by
if (item != null && item.Type == JTokenType.Comment)
return;
base.InsertItem(index, item);
}
internal override void ValidateToken(JToken o, JToken existing)
{
ValidationUtils.ArgumentNotNull(o, "o");
@@ -480,16 +489,6 @@ namespace Newtonsoft.Json.Linq
}
}
/// <summary>
/// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
/// </summary>
/// <value></value>
/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</returns>
public int Count
{
get { return ChildrenTokens.Count; }
}
bool ICollection<KeyValuePair<string,JToken>>.IsReadOnly
{
get { return false; }
@@ -110,7 +110,7 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.14003")]
[assembly: AssemblyFileVersion("4.0.2.14201")]
#endif
[assembly: CLSCompliant(true)]
@@ -78,7 +78,11 @@ namespace Newtonsoft.Json.Serialization
/// </summary>
public class DefaultContractResolver : IContractResolver
{
internal static readonly IContractResolver Instance = new DefaultContractResolver(true);
private static readonly IContractResolver _instance = new DefaultContractResolver(true);
internal static IContractResolver Instance
{
get { return _instance; }
}
private static readonly IList<JsonConverter> BuiltInConverters = new List<JsonConverter>
{
#if !PocketPC && !SILVERLIGHT && !NET20
@@ -792,7 +796,7 @@ namespace Newtonsoft.Json.Serialization
property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess, hasExplicitAttribute);
property.ShouldSerialize = CreateShouldSerializeTest(member);
SetIsSpecifiedActions(property, member);
SetIsSpecifiedActions(property, member, allowNonPublicAccess);
return property;
}
@@ -898,7 +902,7 @@ namespace Newtonsoft.Json.Serialization
return o => (bool)shouldSerializeCall(o);
}
private void SetIsSpecifiedActions(JsonProperty property, MemberInfo member)
private void SetIsSpecifiedActions(JsonProperty property, MemberInfo member, bool allowNonPublicAccess)
{
MemberInfo specifiedMember = member.DeclaringType.GetProperty(member.Name + JsonTypeReflector.SpecifiedPostfix);
if (specifiedMember == null)
@@ -912,7 +916,9 @@ namespace Newtonsoft.Json.Serialization
Func<object, object> specifiedPropertyGet = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(specifiedMember);
property.GetIsSpecified = o => (bool)specifiedPropertyGet(o);
property.SetIsSpecified = JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(specifiedMember);
if (ReflectionUtils.CanSetMemberValue(specifiedMember, allowNonPublicAccess, false))
property.SetIsSpecified = JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(specifiedMember);
}
/// <summary>
@@ -45,6 +45,7 @@ namespace Newtonsoft.Json.Serialization
internal Type DictionaryKeyType { get; private set; }
internal Type DictionaryValueType { get; private set; }
private readonly bool _isDictionaryValueTypeNullableType;
private readonly Type _genericCollectionDefinitionType;
private Type _genericWrapperType;
private MethodCall<object, object> _genericWrapperCreator;
@@ -71,6 +72,9 @@ namespace Newtonsoft.Json.Serialization
DictionaryKeyType = keyType;
DictionaryValueType = valueType;
if (DictionaryValueType != null)
_isDictionaryValueTypeNullableType = ReflectionUtils.IsNullableType(DictionaryValueType);
if (IsTypeGenericDictionaryInterface(UnderlyingType))
{
CreatedType = ReflectionUtils.MakeGenericType(typeof(Dictionary<,>), keyType, valueType);
@@ -83,7 +87,7 @@ namespace Newtonsoft.Json.Serialization
internal IWrappedDictionary CreateWrapper(object dictionary)
{
if (dictionary is IDictionary)
if (dictionary is IDictionary && (DictionaryValueType == null || !_isDictionaryValueTypeNullableType))
return new DictionaryWrapper<object, object>((IDictionary)dictionary);
if (_genericWrapperType == null)
@@ -31,7 +31,12 @@ namespace Newtonsoft.Json.Utilities
{
internal class LateBoundReflectionDelegateFactory : ReflectionDelegateFactory
{
public static readonly LateBoundReflectionDelegateFactory Instance = new LateBoundReflectionDelegateFactory();
private static readonly LateBoundReflectionDelegateFactory _instance = new LateBoundReflectionDelegateFactory();
internal static ReflectionDelegateFactory Instance
{
get { return _instance; }
}
public override MethodCall<T, object> CreateMethodCall<T>(MethodBase method)
{