-Added IgnoreAll to DefaultValueHandling which ignores all default class/struct/privative values

-Added support for DataMemberAttribute's EmitDefaultValue
-Fixed losing decimal precision when writing JValues
-Fixed error when calling ToList on empty JObjects and JArrays
This commit is contained in:
James Newton-King
2012-09-01 13:34:07 +12:00
parent 028c287cff
commit d9c8d78d77
20 changed files with 312 additions and 39 deletions
@@ -701,7 +701,7 @@ namespace Newtonsoft.Json.Tests.Linq
// "Enabled": true,
// "Price": 2.45,
// "StockCount": 9000,
// "StockValue": 22050.0
// "StockValue": 22050.00
// }
Assert.AreEqual(@"{
@@ -709,7 +709,7 @@ namespace Newtonsoft.Json.Tests.Linq
""Enabled"": true,
""Price"": 2.45,
""StockCount"": 9000,
""StockValue"": 22050.0
""StockValue"": 22050.00
}", json);
}
}
@@ -561,5 +561,21 @@ Parameter name: index",
JArray.Parse(json);
});
}
[Test]
public void ToListOnEmptyArray()
{
string json = @"{""decks"":[]}";
JArray decks = (JArray)JObject.Parse(json)["decks"];
IList<JToken> l = decks.ToList();
Assert.AreEqual(0, l.Count);
json = @"{""decks"":[1]}";
decks = (JArray)JObject.Parse(json)["decks"];
l = decks.ToList();
Assert.AreEqual(1, l.Count);
}
}
}
@@ -1846,5 +1846,24 @@ Parameter name: arrayIndex",
Assert.IsFalse(o1.DeepEquals(null));
}
[Test]
public void ToListOnEmptyObject()
{
JObject o = JObject.Parse(@"{}");
IList<JToken> l1 = o.ToList<JToken>();
Assert.AreEqual(0, l1.Count);
IList<KeyValuePair<string, JToken>> l2 = o.ToList<KeyValuePair<string, JToken>>();
Assert.AreEqual(0, l2.Count);
o = JObject.Parse(@"{'hi':null}");
l1 = o.ToList<JToken>();
Assert.AreEqual(1, l1.Count);
l2 = o.ToList<KeyValuePair<string, JToken>>();
Assert.AreEqual(1, l2.Count);
}
}
}
@@ -314,5 +314,33 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual("5.2", json);
}
public class Rate
{
public decimal Compoundings { get; set; }
}
private readonly Rate rate = new Rate { Compoundings = 12.166666666666666666666666667m };
[Test]
public void WriteFullDecimalPrecision()
{
var jTokenWriter = new JTokenWriter();
new JsonSerializer().Serialize(jTokenWriter, rate);
string json = jTokenWriter.Token.ToString();
Assert.AreEqual(@"{
""Compoundings"": 12.166666666666666666666666667
}", json);
}
[Test]
public void RoundTripDecimal()
{
var jTokenWriter = new JTokenWriter();
new JsonSerializer().Serialize(jTokenWriter, rate);
var rate2 = new JsonSerializer().Deserialize<Rate>(new JTokenReader(jTokenWriter.Token));
Assert.AreEqual(rate.Compoundings, rate2.Compoundings);
}
}
}