diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index e958273c..72ebb846 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -76,5 +76,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.9.15324")] +[assembly: AssemblyFileVersion("4.5.9.15402")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 25a6f13b..9aefe97a 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -1312,7 +1312,7 @@ keyword such as type of business."" String strFromTest = JsonConvert.SerializeObject(testClass); ExceptionAssert.Throws( - @"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantated. Path 'co.Name', line 1, position 14.", + @"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantiated. Path 'co.Name', line 1, position 14.", () => { InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass)JsonConvert.DeserializeObject(strFromTest, typeof(InterfacePropertyTestClass)); @@ -7162,6 +7162,23 @@ Parameter name: value", }", json); } #endif + +#if !NET20 + [Test] + public void RoundtripOfDateTimeOffset() + { + var content = @"{""startDateTime"":""2012-07-19T14:30:00+09:30""}"; + + var jsonSerializerSettings = new JsonSerializerSettings() {DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind}; + + var obj = (JObject)JsonConvert.DeserializeObject(content, jsonSerializerSettings); + + var dateTimeOffset = (DateTimeOffset)((JValue) obj["startDateTime"]).Value; + + Assert.AreEqual(TimeSpan.FromHours(9.5), dateTimeOffset.Offset); + Assert.AreEqual("07/19/2012 14:30:00 +09:30", dateTimeOffset.ToString(CultureInfo.InvariantCulture)); + } +#endif } #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) diff --git a/Src/Newtonsoft.Json/DefaultValueHandling.cs b/Src/Newtonsoft.Json/DefaultValueHandling.cs index 92ee7b62..e427ed27 100644 --- a/Src/Newtonsoft.Json/DefaultValueHandling.cs +++ b/Src/Newtonsoft.Json/DefaultValueHandling.cs @@ -47,8 +47,8 @@ namespace Newtonsoft.Json /// Ignore members where the member value is the same as the member's default value when serializing objects /// so that is is not written to JSON. /// This option will ignore all default values (e.g. null for objects and nullable typesl; 0 for integers, - /// decimals and floating point numbers; and false for booleans). The default value ignored can be override by - /// the . + /// decimals and floating point numbers; and false for booleans). The default value ignored can be changed by + /// placing the on the property. /// Ignore = 1, /// diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index f76e96b3..9486b0c0 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -530,7 +530,7 @@ namespace Newtonsoft.Json reader.DateTimeZoneHandling = _dateTimeZoneHandling.Value; } DateParseHandling? previousDateParseHandling = null; - if (_dateParseHandling != null && reader.DateTimeZoneHandling != _dateTimeZoneHandling) + if (_dateParseHandling != null && reader.DateParseHandling != _dateParseHandling) { previousDateParseHandling = reader.DateParseHandling; reader.DateParseHandling = _dateParseHandling.Value; diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 0dcf22ba..583d73d9 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -432,13 +432,22 @@ namespace Newtonsoft.Json WritePropertyName(reader.Value.ToString()); break; case JsonToken.Comment: - WriteComment(reader.Value.ToString()); + WriteComment((reader.Value != null) ? reader.Value.ToString() : null); break; case JsonToken.Integer: WriteValue(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture)); break; case JsonToken.Float: - WriteValue(Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture)); + object value = reader.Value; + + if (value is decimal) + WriteValue((decimal)value); + else if (value is double) + WriteValue((double)value); + else if (value is float) + WriteValue((float)value); + else + WriteValue(Convert.ToDouble(value, CultureInfo.InvariantCulture)); break; case JsonToken.String: WriteValue(reader.Value.ToString()); @@ -462,10 +471,15 @@ namespace Newtonsoft.Json WriteEndConstructor(); break; case JsonToken.Date: - WriteValue((DateTime)reader.Value); +#if !PocketPC && !NET20 + if (reader.Value is DateTimeOffset) + WriteValue((DateTimeOffset)reader.Value); + else +#endif + WriteValue(Convert.ToDateTime(reader.Value, CultureInfo.InvariantCulture)); break; case JsonToken.Raw: - WriteRawValue((string)reader.Value); + WriteRawValue((reader.Value != null) ? reader.Value.ToString() : null); break; case JsonToken.Bytes: WriteValue((byte[])reader.Value); diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index dcb0baa9..942d9c05 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -90,7 +90,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.9.15324")] +[assembly: AssemblyFileVersion("4.5.9.15402")] #endif [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 56aaf7ef..37051213 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -432,7 +432,10 @@ namespace Newtonsoft.Json.Serialization foreach (ParameterInfo parameterInfo in constructorParameters) { - JsonProperty matchingMemberProperty = memberProperties.GetClosestMatchProperty(parameterInfo.Name); + // it is possible to generate a ParameterInfo with a null name using Reflection.Emit + // protect against an ArgumentNullException from GetClosestMatchProperty by testing for null here + JsonProperty matchingMemberProperty = (parameterInfo.Name != null) ? memberProperties.GetClosestMatchProperty(parameterInfo.Name) : null; + // type must match as well as name if (matchingMemberProperty != null && matchingMemberProperty.PropertyType != parameterInfo.ParameterType) matchingMemberProperty = null; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index c753fbea..b559f9ae 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -1165,7 +1165,7 @@ To fix this error either change the environment to be fully trusted, change the IDynamicMetaObjectProvider newObject; if (contract.UnderlyingType.IsInterface() || contract.UnderlyingType.IsAbstract()) - throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); + throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); if (contract.DefaultCreator != null && (!contract.DefaultCreatorNonPublic || Serializer.ConstructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor)) @@ -1446,7 +1446,7 @@ To fix this error either change the environment to be fully trusted, change the object newObject = null; if (objectContract.UnderlyingType.IsInterface() || objectContract.UnderlyingType.IsAbstract()) - throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantated.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType)); + throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType)); if (objectContract.OverrideConstructor != null) {