-Fixed using FloatParseHandling.Decimal with XmlNodeConverter
-Fixed using DateParseHandling.DateTimeOffset with XmlNodeConverter
This commit is contained in:
@@ -115,6 +115,64 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class DecimalContainer
|
||||||
|
{
|
||||||
|
public decimal Number { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void FloatParseHandlingDecimal()
|
||||||
|
{
|
||||||
|
decimal d = (decimal)Math.PI + 1000000000m;
|
||||||
|
var x = new DecimalContainer { Number = d };
|
||||||
|
|
||||||
|
var json = JsonConvert.SerializeObject(x, Formatting.Indented);
|
||||||
|
|
||||||
|
XDocument doc1 = JsonConvert.DeserializeObject<XDocument>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Converters = { new XmlNodeConverter() },
|
||||||
|
FloatParseHandling = FloatParseHandling.Decimal
|
||||||
|
});
|
||||||
|
|
||||||
|
var xml = doc1.ToString();
|
||||||
|
Assert.AreEqual("<Number>1000000003.14159265358979</Number>", xml);
|
||||||
|
|
||||||
|
string json2 = JsonConvert.SerializeObject(doc1, Formatting.Indented);
|
||||||
|
|
||||||
|
DecimalContainer x2 = JsonConvert.DeserializeObject<DecimalContainer>(json2);
|
||||||
|
|
||||||
|
Assert.AreEqual(x.Number, x2.Number);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DateTimeOffsetContainer
|
||||||
|
{
|
||||||
|
public DateTimeOffset Date { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DateTimeParseHandlingOffset()
|
||||||
|
{
|
||||||
|
DateTimeOffset d = new DateTimeOffset(2012, 12, 12, 12, 44, 1, TimeSpan.FromHours(12).Add(TimeSpan.FromMinutes(34)));
|
||||||
|
var x = new DateTimeOffsetContainer { Date = d };
|
||||||
|
|
||||||
|
var json = JsonConvert.SerializeObject(x, Formatting.Indented);
|
||||||
|
|
||||||
|
XDocument doc1 = JsonConvert.DeserializeObject<XDocument>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Converters = { new XmlNodeConverter() },
|
||||||
|
DateParseHandling = DateParseHandling.DateTimeOffset
|
||||||
|
});
|
||||||
|
|
||||||
|
var xml = doc1.ToString();
|
||||||
|
Assert.AreEqual("<Date>2012-12-12T12:44:01+12:34</Date>", xml);
|
||||||
|
|
||||||
|
string json2 = JsonConvert.SerializeObject(doc1, Formatting.Indented);
|
||||||
|
|
||||||
|
DateTimeOffsetContainer x2 = JsonConvert.DeserializeObject<DateTimeOffsetContainer>(json2);
|
||||||
|
|
||||||
|
Assert.AreEqual(x.Date, x2.Date);
|
||||||
|
}
|
||||||
|
|
||||||
#if !NET20
|
#if !NET20
|
||||||
[Test]
|
[Test]
|
||||||
public void GroupElementsOfTheSameName()
|
public void GroupElementsOfTheSameName()
|
||||||
|
|||||||
@@ -50,6 +50,17 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JPathExecuteTests : TestFixtureBase
|
public class JPathExecuteTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
|
public void EvaluatePropertyWithRequired()
|
||||||
|
{
|
||||||
|
string json = "{\"bookId\":\"1000\"}";
|
||||||
|
JObject o = JObject.Parse(json);
|
||||||
|
|
||||||
|
string bookId = (string)o.SelectToken("bookId", true);
|
||||||
|
|
||||||
|
Assert.AreEqual("1000", bookId);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void EvaluateEmptyPropertyIndexer()
|
public void EvaluateEmptyPropertyIndexer()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -36,6 +36,27 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class ShouldSerializeTests : TestFixtureBase
|
public class ShouldSerializeTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
public class A
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class B
|
||||||
|
{
|
||||||
|
public A A { get; set; }
|
||||||
|
public virtual bool ShouldSerializeA()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void VirtualShouldSerializeSimple()
|
||||||
|
{
|
||||||
|
string json = JsonConvert.SerializeObject(new B());
|
||||||
|
|
||||||
|
Assert.AreEqual("{}", json);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void VirtualShouldSerialize()
|
public void VirtualShouldSerialize()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1397,6 +1397,11 @@ namespace Newtonsoft.Json.Converters
|
|||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Float)
|
else if (reader.TokenType == JsonToken.Float)
|
||||||
{
|
{
|
||||||
|
if (reader.Value is decimal)
|
||||||
|
return XmlConvert.ToString((decimal)reader.Value);
|
||||||
|
if (reader.Value is float)
|
||||||
|
return XmlConvert.ToString((float)reader.Value);
|
||||||
|
|
||||||
return XmlConvert.ToString(Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture));
|
return XmlConvert.ToString(Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture));
|
||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Boolean)
|
else if (reader.TokenType == JsonToken.Boolean)
|
||||||
@@ -1405,11 +1410,16 @@ namespace Newtonsoft.Json.Converters
|
|||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Date)
|
else if (reader.TokenType == JsonToken.Date)
|
||||||
{
|
{
|
||||||
|
#if !NET20
|
||||||
|
if (reader.Value is DateTimeOffset)
|
||||||
|
return XmlConvert.ToString((DateTimeOffset)reader.Value);
|
||||||
|
#endif
|
||||||
|
|
||||||
DateTime d = Convert.ToDateTime(reader.Value, CultureInfo.InvariantCulture);
|
DateTime d = Convert.ToDateTime(reader.Value, CultureInfo.InvariantCulture);
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
#if !(NETFX_CORE || PORTABLE)
|
||||||
return XmlConvert.ToString(d, DateTimeUtils.ToSerializationMode(d.Kind));
|
return XmlConvert.ToString(d, DateTimeUtils.ToSerializationMode(d.Kind));
|
||||||
#else
|
#else
|
||||||
return XmlConvert.ToString(d);
|
return XmlConvert.ToString(d);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Null)
|
else if (reader.TokenType == JsonToken.Null)
|
||||||
|
|||||||
Reference in New Issue
Block a user