Added opt-in, opt-out serialization option

Added virtual GetSerializableMembers to JsonSerializer
This commit is contained in:
JamesNK
2008-03-29 10:27:44 +00:00
parent 3d150fe2c3
commit e469918596
7 changed files with 202 additions and 10 deletions
@@ -941,5 +941,43 @@ keyword such as type of business.""
StringComparison s = JavaScriptConvert.DeserializeObject<StringComparison>(json);
Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, s);
}
[JsonObject(MemberSerialization.OptIn)]
public class JsonIgnoreAttributeOnClassTestClass
{
private int _property = 21;
private int _ignoredProperty = 12;
[JsonProperty("TheField")]
public int Field;
[JsonProperty]
public int Property
{
get { return _property; }
}
public int IgnoredField;
[JsonProperty]
[JsonIgnore] // JsonIgnore should take priority
public int IgnoredProperty
{
get { return _ignoredProperty; }
}
}
[Test]
public void JsonIgnoreAttributeOnClassTest()
{
string json = JavaScriptConvert.SerializeObject(new JsonIgnoreAttributeOnClassTestClass());
Assert.AreEqual(@"{""TheField"":0,""Property"":21}", json);
JsonIgnoreAttributeOnClassTestClass c = JavaScriptConvert.DeserializeObject<JsonIgnoreAttributeOnClassTestClass>(@"{""TheField"":99,""Property"":-1,""IgnoredField"":-1}");
Assert.AreEqual(0, c.IgnoredField);
Assert.AreEqual(99, c.Field);
}
}
}
@@ -347,7 +347,7 @@ namespace Newtonsoft.Json.Tests
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties.")]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document.")]
public void RootObjectMultipleProperties()
{
XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeserializeXmlNode(@"{Prop1:1,Prop2:2}");
@@ -414,5 +414,45 @@ namespace Newtonsoft.Json.Tests
arrayJsonText = JavaScriptConvert.SerializeXmlNode(arrayDoc);
Assert.AreEqual(@"{""root"":{""person"":{""@id"":""1"",""name"":""Alan"",""url"":""http://www.google.com"",""role"":""Admin1""}}}", arrayJsonText);
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document.")]
public void MultipleRootProperties()
{
string strJSON = @"{
""count"": 773840,""photos"": [
{
""photo_id"": 532693,
""photo_title"": ""Wheatfield in afternoon light"",
""photo_url"": ""http://www.panoramio.com/photo/532693"",
""photofileurl"": ""http://static2.bareka.com/photos/medium/532693.jpg"",
""longitude"": 11.280727,
""latitude"": 59.643198,
""width"": 500,
""height"": 333,
""upload_date"": ""22 January 2007"",
""owner_id"": 39160,
""owner_name"": ""Snemann"",
""owner_url"": ""http://www.panoramio.com/user/39160"",
},
{
""photo_id"": 505229,
""photo_title"": ""Etangs près de Dijon"",
""photo_url"": ""http://www.panoramio.com/photo/505229"",
""photofileurl"": ""http://static2.bareka.com/photos/medium/505229.jpg"",
""longitude"": 5.168552,
""latitude"": 47.312642,
""width"": 350,
""height"": 500,
""upload_date"": ""20 January 2007"",
""owner_id"": 78506,
""owner_name"": ""Philippe Stoop"",
""owner_url"": ""http://www.panoramio.com/user/78506""
}
]
}";
XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeserializeXmlNode(strJSON);
}
}
}