-Documentation

-Fixed potential casting error when writing JSON using a JsonReader
This commit is contained in:
JamesNK
2011-10-01 11:32:48 +00:00
parent b5f5db5624
commit 4f947d9207
3 changed files with 402 additions and 375 deletions
+1 -1
View File
@@ -25,7 +25,7 @@
<h3>Conversion Rules</h3> <h3>Conversion Rules</h3>
<ul> <ul>
<li>Elements remain unchanged. </li> <li>Elements remain unchanged. </li>
<li>Attributes are prefixed with an @. </li> <li>Attributes are prefixed with an @ and should be at the start of the object. </li>
<li>Single child text nodes are a value directly against an element, otherwise they <li>Single child text nodes are a value directly against an element, otherwise they
are accessed via #text. </li> are accessed via #text. </li>
<li>The XML declaration and processing instructions are prefixed with ?. </li> <li>The XML declaration and processing instructions are prefixed with ?. </li>
@@ -461,7 +461,8 @@ keyword such as type of business.""
[Test] [Test]
public void DateTimeTest() public void DateTimeTest()
{ {
List<DateTime> testDates = new List<DateTime> { List<DateTime> testDates = new List<DateTime>
{
new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Local), new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Local),
new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Unspecified), new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Unspecified),
new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc), new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc),
@@ -487,7 +488,8 @@ keyword such as type of business.""
[Test] [Test]
public void DateTimeOffset() public void DateTimeOffset()
{ {
List<DateTimeOffset> testDates = new List<DateTimeOffset> { List<DateTimeOffset> testDates = new List<DateTimeOffset>
{
new DateTimeOffset(new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc)), new DateTimeOffset(new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc)),
new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero), new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero),
new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(13)), new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.FromHours(13)),
@@ -1391,8 +1393,10 @@ keyword such as type of business.""
{ {
[DataMember] [DataMember]
public string Name { get; set; } public string Name { get; set; }
[DataMember] [DataMember]
public int Age { get; set; } public int Age { get; set; }
public string NotIncluded { get; set; } public string NotIncluded { get; set; }
} }
@@ -1443,12 +1447,14 @@ keyword such as type of business.""
Title = title; Title = title;
} }
[DataMember] [DataMember] private string _name;
private string _name;
[DataMember(Name = "_age")] [DataMember(Name = "_age")]
private int Age { get; set; } private int Age { get; set; }
[JsonProperty] [JsonProperty]
private int Rank { get; set; } private int Rank { get; set; }
[JsonProperty(PropertyName = "JsonTitle")] [JsonProperty(PropertyName = "JsonTitle")]
[DataMember(Name = "DataTitle")] [DataMember(Name = "DataTitle")]
private string Title { get; set; } private string Title { get; set; }
@@ -2328,9 +2334,13 @@ keyword such as type of business.""
[JsonObject(MemberSerialization.OptIn)] [JsonObject(MemberSerialization.OptIn)]
public class A public class A
{ {
[JsonProperty("A1")] [JsonProperty("A1")] private string _A1;
private string _A1;
public string A1 { get { return _A1; } set { _A1 = value; } } public string A1
{
get { return _A1; }
set { _A1 = value; }
}
[JsonProperty("A2")] [JsonProperty("A2")]
private string A2 { get; set; } private string A2 { get; set; }
@@ -2341,9 +2351,13 @@ keyword such as type of business.""
{ {
public string B1 { get; set; } public string B1 { get; set; }
[JsonProperty("B2")] [JsonProperty("B2")] private string _B2;
string _B2;
public string B2 { get { return _B2; } set { _B2 = value; } } public string B2
{
get { return _B2; }
set { _B2 = value; }
}
[JsonProperty("B3")] [JsonProperty("B3")]
private string B3 { get; set; } private string B3 { get; set; }
@@ -2456,26 +2470,17 @@ keyword such as type of business.""
public int Count public int Count
{ {
get get { return _innerDictionary.Count; }
{
return _innerDictionary.Count;
}
} }
public bool IsReadOnly public bool IsReadOnly
{ {
get get { return ((IDictionary<string, T>) _innerDictionary).IsReadOnly; }
{
return ((IDictionary<string, T>)_innerDictionary).IsReadOnly;
}
} }
public ICollection<string> Keys public ICollection<string> Keys
{ {
get get { return _innerDictionary.Keys; }
{
return _innerDictionary.Keys;
}
} }
public T this[string key] public T this[string key]
@@ -2486,18 +2491,12 @@ keyword such as type of business.""
_innerDictionary.TryGetValue(key, out value); _innerDictionary.TryGetValue(key, out value);
return value; return value;
} }
set set { _innerDictionary[key] = value; }
{
_innerDictionary[key] = value;
}
} }
public ICollection<T> Values public ICollection<T> Values
{ {
get get { return _innerDictionary.Values; }
{
return _innerDictionary.Values;
}
} }
public void Add(KeyValuePair<string, T> item) public void Add(KeyValuePair<string, T> item)
@@ -2844,17 +2843,21 @@ keyword such as type of business.""
public class AA public class AA
{ {
[JsonProperty] [JsonProperty] protected int AA_field1;
protected int AA_field1;
protected int AA_field2; protected int AA_field2;
[JsonProperty] [JsonProperty]
protected int AA_property1 { get; set; } protected int AA_property1 { get; set; }
[JsonProperty] [JsonProperty]
protected int AA_property2 { get; private set; } protected int AA_property2 { get; private set; }
[JsonProperty] [JsonProperty]
protected int AA_property3 { private get; set; } protected int AA_property3 { private get; set; }
[JsonProperty] [JsonProperty]
private int AA_property4 { get; set; } private int AA_property4 { get; set; }
protected int AA_property5 { get; private set; } protected int AA_property5 { get; private set; }
protected int AA_property6 { private get; set; } protected int AA_property6 { private get; set; }
@@ -2877,21 +2880,27 @@ keyword such as type of business.""
public class BB : AA public class BB : AA
{ {
[JsonProperty] [JsonProperty] public int BB_field1;
public int BB_field1;
public int BB_field2; public int BB_field2;
[JsonProperty] [JsonProperty]
public int BB_property1 { get; set; } public int BB_property1 { get; set; }
[JsonProperty] [JsonProperty]
public int BB_property2 { get; private set; } public int BB_property2 { get; private set; }
[JsonProperty] [JsonProperty]
public int BB_property3 { private get; set; } public int BB_property3 { private get; set; }
[JsonProperty] [JsonProperty]
private int BB_property4 { get; set; } private int BB_property4 { get; set; }
public int BB_property5 { get; private set; } public int BB_property5 { get; private set; }
public int BB_property6 { private get; set; } public int BB_property6 { private get; set; }
[JsonProperty] [JsonProperty]
public int BB_property7 { protected get; set; } public int BB_property7 { protected get; set; }
public int BB_property8 { protected get; set; } public int BB_property8 { protected get; set; }
public BB() public BB()
@@ -3315,6 +3324,7 @@ keyword such as type of business.""
{ {
[JsonIgnore] [JsonIgnore]
public Product Ignored { get; set; } public Product Ignored { get; set; }
public string First { get; set; } public string First { get; set; }
public int Second { get; set; } public int Second { get; set; }
@@ -3439,12 +3449,9 @@ keyword such as type of business.""
set { _nameSpecified = value; } set { _nameSpecified = value; }
} }
[JsonIgnore] [JsonIgnore] public bool WeightSpecified;
public bool WeightSpecified;
[JsonIgnore] [JsonIgnore] [System.Xml.Serialization.XmlIgnoreAttribute] public bool HeightSpecified;
[System.Xml.Serialization.XmlIgnoreAttribute]
public bool HeightSpecified;
[JsonIgnore] [JsonIgnore]
@@ -3548,8 +3555,7 @@ keyword such as type of business.""
public string FirstOrder; public string FirstOrder;
// Use the XmlIgnoreAttribute to ignore the // Use the XmlIgnoreAttribute to ignore the
// special field named "FirstOrderSpecified". // special field named "FirstOrderSpecified".
[System.Xml.Serialization.XmlIgnoreAttribute] [System.Xml.Serialization.XmlIgnoreAttribute] public bool FirstOrderSpecified;
public bool FirstOrderSpecified;
} }
public class FamilyDetails public class FamilyDetails
@@ -3735,6 +3741,7 @@ keyword such as type of business.""
public struct StructWithAttribute public struct StructWithAttribute
{ {
public string MyString { get; set; } public string MyString { get; set; }
[JsonProperty] [JsonProperty]
public int MyInt { get; set; } public int MyInt { get; set; }
} }
@@ -3794,7 +3801,10 @@ keyword such as type of business.""
public class DerivedEvent : LogEvent public class DerivedEvent : LogEvent
{ {
public override string EventName { get { return "derived"; } } public override string EventName
{
get { return "derived"; }
}
} }
[Test] [Test]
@@ -3923,7 +3933,9 @@ keyword such as type of business.""
[JsonProperty] [JsonProperty]
public string IDoWork { get; private set; } public string IDoWork { get; private set; }
private Derived() { } private Derived()
{
}
internal Derived(string dontWork, string doWork) internal Derived(string dontWork, string doWork)
: base(dontWork) : base(dontWork)
@@ -3938,7 +3950,9 @@ keyword such as type of business.""
[JsonProperty] [JsonProperty]
public string IDontWork { get; private set; } public string IDontWork { get; private set; }
protected Base() { } protected Base()
{
}
internal Base(string dontWork) internal Base(string dontWork)
{ {
@@ -4035,7 +4049,7 @@ keyword such as type of business.""
} }
static NullableStructPropertyClass deserialize(string serStr) private static NullableStructPropertyClass deserialize(string serStr)
{ {
return JsonConvert.DeserializeObject<NullableStructPropertyClass>( return JsonConvert.DeserializeObject<NullableStructPropertyClass>(
serStr, serStr,
@@ -4078,7 +4092,10 @@ keyword such as type of business.""
[JsonObject(MemberSerialization.OptIn)] [JsonObject(MemberSerialization.OptIn)]
public class DecimalTest : Test<decimal> public class DecimalTest : Test<decimal>
{ {
protected DecimalTest() { } protected DecimalTest()
{
}
public DecimalTest(decimal val) public DecimalTest(decimal val)
{ {
Value = val; Value = val;
@@ -4294,6 +4311,7 @@ keyword such as type of business.""
get { return null; } get { return null; }
set { } set { }
} }
protected internal object this[object property] protected internal object this[object property]
{ {
get { return null; } get { return null; }
@@ -4329,7 +4347,7 @@ keyword such as type of business.""
Assert.AreEqual(63, commentTestClass.Values.Count); Assert.AreEqual(63, commentTestClass.Values.Count);
} }
class DTOWithParameterisedConstructor private class DTOWithParameterisedConstructor
{ {
public DTOWithParameterisedConstructor(string A) public DTOWithParameterisedConstructor(string A)
{ {
@@ -4341,7 +4359,7 @@ keyword such as type of business.""
public int? B { get; set; } public int? B { get; set; }
} }
class DTOWithoutParameterisedConstructor private class DTOWithoutParameterisedConstructor
{ {
public DTOWithoutParameterisedConstructor() public DTOWithoutParameterisedConstructor()
{ {
@@ -4472,6 +4490,7 @@ keyword such as type of business.""
{ {
public DateTime InterfaceMember { get; set; } public DateTime InterfaceMember { get; set; }
public string NewMember { get; set; } public string NewMember { get; set; }
[JsonProperty(PropertyName = "newMemberWithProperty")] [JsonProperty(PropertyName = "newMemberWithProperty")]
public string NewMemberWithProperty { get; set; } public string NewMemberWithProperty { get; set; }
} }
@@ -4670,12 +4689,9 @@ keyword such as type of business.""
_readonlyString = "default!"; _readonlyString = "default!";
} }
[JsonProperty] [JsonProperty] private string _privateString;
private string _privateString; [JsonProperty] private readonly string _readonlyString;
[JsonProperty] [JsonProperty] internal string _internalString;
private readonly string _readonlyString;
[JsonProperty]
internal string _internalString;
public string UseValue() public string UseValue()
{ {
@@ -4721,27 +4737,19 @@ keyword such as type of business.""
public class BaseType public class BaseType
{ {
[DataMember] [DataMember] public string zebra;
public string zebra;
} }
[DataContract] [DataContract]
public class DerivedType : BaseType public class DerivedType : BaseType
{ {
[DataMember(Order = 0)] [DataMember(Order = 0)] public string bird;
public string bird; [DataMember(Order = 1)] public string parrot;
[DataMember(Order = 1)] [DataMember] public string dog;
public string parrot; [DataMember(Order = 3)] public string antelope;
[DataMember] [DataMember] public string cat;
public string dog; [JsonProperty(Order = 1)] public string albatross;
[DataMember(Order = 3)] [JsonProperty(Order = -2)] public string dinosaur;
public string antelope;
[DataMember]
public string cat;
[JsonProperty(Order = 1)]
public string albatross;
[JsonProperty(Order = -2)]
public string dinosaur;
} }
[Test] [Test]
@@ -4883,6 +4891,25 @@ keyword such as type of business.""
Assert.AreEqual(null, v2["Second"]); Assert.AreEqual(null, v2["Second"]);
Assert.AreEqual(3, v2["Third"]); Assert.AreEqual(3, v2["Third"]);
} }
[Test]
public void UsingJsonTextWriter()
{
// The property of the object has to be a number for the cast exception to occure
object o = new {p = 1};
var json = JObject.FromObject(o);
using (var sw = new StringWriter())
using (var jw = new JsonTextWriter(sw))
{
jw.WriteToken(json.CreateReader());
jw.Flush();
string result = sw.ToString();
Assert.AreEqual(@"{""p"":1}", result);
}
}
} }
public class UriGuidTimeSpanTestClass public class UriGuidTimeSpanTestClass
+3 -3
View File
@@ -353,16 +353,16 @@ namespace Newtonsoft.Json
WriteComment(reader.Value.ToString()); WriteComment(reader.Value.ToString());
break; break;
case JsonToken.Integer: case JsonToken.Integer:
WriteValue((long)reader.Value); WriteValue(Convert.ToInt64(reader.Value));
break; break;
case JsonToken.Float: case JsonToken.Float:
WriteValue((double)reader.Value); WriteValue(Convert.ToDouble(reader.Value));
break; break;
case JsonToken.String: case JsonToken.String:
WriteValue(reader.Value.ToString()); WriteValue(reader.Value.ToString());
break; break;
case JsonToken.Boolean: case JsonToken.Boolean:
WriteValue((bool)reader.Value); WriteValue(Convert.ToBoolean(reader.Value));
break; break;
case JsonToken.Null: case JsonToken.Null:
WriteNull(); WriteNull();