-Fixed attributes on nullable structs not being used
-Fixed deserializing nullable enums -Fixed JsonConstructorAttribute incorrectly being allowed on properties -Added new NUnit
This commit is contained in:
@@ -263,6 +263,30 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Could not convert string to integer: a.")]
|
||||
public void ReadAsInt32BadString()
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
|
||||
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
|
||||
Assert.AreEqual(false, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling);
|
||||
|
||||
reader.ReadRootValueAsArray = true;
|
||||
reader.DateTimeKindHandling = DateTimeKind.Utc;
|
||||
|
||||
Assert.AreEqual(true, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
|
||||
reader.ReadAsInt32();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadBytes()
|
||||
{
|
||||
|
||||
@@ -66,5 +66,5 @@ using System.Runtime.InteropServices;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.0.7.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.0.7.14524")]
|
||||
[assembly: AssemblyFileVersion("4.0.7.14603")]
|
||||
#endif
|
||||
|
||||
@@ -5162,13 +5162,13 @@ keyword such as type of business.""
|
||||
|
||||
Widget w = JsonConvert.DeserializeObject<Widget>(json);
|
||||
|
||||
Assert.AreEqual(new WidgetId { Value = "id" }, w.Id);
|
||||
Assert.AreEqual(new WidgetId { Value = "id" }, w.Id.Value);
|
||||
Assert.AreEqual(new WidgetId {Value = "id"}, w.Id);
|
||||
Assert.AreEqual(new WidgetId {Value = "id"}, w.Id.Value);
|
||||
Assert.AreEqual("id", w.Id.Value.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected token when reading integer: Boolean. Line 2, position 22.")]
|
||||
[ExpectedException(typeof (JsonReaderException), ExpectedMessage = "Unexpected token when reading integer: Boolean. Line 2, position 22.")]
|
||||
public void DeserializeBoolInt()
|
||||
{
|
||||
string json = @"{
|
||||
@@ -5180,7 +5180,7 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected end when reading integer. Line 2, position 18.")]
|
||||
[ExpectedException(typeof (JsonReaderException), ExpectedMessage = "Unexpected end when reading integer. Line 2, position 18.")]
|
||||
public void DeserializeUnexpectedEndInt()
|
||||
{
|
||||
string json = @"{
|
||||
@@ -5188,6 +5188,166 @@ keyword such as type of business.""
|
||||
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeNullableGuid()
|
||||
{
|
||||
string json = @"{""Id"":null}";
|
||||
var c = JsonConvert.DeserializeObject<NullableGuid>(json);
|
||||
|
||||
Assert.AreEqual(null, c.Id);
|
||||
|
||||
json = @"{""Id"":""d8220a4b-75b1-4b7a-8112-b7bdae956a45""}";
|
||||
c = JsonConvert.DeserializeObject<NullableGuid>(json);
|
||||
|
||||
Assert.AreEqual(new Guid("d8220a4b-75b1-4b7a-8112-b7bdae956a45"), c.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeGuid()
|
||||
{
|
||||
Item expected = new Item()
|
||||
{
|
||||
SourceTypeID = new Guid("d8220a4b-75b1-4b7a-8112-b7bdae956a45"),
|
||||
BrokerID = new Guid("951663c4-924e-4c86-a57a-7ed737501dbd"),
|
||||
Latitude = 33.657145,
|
||||
Longitude = -117.766684,
|
||||
TimeStamp = new DateTime(2000, 3, 1, 23, 59, 59, DateTimeKind.Utc),
|
||||
Payload = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
};
|
||||
|
||||
string jsonString = JsonConvert.SerializeObject(expected, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""SourceTypeID"": ""d8220a4b-75b1-4b7a-8112-b7bdae956a45"",
|
||||
""BrokerID"": ""951663c4-924e-4c86-a57a-7ed737501dbd"",
|
||||
""Latitude"": 33.657145,
|
||||
""Longitude"": -117.766684,
|
||||
""TimeStamp"": ""\/Date(951955199000)\/"",
|
||||
""Payload"": {
|
||||
""$type"": ""System.Byte[], mscorlib"",
|
||||
""$value"": ""AAECAwQFBgcICQ==""
|
||||
}
|
||||
}", jsonString);
|
||||
|
||||
Item actual = JsonConvert.DeserializeObject<Item>(jsonString);
|
||||
|
||||
Assert.AreEqual(new Guid("d8220a4b-75b1-4b7a-8112-b7bdae956a45"), actual.SourceTypeID);
|
||||
Assert.AreEqual(new Guid("951663c4-924e-4c86-a57a-7ed737501dbd"), actual.BrokerID);
|
||||
Assert.AreEqual(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, actual.Payload);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeObjectDictionary()
|
||||
{
|
||||
var serializer = JsonSerializer.Create(new JsonSerializerSettings());
|
||||
var dict = serializer.Deserialize<Dictionary<string, string>>(new JsonTextReader(new StringReader("{'k1':'','k2':'v2'}")));
|
||||
|
||||
Assert.AreEqual("", dict["k1"]);
|
||||
Assert.AreEqual("v2", dict["k2"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeNullableEnum()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new WithEnums
|
||||
{
|
||||
Id = 7,
|
||||
NullableEnum = null
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{""Id"":7,""NullableEnum"":null}", json);
|
||||
|
||||
WithEnums e = JsonConvert.DeserializeObject<WithEnums>(json);
|
||||
|
||||
Assert.AreEqual(null, e.NullableEnum);
|
||||
|
||||
json = JsonConvert.SerializeObject(new WithEnums
|
||||
{
|
||||
Id = 7,
|
||||
NullableEnum = MyEnum.Value2
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{""Id"":7,""NullableEnum"":1}", json);
|
||||
|
||||
e = JsonConvert.DeserializeObject<WithEnums>(json);
|
||||
|
||||
Assert.AreEqual(MyEnum.Value2, e.NullableEnum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullableStructWithConverter()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new Widget1 { Id = new WidgetId1 { Value = 1234 } });
|
||||
|
||||
Assert.AreEqual(@"{""Id"":""1234""}", json);
|
||||
|
||||
Widget1 w = JsonConvert.DeserializeObject<Widget1>(@"{""Id"":""1234""}");
|
||||
|
||||
Assert.AreEqual(new WidgetId1 { Value = 1234 }, w.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public class Widget1
|
||||
{
|
||||
public WidgetId1? Id { get; set; }
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(WidgetIdJsonConverter))]
|
||||
public struct WidgetId1
|
||||
{
|
||||
public long Value { get; set; }
|
||||
}
|
||||
|
||||
public class WidgetIdJsonConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(WidgetId1) || objectType == typeof(WidgetId1?);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
WidgetId1 id = (WidgetId1)value;
|
||||
writer.WriteValue(id.Value.ToString());
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return null;
|
||||
return new WidgetId1 { Value = int.Parse(reader.Value.ToString()) };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public enum MyEnum
|
||||
{
|
||||
Value1,
|
||||
Value2,
|
||||
Value3
|
||||
}
|
||||
|
||||
public class WithEnums
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public MyEnum? NullableEnum { get; set; }
|
||||
}
|
||||
|
||||
public class Item
|
||||
{
|
||||
public Guid SourceTypeID { get; set; }
|
||||
public Guid BrokerID { get; set; }
|
||||
public double Latitude { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
public DateTime TimeStamp { get; set; }
|
||||
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
|
||||
public object Payload { get; set; }
|
||||
}
|
||||
|
||||
public class NullableGuid
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
}
|
||||
|
||||
public class Widget
|
||||
|
||||
@@ -6,9 +6,9 @@ using System.Text;
|
||||
namespace Newtonsoft.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Instructs the <see cref="JsonSerializer"/> not to serialize the public field or public read/write property value.
|
||||
/// Instructs the <see cref="JsonSerializer"/> to use the specified constructor when deserializing that object.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Property, AllowMultiple = false)]
|
||||
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false)]
|
||||
public sealed class JsonConstructorAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.0.7.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.0.7.14524")]
|
||||
[assembly: AssemblyFileVersion("4.0.7.14603")]
|
||||
#endif
|
||||
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
@@ -419,7 +419,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
#endif
|
||||
private void InitializeContract(JsonContract contract)
|
||||
{
|
||||
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType);
|
||||
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.NonNullableUnderlyingType);
|
||||
if (containerAttribute != null)
|
||||
{
|
||||
contract.IsReference = containerAttribute._isReference;
|
||||
@@ -427,17 +427,17 @@ namespace Newtonsoft.Json.Serialization
|
||||
#if !PocketPC && !NET20
|
||||
else
|
||||
{
|
||||
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType);
|
||||
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.NonNullableUnderlyingType);
|
||||
// doesn't have a null value
|
||||
if (dataContractAttribute != null && dataContractAttribute.IsReference)
|
||||
contract.IsReference = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
contract.Converter = ResolveContractConverter(contract.UnderlyingType);
|
||||
contract.Converter = ResolveContractConverter(contract.NonNullableUnderlyingType);
|
||||
|
||||
// then see whether object is compadible with any of the built in converters
|
||||
contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.UnderlyingType);
|
||||
contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.NonNullableUnderlyingType);
|
||||
|
||||
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
|
||||
|| contract.CreatedType.IsValueType)
|
||||
@@ -448,7 +448,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
|
||||
}
|
||||
|
||||
ResolveCallbackMethods(contract, contract.UnderlyingType);
|
||||
ResolveCallbackMethods(contract, contract.NonNullableUnderlyingType);
|
||||
}
|
||||
|
||||
private void ResolveCallbackMethods(JsonContract contract, Type t)
|
||||
|
||||
@@ -530,18 +530,18 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
if (contract.IsConvertable)
|
||||
{
|
||||
if (targetType.IsEnum)
|
||||
if (contract.NonNullableUnderlyingType.IsEnum)
|
||||
{
|
||||
if (value is string)
|
||||
return Enum.Parse(targetType, value.ToString(), true);
|
||||
return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true);
|
||||
else if (ConvertUtils.IsInteger(value))
|
||||
return Enum.ToObject(targetType, value);
|
||||
return Enum.ToObject(contract.NonNullableUnderlyingType, value);
|
||||
}
|
||||
|
||||
return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture);
|
||||
}
|
||||
|
||||
return ConvertUtils.ConvertOrCast(value, culture, targetType);
|
||||
return ConvertUtils.ConvertOrCast(value, culture, contract.NonNullableUnderlyingType);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user