Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated to 4.5.4.14907.

  • Loading branch information...
commit 3e698fb084a8be5b24ec121c6dafe6b71a9dcae7 1 parent 7e40746
@attilah authored
Showing with 7,748 additions and 1,196 deletions.
  1. +50 −0 Src/Newtonsoft.Json.All.sln
  2. +5 −5 Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
  3. +29 −0 Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs
  4. +28 −0 Src/Newtonsoft.Json.Tests/JsonConvertTest.cs
  5. +1 −1  Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs
  6. +3 −3 Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
  7. +17 −17 Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs
  8. +1 −0  Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Metro.csproj
  9. +6 −6 Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj
  10. +34 −27 Src/Newtonsoft.Json.Tests/PerformanceTests.cs
  11. +1 −1  Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs
  12. +27 −0 Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs
  13. +448 −290 Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
  14. +70 −2 Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs
  15. +56 −0 Src/Newtonsoft.Json.Tests/Serialization/ReferenceLoopHandlingTests.cs
  16. +529 −175 Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
  17. +8 −8 Src/Newtonsoft.Json/Bson/BsonWriter.cs
  18. +1 −1  Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs
  19. +19 −0 Src/Newtonsoft.Json/JsonException.cs
  20. +38 −5 Src/Newtonsoft.Json/JsonPropertyAttribute.cs
  21. +0 −18 Src/Newtonsoft.Json/JsonReaderException.cs
  22. +1 −1  Src/Newtonsoft.Json/JsonSerializationException.cs
  23. +1 −1  Src/Newtonsoft.Json/JsonTextWriter.cs
  24. +13 −15 Src/Newtonsoft.Json/JsonWriter.cs
  25. +24 −0 Src/Newtonsoft.Json/JsonWriterException.cs
  26. +1 −1  Src/Newtonsoft.Json/Linq/IJEnumerable.cs
  27. +18 −15 Src/Newtonsoft.Json/Linq/JContainer.cs
  28. +8 −8 Src/Newtonsoft.Json/Linq/JObject.cs
  29. +3 −3 Src/Newtonsoft.Json/Linq/JToken.cs
  30. +3 −3 Src/Newtonsoft.Json/Linq/JValue.cs
  31. +80 −83 Src/Newtonsoft.Json/Newtonsoft.Json.MonoDroid.csproj
  32. +78 −85 Src/Newtonsoft.Json/Newtonsoft.Json.MonoTouch.csproj
  33. +3 −0  Src/Newtonsoft.Json/Newtonsoft.Json.csproj
  34. +3 −3 Src/Newtonsoft.Json/Properties/AssemblyInfo.cs
  35. +1 −1  Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs
  36. +21 −12 Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
  37. +1 −1  Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs
  38. +0 −2  Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs
  39. +27 −0 Src/Newtonsoft.Json/Serialization/JsonContainerContract.cs
  40. +1 −1  Src/Newtonsoft.Json/Serialization/JsonContract.cs
  41. +1 −2  Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs
  42. +1 −1  Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs
  43. +38 −0 Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs
  44. +31 −1 Src/Newtonsoft.Json/Serialization/JsonProperty.cs
  45. +2 −1  Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs
  46. +365 −271 Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
  47. +104 −98 Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
  48. +6 −6 Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs
  49. +2 −18 Src/Newtonsoft.Json/Utilities/CollectionUtils.cs
  50. +1 −1  Src/Newtonsoft.Json/Utilities/DynamicProxy.cs
  51. +1 −1  Src/Newtonsoft.Json/Utilities/DynamicProxyMetaObject.cs
  52. +1 −1  Src/Newtonsoft.Json/Utilities/DynamicUtils.cs
  53. +1 −1  Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
  54. +15 −0 Src/Newtonsoft.Json/Utilities/StringUtils.cs
  55. +5,521 −0 Src/Newtonsoft.Json/nunit.framework.xml
View
50 Src/Newtonsoft.Json.All.sln
@@ -0,0 +1,50 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lib", "Lib", "{620042D9-2753-48F5-BEDE-3905248781D2}"
+ ProjectSection(SolutionItems) = preProject
+ Lib\NUnit\DotNet\nunit.framework.dll = Lib\NUnit\DotNet\nunit.framework.dll
+ Lib\NUnit\DotNet\nunit.framework.xml = Lib\NUnit\DotNet\nunit.framework.xml
+ EndProjectSection
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json", "Newtonsoft.Json\Newtonsoft.Json.csproj", "{A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Tests", "Newtonsoft.Json.Tests\Newtonsoft.Json.Tests.csproj", "{3E6E2335-B079-4B5B-A65A-9D586914BCBB}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.MonoDroid", "Newtonsoft.Json\Newtonsoft.Json.MonoDroid.csproj", "{C070929C-A786-4FBE-A2D1-FF69F4EF3F6D}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.MonoTouch", "Newtonsoft.Json\Newtonsoft.Json.MonoTouch.csproj", "{12907C78-F5AB-46CA-8BB8-888E26ECFF72}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Portable", "Newtonsoft.Json\Newtonsoft.Json.Portable.csproj", "{959F7F85-C98B-4876-971A-9036224578E4}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}.Release|Any CPU.Build.0 = Release|Any CPU
+ {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Release|Any CPU.Build.0 = Release|Any CPU
+ {C070929C-A786-4FBE-A2D1-FF69F4EF3F6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {C070929C-A786-4FBE-A2D1-FF69F4EF3F6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {C070929C-A786-4FBE-A2D1-FF69F4EF3F6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {C070929C-A786-4FBE-A2D1-FF69F4EF3F6D}.Release|Any CPU.Build.0 = Release|Any CPU
+ {12907C78-F5AB-46CA-8BB8-888E26ECFF72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {12907C78-F5AB-46CA-8BB8-888E26ECFF72}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {12907C78-F5AB-46CA-8BB8-888E26ECFF72}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {12907C78-F5AB-46CA-8BB8-888E26ECFF72}.Release|Any CPU.Build.0 = Release|Any CPU
+ {959F7F85-C98B-4876-971A-9036224578E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {959F7F85-C98B-4876-971A-9036224578E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {959F7F85-C98B-4876-971A-9036224578E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {959F7F85-C98B-4876-971A-9036224578E4}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
View
10 Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
@@ -378,7 +378,7 @@ public void WriteEmptyStrings()
[Test]
public void WriteComment()
{
- ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON comment as BSON.",
+ ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON comment as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
@@ -392,7 +392,7 @@ public void WriteComment()
[Test]
public void WriteConstructor()
{
- ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON constructor as BSON.",
+ ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON constructor as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
@@ -406,7 +406,7 @@ public void WriteConstructor()
[Test]
public void WriteRaw()
{
- ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON.",
+ ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
@@ -420,7 +420,7 @@ public void WriteRaw()
[Test]
public void WriteRawValue()
{
- ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON.",
+ ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON. Path ''.",
() =>
{
MemoryStream ms = new MemoryStream();
@@ -652,7 +652,7 @@ public void WriteDateTimes()
[Test]
public void WriteValueOutsideOfObjectOrArray()
{
- ExceptionAssert.Throws<JsonWriterException>("Error writing String value. BSON must start with an Object or Array.",
+ ExceptionAssert.Throws<JsonWriterException>("Error writing String value. BSON must start with an Object or Array. Path ''.",
() =>
{
MemoryStream stream = new MemoryStream();
View
29 Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs
@@ -114,6 +114,35 @@ private XmlNode DeserializeXmlNode(string json, string deserializeRootElementNam
return node;
}
+#if !NET20
+ [Test]
+ public void SerializeEmptyDocument()
+ {
+ XmlDocument doc = new XmlDocument();
+ doc.LoadXml("<root />");
+
+ string json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
+ Assert.AreEqual("null", json);
+
+ doc = new XmlDocument();
+ doc.LoadXml("<root></root>");
+
+ json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
+ Assert.AreEqual("null", json);
+
+
+ XDocument doc1 = XDocument.Parse("<root />");
+
+ json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
+ Assert.AreEqual("null", json);
+
+ doc1 = XDocument.Parse("<root></root>");
+
+ json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
+ Assert.AreEqual("null", json);
+ }
+#endif
+
[Test]
public void DocumentSerializeIndented()
{
View
28 Src/Newtonsoft.Json.Tests/JsonConvertTest.cs
@@ -721,5 +721,33 @@ public void SerializeObjectDateTimeZoneHandling()
Assert.AreEqual(@"""2000-01-01T01:01:01Z""", json);
}
+
+ //[Test]
+ public void StackOverflowTest()
+ {
+ StringBuilder sb = new StringBuilder();
+
+ int depth = 900;
+ for (int i = 0; i < depth; i++)
+ {
+ sb.Append("{'A':");
+ }
+
+ // invalid json
+ sb.Append("{***}");
+ for (int i = 0; i < depth; i++)
+ {
+ sb.Append("}");
+ }
+
+ string json = sb.ToString();
+ JsonSerializer serializer = new JsonSerializer() { };
+ serializer.Deserialize<Nest>(new JsonTextReader(new StringReader(json)));
+ }
+
+ public class Nest
+ {
+ public Nest A { get; set; }
+ }
}
}
View
2  Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs
@@ -2513,7 +2513,7 @@ public void ResetJsonTextReaderErrorCount()
toggleReaderError.Error = false;
Assert.IsTrue(jsonTextReader.Read());
- Assert.AreEqual(1, jsonTextReader.Value);
+ Assert.AreEqual(1L, jsonTextReader.Value);
toggleReaderError.Error = true;
View
6 Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
@@ -174,8 +174,8 @@ public void WriteValueObjectWithNullable()
[Test]
public void WriteValueObjectWithUnsupportedValue()
{
- ExceptionAssert.Throws<ArgumentException>(
- @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.",
+ ExceptionAssert.Throws<JsonWriterException>(
+ @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation. Path ''.",
() =>
{
StringWriter sw = new StringWriter();
@@ -597,7 +597,7 @@ public void WriteFloatingPointNumber()
public void BadWriteEndArray()
{
ExceptionAssert.Throws<JsonWriterException>(
- "No token to close.",
+ "No token to close. Path ''.",
() =>
{
StringBuilder sb = new StringBuilder();
View
34 Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs
@@ -712,27 +712,27 @@ public void ImprovedDynamicLinqExample()
""StockValue"": 22050.0
}", json);
}
+ }
- public class DynamicDictionary : DynamicObject
- {
- private readonly IDictionary<string, object> _values = new Dictionary<string, object>();
+ public class DynamicDictionary : DynamicObject
+ {
+ private readonly IDictionary<string, object> _values = new Dictionary<string, object>();
- public override IEnumerable<string> GetDynamicMemberNames()
- {
- return _values.Keys;
- }
+ public override IEnumerable<string> GetDynamicMemberNames()
+ {
+ return _values.Keys;
+ }
- public override bool TryGetMember(GetMemberBinder binder, out object result)
- {
- result = _values[binder.Name];
- return true;
- }
+ public override bool TryGetMember(GetMemberBinder binder, out object result)
+ {
+ result = _values[binder.Name];
+ return true;
+ }
- public override bool TrySetMember(SetMemberBinder binder, object value)
- {
- _values[binder.Name] = value;
- return true;
- }
+ public override bool TrySetMember(SetMemberBinder binder, object value)
+ {
+ _values[binder.Name] = value;
+ return true;
}
}
}
View
1  Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Metro.csproj
@@ -169,6 +169,7 @@
<Compile Include="Serialization\NullValueHandlingTests.cs" />
<Compile Include="Serialization\PopulateTests.cs" />
<Compile Include="Serialization\PreserveReferencesHandlingTests.cs" />
+ <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\SerializationErrorHandlingTests.cs" />
<Compile Include="Serialization\SerializationEventAttributeTests.cs" />
<Compile Include="Serialization\TypeNameHandlingTests.cs" />
View
12 Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj
@@ -254,12 +254,6 @@
<Compile Include="Utilities\ReflectionUtilsTests.cs" />
</ItemGroup>
<ItemGroup>
- <ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.csproj">
- <Project>{A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}</Project>
- <Name>Newtonsoft.Json</Name>
- </ProjectReference>
- </ItemGroup>
- <ItemGroup>
<None Include="LinqToSql\LinqToSqlClasses.dbml">
<Generator>MSLinqToSQLGenerator</Generator>
<LastGenOutput>LinqToSqlClasses.designer.cs</LastGenOutput>
@@ -286,6 +280,12 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.csproj">
+ <Project>{A9AE40FF-1A21-414A-9FE7-3BE13644CC6D}</Project>
+ <Name>Newtonsoft.Json</Name>
+ </ProjectReference>
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
View
61 Src/Newtonsoft.Json.Tests/PerformanceTests.cs
@@ -93,6 +93,7 @@ public enum SerializeMethod
JsonNet,
JsonNetWithIsoConverter,
JsonNetBinary,
+ JsonNetLinq,
BinaryFormatter,
JavaScriptSerializer,
DataContractSerializer,
@@ -130,6 +131,7 @@ private void SerializeTests(object value)
BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, value);
BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, value);
BenchmarkSerializeMethod(SerializeMethod.JsonNet, value);
+ BenchmarkSerializeMethod(SerializeMethod.JsonNetLinq, value);
BenchmarkSerializeMethod(SerializeMethod.JsonNetWithIsoConverter, value);
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, value);
}
@@ -428,33 +430,6 @@ private static SimpleObject CreateSimpleObject()
};
}
- public string SerializeJsonNet(object value)
- {
- Type type = value.GetType();
-
- Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
-
- json.NullValueHandling = NullValueHandling.Ignore;
-
- json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
- json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
- json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
-
-
- StringWriter sw = new StringWriter();
- Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
-
- writer.Formatting = Formatting.None;
-
- writer.QuoteChar = '"';
- json.Serialize(writer, value);
-
- string output = sw.ToString();
- writer.Close();
-
- return output;
- }
-
public string SerializeWebExtensions(object value)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
@@ -506,6 +481,38 @@ private string Serialize(SerializeMethod method, object value)
case SerializeMethod.JsonNetWithIsoConverter:
json = JsonConvert.SerializeObject(value, new IsoDateTimeConverter());
break;
+ case SerializeMethod.JsonNetLinq:
+ {
+ TestClass c = value as TestClass;
+ if (c != null)
+ {
+ JObject o = new JObject(
+ new JProperty("strings", new JArray(
+ c.strings
+ )),
+ new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))),
+ new JProperty("Name", c.Name),
+ new JProperty("Now", c.Now),
+ new JProperty("BigNumber", c.BigNumber),
+ new JProperty("Address1", new JObject(
+ new JProperty("Street", c.Address1.Street),
+ new JProperty("Phone", c.Address1.Phone),
+ new JProperty("Entered", c.Address1.Entered))),
+ new JProperty("Addresses", new JArray(c.Addresses.Select(a =>
+ new JObject(
+ new JProperty("Street", a.Street),
+ new JProperty("Phone", a.Phone),
+ new JProperty("Entered", a.Entered)))))
+ );
+
+ json = o.ToString(Formatting.None);
+ }
+ else
+ {
+ json = string.Empty;
+ }
+ break;
+ }
case SerializeMethod.JsonNetBinary:
{
MemoryStream ms = new MemoryStream(Buffer);
View
2  Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs
@@ -72,5 +72,5 @@
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
-[assembly: AssemblyFileVersion("4.5.4.14902")]
+[assembly: AssemblyFileVersion("4.5.4.14907")]
#endif
View
27 Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs
@@ -751,6 +751,33 @@ public void JsonPropertyWithHandlingValues()
}
}", json);
}
+
+ [Test]
+ public void GenerateForNullableInt32()
+ {
+ JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
+
+ JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(NullableInt32TestClass));
+ string json = jsonSchema.ToString();
+
+ Assert.AreEqual(@"{
+ ""type"": ""object"",
+ ""properties"": {
+ ""Value"": {
+ ""required"": true,
+ ""type"": [
+ ""integer"",
+ ""null""
+ ]
+ }
+ }
+}", json);
+ }
+ }
+
+ public class NullableInt32TestClass
+ {
+ public int? Value { get; set; }
}
public class DMDSLBase
View
738 Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -93,7 +93,7 @@ public void PersonTypedObjectDeserialization()
string jsonText = JsonConvert.SerializeObject(store);
- Store deserializedStore = (Store) JsonConvert.DeserializeObject(jsonText, typeof (Store));
+ Store deserializedStore = (Store)JsonConvert.DeserializeObject(jsonText, typeof(Store));
Assert.AreEqual(store.Establised, deserializedStore.Establised);
Assert.AreEqual(store.product.Count, deserializedStore.product.Count);
@@ -109,7 +109,7 @@ public void TypedObjectDeserialization()
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
- product.Sizes = new string[] {"Small", "Medium", "Large"};
+ product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
@@ -123,7 +123,7 @@ public void TypedObjectDeserialization()
// ]
//}
- Product deserializedProduct = (Product) JsonConvert.DeserializeObject(output, typeof (Product));
+ Product deserializedProduct = (Product)JsonConvert.DeserializeObject(output, typeof(Product));
Assert.AreEqual("Apple", deserializedProduct.Name);
Assert.AreEqual(new DateTime(2008, 12, 28), deserializedProduct.ExpiryDate);
@@ -156,7 +156,7 @@ public void JsonConvertSerializer()
{
string value = @"{""Name"":""Orange"", ""Price"":3.99, ""ExpiryDate"":""01/24/2010 12:00:00""}";
- Product p = JsonConvert.DeserializeObject(value, typeof (Product)) as Product;
+ Product p = JsonConvert.DeserializeObject(value, typeof(Product)) as Product;
Assert.AreEqual("Orange", p.Name);
Assert.AreEqual(new DateTime(2010, 1, 24, 12, 0, 0), p.ExpiryDate);
@@ -174,15 +174,15 @@ public void DeserializeJavaScriptDate()
#if !PocketPC && !NET20 && !WINDOWS_PHONE
MemoryStream ms = new MemoryStream();
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (Dictionary<string, object>));
+ DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary<string, object>));
serializer.WriteObject(ms, testDictionary);
byte[] data = ms.ToArray();
string output = Encoding.UTF8.GetString(data, 0, data.Length);
#endif
- Dictionary<string, object> deserializedDictionary = (Dictionary<string, object>) JsonConvert.DeserializeObject(jsonText, typeof (Dictionary<string, object>));
- DateTime deserializedDate = (DateTime) deserializedDictionary["date"];
+ Dictionary<string, object> deserializedDictionary = (Dictionary<string, object>)JsonConvert.DeserializeObject(jsonText, typeof(Dictionary<string, object>));
+ DateTime deserializedDate = (DateTime)deserializedDictionary["date"];
Assert.AreEqual(dateValue, deserializedDate);
}
@@ -192,12 +192,12 @@ public void TestMethodExecutorObject()
{
MethodExecutorObject executorObject = new MethodExecutorObject();
executorObject.serverClassName = "BanSubs";
- executorObject.serverMethodParams = new object[] {"21321546", "101", "1236", "D:\\1.txt"};
+ executorObject.serverMethodParams = new object[] { "21321546", "101", "1236", "D:\\1.txt" };
executorObject.clientGetResultFunction = "ClientBanSubsCB";
string output = JsonConvert.SerializeObject(executorObject);
- MethodExecutorObject executorObject2 = JsonConvert.DeserializeObject(output, typeof (MethodExecutorObject)) as MethodExecutorObject;
+ MethodExecutorObject executorObject2 = JsonConvert.DeserializeObject(output, typeof(MethodExecutorObject)) as MethodExecutorObject;
Assert.AreNotSame(executorObject, executorObject2);
Assert.AreEqual(executorObject2.serverClassName, "BanSubs");
@@ -212,7 +212,7 @@ public void HashtableDeserialization()
{
string value = @"{""Name"":""Orange"", ""Price"":3.99, ""ExpiryDate"":""01/24/2010 12:00:00""}";
- Hashtable p = JsonConvert.DeserializeObject(value, typeof (Hashtable)) as Hashtable;
+ Hashtable p = JsonConvert.DeserializeObject(value, typeof(Hashtable)) as Hashtable;
Assert.AreEqual("Orange", p["Name"].ToString());
}
@@ -222,7 +222,7 @@ public void TypedHashtableDeserialization()
{
string value = @"{""Name"":""Orange"", ""Hash"":{""ExpiryDate"":""01/24/2010 12:00:00"",""UntypedArray"":[""01/24/2010 12:00:00""]}}";
- TypedSubHashtable p = JsonConvert.DeserializeObject(value, typeof (TypedSubHashtable)) as TypedSubHashtable;
+ TypedSubHashtable p = JsonConvert.DeserializeObject(value, typeof(TypedSubHashtable)) as TypedSubHashtable;
Assert.AreEqual("01/24/2010 12:00:00", p.Hash["ExpiryDate"].ToString());
Assert.AreEqual(@"[
@@ -365,11 +365,11 @@ public void TorrentDeserializeTest()
""torrentc"": ""1816000723""
}";
- JObject o = (JObject) JsonConvert.DeserializeObject(jsonText);
+ JObject o = (JObject)JsonConvert.DeserializeObject(jsonText);
Assert.AreEqual(4, o.Children().Count());
- JToken torrentsArray = (JToken) o["torrents"];
- JToken nestedTorrentsArray = (JToken) torrentsArray[0];
+ JToken torrentsArray = (JToken)o["torrents"];
+ JToken nestedTorrentsArray = (JToken)torrentsArray[0];
Assert.AreEqual(nestedTorrentsArray.Children().Count(), 19);
}
@@ -422,7 +422,7 @@ public void InheritedListSerialize()
[Test]
public void ReadOnlyCollectionSerialize()
{
- ReadOnlyCollection<int> r1 = new ReadOnlyCollection<int>(new int[] {0, 1, 2, 3, 4});
+ ReadOnlyCollection<int> r1 = new ReadOnlyCollection<int>(new int[] { 0, 1, 2, 3, 4 });
string jsonText = JsonConvert.SerializeObject(r1);
@@ -437,8 +437,8 @@ public void Unicode()
{
string json = @"[""PRE\u003cPOST""]";
- DataContractJsonSerializer s = new DataContractJsonSerializer(typeof (List<string>));
- List<string> dataContractResult = (List<string>) s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
+ DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List<string>));
+ List<string> dataContractResult = (List<string>)s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
List<string> jsonNetResult = JsonConvert.DeserializeObject<List<string>>(json);
@@ -456,8 +456,8 @@ public void BackslashEqivilence()
List<string> javaScriptSerializerResult = javaScriptSerializer.Deserialize<List<string>>(json);
#endif
- DataContractJsonSerializer s = new DataContractJsonSerializer(typeof (List<string>));
- List<string> dataContractResult = (List<string>) s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
+ DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List<string>));
+ List<string> dataContractResult = (List<string>)s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
List<string> jsonNetResult = JsonConvert.DeserializeObject<List<string>>(json);
@@ -476,9 +476,9 @@ public void InvalidBackslash()
ExceptionAssert.Throws<JsonReaderException>(
@"Bad JSON escape sequence: \j. Path '', line 1, position 7.",
() =>
- {
- JsonConvert.DeserializeObject<List<string>>(json);
- });
+ {
+ JsonConvert.DeserializeObject<List<string>>(json);
+ });
}
[Test]
@@ -495,7 +495,7 @@ public void DateTimeTest()
};
MemoryStream ms = new MemoryStream();
- DataContractJsonSerializer s = new DataContractJsonSerializer(typeof (List<DateTime>));
+ DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List<DateTime>));
s.WriteObject(ms, testDates);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
@@ -561,9 +561,9 @@ public void AnonymousObjectSerialization()
{
StringValue = "I am a string",
IntValue = int.MaxValue,
- NestedAnonymous = new {NestedValue = byte.MaxValue},
- NestedArray = new[] {1, 2},
- Product = new Product() {Name = "TestProduct"}
+ NestedAnonymous = new { NestedValue = byte.MaxValue },
+ NestedArray = new[] { 1, 2 },
+ Product = new Product() { Name = "TestProduct" }
};
string json = JsonConvert.SerializeObject(anonymous);
@@ -599,7 +599,7 @@ public void CustomCollectionSerialization()
Assert.AreEqual(@"[{""Name"":""Test1"",""ExpiryDate"":""2000-01-01T00:00:00Z"",""Price"":0.0,""Sizes"":null},{""Name"":""Test2"",""ExpiryDate"":""2000-01-01T00:00:00Z"",""Price"":0.0,""Sizes"":null},{""Name"":""Test3"",""ExpiryDate"":""2000-01-01T00:00:00Z"",""Price"":0.0,""Sizes"":null}]",
sw.GetStringBuilder().ToString());
- ProductCollection collectionNew = (ProductCollection) jsonSerializer.Deserialize(new JsonTextReader(new StringReader(sw.GetStringBuilder().ToString())), typeof (ProductCollection));
+ ProductCollection collectionNew = (ProductCollection)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(sw.GetStringBuilder().ToString())), typeof(ProductCollection));
CollectionAssert.AreEqual(collection, collectionNew);
}
@@ -654,7 +654,7 @@ public void ObjectCreationHandlingReplace()
JsonSerializer s = new JsonSerializer();
s.ObjectCreationHandling = ObjectCreationHandling.Replace;
- ClassWithArray wibble = (ClassWithArray) s.Deserialize(new StringReader(json), typeof (ClassWithArray));
+ ClassWithArray wibble = (ClassWithArray)s.Deserialize(new StringReader(json), typeof(ClassWithArray));
Assert.AreEqual("hello", wibble.Foo);
@@ -750,7 +750,7 @@ public void GuidTest()
{
Guid guid = new Guid("BED7F4EA-1A96-11d2-8F08-00A0C9A6186D");
- string json = JsonConvert.SerializeObject(new ClassWithGuid {GuidField = guid});
+ string json = JsonConvert.SerializeObject(new ClassWithGuid { GuidField = guid });
Assert.AreEqual(@"{""GuidField"":""bed7f4ea-1a96-11d2-8f08-00a0c9a6186d""}", json);
ClassWithGuid c = JsonConvert.DeserializeObject<ClassWithGuid>(json);
@@ -777,7 +777,7 @@ public void TimeSpanTest()
{
TimeSpan ts = new TimeSpan(00, 23, 59, 1);
- string json = JsonConvert.SerializeObject(new ClassWithTimeSpan {TimeSpanField = ts}, Formatting.Indented);
+ string json = JsonConvert.SerializeObject(new ClassWithTimeSpan { TimeSpanField = ts }, Formatting.Indented);
Assert.AreEqual(@"{
""TimeSpanField"": ""23:59:01""
}", json);
@@ -807,8 +807,8 @@ public void SerializeArrayAsArrayList()
ArrayList o = JsonConvert.DeserializeObject<ArrayList>(jsonText);
Assert.AreEqual(4, o.Count);
- Assert.AreEqual(3, ((JArray) o[2]).Count);
- Assert.AreEqual(0, ((JObject) o[3]).Count);
+ Assert.AreEqual(3, ((JArray)o[2]).Count);
+ Assert.AreEqual(0, ((JObject)o[3]).Count);
}
#endif
@@ -891,7 +891,7 @@ public void SerializerShouldUseClassConverterOverArgumentConverter()
public void SerializerShouldUseMemberConverter_IsoDate()
{
DateTime testDate = new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc);
- MemberConverterClass m1 = new MemberConverterClass {DefaultConverter = testDate, MemberConverter = testDate};
+ MemberConverterClass m1 = new MemberConverterClass { DefaultConverter = testDate, MemberConverter = testDate };
string json = JsonConvert.SerializeObject(m1);
Assert.AreEqual(@"{""DefaultConverter"":""1970-01-01T00:00:00Z"",""MemberConverter"":""1970-01-01T00:00:00Z""}", json);
@@ -965,7 +965,7 @@ public void SerializerShouldUseMemberConverter_IsoDate_DateParseNone()
public void SerializerShouldUseMemberConverterOverArgumentConverter()
{
DateTime testDate = new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc);
- MemberConverterClass m1 = new MemberConverterClass {DefaultConverter = testDate, MemberConverter = testDate};
+ MemberConverterClass m1 = new MemberConverterClass { DefaultConverter = testDate, MemberConverter = testDate };
string json = JsonConvert.SerializeObject(m1, new JavaScriptDateTimeConverter());
Assert.AreEqual(@"{""DefaultConverter"":new Date(0),""MemberConverter"":""1970-01-01T00:00:00Z""}", json);
@@ -1036,10 +1036,10 @@ public void DeserializeNullable()
{
string json;
- json = JsonConvert.SerializeObject((int?) null);
+ json = JsonConvert.SerializeObject((int?)null);
Assert.AreEqual("null", json);
- json = JsonConvert.SerializeObject((int?) 1);
+ json = JsonConvert.SerializeObject((int?)1);
Assert.AreEqual("1", json);
}
@@ -1113,23 +1113,23 @@ public void DeserializeInt64ToNullableDouble()
[Test]
public void SerializeTypeProperty()
{
- string boolRef = typeof (bool).AssemblyQualifiedName;
- TypeClass typeClass = new TypeClass {TypeProperty = typeof (bool)};
+ string boolRef = typeof(bool).AssemblyQualifiedName;
+ TypeClass typeClass = new TypeClass { TypeProperty = typeof(bool) };
string json = JsonConvert.SerializeObject(typeClass);
Assert.AreEqual(@"{""TypeProperty"":""" + boolRef + @"""}", json);
TypeClass typeClass2 = JsonConvert.DeserializeObject<TypeClass>(json);
- Assert.AreEqual(typeof (bool), typeClass2.TypeProperty);
+ Assert.AreEqual(typeof(bool), typeClass2.TypeProperty);
- string jsonSerializerTestRef = typeof (JsonSerializerTest).AssemblyQualifiedName;
- typeClass = new TypeClass {TypeProperty = typeof (JsonSerializerTest)};
+ string jsonSerializerTestRef = typeof(JsonSerializerTest).AssemblyQualifiedName;
+ typeClass = new TypeClass { TypeProperty = typeof(JsonSerializerTest) };
json = JsonConvert.SerializeObject(typeClass);
Assert.AreEqual(@"{""TypeProperty"":""" + jsonSerializerTestRef + @"""}", json);
typeClass2 = JsonConvert.DeserializeObject<TypeClass>(json);
- Assert.AreEqual(typeof (JsonSerializerTest), typeClass2.TypeProperty);
+ Assert.AreEqual(typeof(JsonSerializerTest), typeClass2.TypeProperty);
}
[Test]
@@ -1181,36 +1181,36 @@ public void DeserializeRequiredMembersClassNullRequiredValueProperty()
ExceptionAssert.Throws<JsonSerializationException>(
"Required property 'FirstName' expects a value but got null. Path '', line 6, position 2.",
() =>
- {
- string json = @"{
+ {
+ string json = @"{
""FirstName"": null,
""MiddleName"": null,
""LastName"": null,
""BirthDate"": ""\/Date(977309755000)\/""
}";
- JsonConvert.DeserializeObject<RequiredMembersClass>(json);
- });
+ JsonConvert.DeserializeObject<RequiredMembersClass>(json);
+ });
}
[Test]
public void SerializeRequiredMembersClassNullRequiredValueProperty()
{
ExceptionAssert.Throws<JsonSerializationException>(
- "Cannot write a null value for property 'FirstName'. Property requires a value.",
+ "Cannot write a null value for property 'FirstName'. Property requires a value. Path ''.",
() =>
- {
- RequiredMembersClass requiredMembersClass = new RequiredMembersClass
- {
- FirstName = null,
- BirthDate = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc),
- LastName = null,
- MiddleName = null
- };
-
- string json = JsonConvert.SerializeObject(requiredMembersClass);
- Console.WriteLine(json);
- });
+ {
+ RequiredMembersClass requiredMembersClass = new RequiredMembersClass
+ {
+ FirstName = null,
+ BirthDate = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc),
+ LastName = null,
+ MiddleName = null
+ };
+
+ string json = JsonConvert.SerializeObject(requiredMembersClass);
+ Console.WriteLine(json);
+ });
}
[Test]
@@ -1219,13 +1219,13 @@ public void RequiredMembersClassMissingRequiredProperty()
ExceptionAssert.Throws<JsonSerializationException>(
"Required property 'LastName' not found in JSON. Path '', line 3, position 2.",
() =>
- {
- string json = @"{
+ {
+ string json = @"{
""FirstName"": ""Bob""
}";
- JsonConvert.DeserializeObject<RequiredMembersClass>(json);
- });
+ JsonConvert.DeserializeObject<RequiredMembersClass>(json);
+ });
}
[Test]
@@ -1234,7 +1234,7 @@ public void SerializeJaggedArray()
JaggedArray aa = new JaggedArray();
aa.Before = "Before!";
aa.After = "After!";
- aa.Coordinates = new[] {new[] {1, 1}, new[] {1, 2}, new[] {2, 1}, new[] {2, 2}};
+ aa.Coordinates = new[] { new[] { 1, 1 }, new[] { 1, 2 }, new[] { 2, 1 }, new[] { 2, 2 } };
string json = JsonConvert.SerializeObject(aa);
@@ -1313,9 +1313,9 @@ public void DeserializeInterfaceProperty()
ExceptionAssert.Throws<JsonSerializationException>(
@"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantated. Path 'co.Name', line 1, position 14.",
() =>
- {
- InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass) JsonConvert.DeserializeObject(strFromTest, typeof (InterfacePropertyTestClass));
- });
+ {
+ InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass)JsonConvert.DeserializeObject(strFromTest, typeof(InterfacePropertyTestClass));
+ });
}
private Person GetPerson()
@@ -1453,7 +1453,7 @@ public void SerializeJsonPropertyWithHandlingValues()
""ReferenceLoopHandlingSerializeProperty"": null
}", json);
- json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
+ json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Assert.AreEqual(@"{
""DefaultValueHandlingIncludeProperty"": ""Default!"",
@@ -1480,7 +1480,7 @@ public void JsonPropertyWithHandlingValues_ReferenceLoopError()
string classRef = typeof(JsonPropertyWithHandlingValues).FullName;
ExceptionAssert.Throws<JsonSerializationException>(
- @"Self referencing loop detected for type '" + classRef + "'.",
+ "Self referencing loop detected for property 'ReferenceLoopHandlingErrorProperty' with type '" + classRef + "'. Path ''.",
() =>
{
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
@@ -1523,7 +1523,7 @@ public void PartialClassDeserialize()
}
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
- [MetadataType(typeof (OptInClassMetadata))]
+ [MetadataType(typeof(OptInClassMetadata))]
public class OptInClass
{
[DataContract]
@@ -1585,7 +1585,8 @@ public DataContractPrivateMembers(string name, int age, int rank, string title)
Title = title;
}
- [DataMember] private string _name;
+ [DataMember]
+ private string _name;
[DataMember(Name = "_age")]
private int Age { get; set; }
@@ -1636,7 +1637,7 @@ public void DeserializeDictionaryInterface()
DictionaryInterfaceClass c = JsonConvert.DeserializeObject<DictionaryInterfaceClass>(
json,
- new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace});
+ new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace });
Assert.AreEqual("Name!", c.Name);
Assert.AreEqual(1, c.Dictionary.Count);
@@ -1666,7 +1667,7 @@ public void DeserializeDictionaryInterfaceWithExistingValues()
}";
DictionaryInterfaceClass c = JsonConvert.DeserializeObject<DictionaryInterfaceClass>(json,
- new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Reuse});
+ new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Reuse });
Assert.AreEqual("Name!", c.Name);
Assert.AreEqual(3, c.Dictionary.Count);
@@ -1694,7 +1695,7 @@ public void TypedObjectDeserializationWithComments()
/*comment*/ ] /*comment*/
} /*comment*/";
- Product deserializedProduct = (Product) JsonConvert.DeserializeObject(json, typeof (Product));
+ Product deserializedProduct = (Product)JsonConvert.DeserializeObject(json, typeof(Product));
Assert.AreEqual("Apple", deserializedProduct.Name);
Assert.AreEqual(new DateTime(2008, 12, 28, 0, 0, 0, DateTimeKind.Utc), deserializedProduct.ExpiryDate);
@@ -1846,11 +1847,11 @@ public void ComplexValuesInObjectArray()
Assert.AreEqual("Navigate", o.Method);
Assert.AreEqual(3, o.Data.Length);
Assert.AreEqual("dashboard", o.Data[0]);
- CustomAssert.IsInstanceOfType(typeof (JArray), o.Data[1]);
- Assert.AreEqual(4, ((JArray) o.Data[1]).Count);
- CustomAssert.IsInstanceOfType(typeof (JObject), o.Data[2]);
- Assert.AreEqual(1, ((JObject) o.Data[2]).Count);
- Assert.AreEqual(1, (int) ((JObject) o.Data[2])["one"]);
+ CustomAssert.IsInstanceOfType(typeof(JArray), o.Data[1]);
+ Assert.AreEqual(4, ((JArray)o.Data[1]).Count);
+ CustomAssert.IsInstanceOfType(typeof(JObject), o.Data[2]);
+ Assert.AreEqual(1, ((JObject)o.Data[2]).Count);
+ Assert.AreEqual(1, (int)((JObject)o.Data[2])["one"]);
}
[Test]
@@ -1974,9 +1975,9 @@ public void FailWhenClassWithNoDefaultConstructorHasMultipleConstructorsWithArgu
ExceptionAssert.Throws<JsonSerializationException>(
@"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'sublocation', line 1, position 15.",
() =>
- {
- Event e = JsonConvert.DeserializeObject<Event>(json);
- });
+ {
+ Event e = JsonConvert.DeserializeObject<Event>(json);
+ });
}
[Test]
@@ -1985,10 +1986,10 @@ public void DeserializeObjectSetOnlyProperty()
string json = @"{'SetOnlyProperty':[1,2,3,4,5]}";
SetOnlyPropertyClass2 setOnly = JsonConvert.DeserializeObject<SetOnlyPropertyClass2>(json);
- JArray a = (JArray) setOnly.GetValue();
+ JArray a = (JArray)setOnly.GetValue();
Assert.AreEqual(5, a.Count);
- Assert.AreEqual(1, (int) a[0]);
- Assert.AreEqual(5, (int) a[a.Count - 1]);
+ Assert.AreEqual(1, (int)a[0]);
+ Assert.AreEqual(5, (int)a[a.Count - 1]);
}
[Test]
@@ -2061,9 +2062,9 @@ public void CannotPopulateObjectIntoArray()
ExceptionAssert.Throws<JsonSerializationException>(
@"Cannot populate JSON object onto type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]'. Path '', line 1, position 2.",
() =>
- {
- JsonConvert.PopulateObject(json, new List<Person>());
- });
+ {
+ JsonConvert.PopulateObject(json, new List<Person>());
+ });
}
[Test]
@@ -2119,17 +2120,17 @@ public void DeserializeEnsureTypeEmptyStringToIntError()
ExceptionAssert.Throws<JsonReaderException>(
@"Could not convert string to integer: . Path 'ReadTimeout', line 1, position 15.",
() =>
- {
- JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
- {
- ContractResolver = new DefaultContractResolver
- {
+ {
+ JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
+ {
+ ContractResolver = new DefaultContractResolver
+ {
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
- IgnoreSerializableAttribute = true
+ IgnoreSerializableAttribute = true
#endif
- }
- });
- });
+ }
+ });
+ });
}
[Test]
@@ -2138,17 +2139,17 @@ public void DeserializeEnsureTypeNullToIntError()
ExceptionAssert.Throws<JsonSerializationException>(
@"Error converting value {null} to type 'System.Int32'. Path 'ReadTimeout', line 1, position 17.",
() =>
+ {
+ JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:null}", new JsonSerializerSettings
{
- JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:null}", new JsonSerializerSettings
+ ContractResolver = new DefaultContractResolver
{
- ContractResolver = new DefaultContractResolver
- {
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
- IgnoreSerializableAttribute = true
+ IgnoreSerializableAttribute = true
#endif
- }
- });
+ }
});
+ });
}
[Test]
@@ -2210,9 +2211,9 @@ public void SerializeStruct()
public void SerializeListWithJsonConverter()
{
Foo f = new Foo();
- f.Bars.Add(new Bar {Id = 0});
- f.Bars.Add(new Bar {Id = 1});
- f.Bars.Add(new Bar {Id = 2});
+ f.Bars.Add(new Bar { Id = 0 });
+ f.Bars.Add(new Bar { Id = 1 });
+ f.Bars.Add(new Bar { Id = 2 });
string json = JsonConvert.SerializeObject(f, Formatting.Indented);
Assert.AreEqual(@"{
@@ -2248,8 +2249,8 @@ public void SerializeGuidKeyedDictionary()
public void SerializePersonKeyedDictionary()
{
Dictionary<Person, int> dictionary = new Dictionary<Person, int>();
- dictionary.Add(new Person {Name = "p1"}, 1);
- dictionary.Add(new Person {Name = "p2"}, 2);
+ dictionary.Add(new Person { Name = "p1" }, 1);
+ dictionary.Add(new Person { Name = "p2" }, 2);
string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
@@ -2360,7 +2361,7 @@ public void SerializeFragment()
public void DeserializeBaseReferenceWithDerivedValue()
{
PersonPropertyClass personPropertyClass = new PersonPropertyClass();
- WagePerson wagePerson = (WagePerson) personPropertyClass.Person;
+ WagePerson wagePerson = (WagePerson)personPropertyClass.Person;
wagePerson.BirthDate = new DateTime(2000, 11, 29, 23, 59, 59, DateTimeKind.Utc);
wagePerson.Department = "McDees";
@@ -2381,7 +2382,7 @@ public void DeserializeBaseReferenceWithDerivedValue()
json);
PersonPropertyClass newPersonPropertyClass = JsonConvert.DeserializeObject<PersonPropertyClass>(json);
- Assert.AreEqual(wagePerson.HourlyWage, ((WagePerson) newPersonPropertyClass.Person).HourlyWage);
+ Assert.AreEqual(wagePerson.HourlyWage, ((WagePerson)newPersonPropertyClass.Person).HourlyWage);
}
public class ExistingValueClass
@@ -2409,8 +2410,8 @@ public void DeserializePopulateDictionaryAndList()
Assert.IsNotNull(d);
Assert.IsNotNull(d.Dictionary);
- Assert.AreEqual(typeof (Dictionary<string, string>), d.Dictionary.GetType());
- Assert.AreEqual(typeof (List<string>), d.List.GetType());
+ Assert.AreEqual(typeof(Dictionary<string, string>), d.Dictionary.GetType());
+ Assert.AreEqual(typeof(List<string>), d.List.GetType());
Assert.AreEqual(2, d.Dictionary.Count);
Assert.AreEqual("new", d.Dictionary["existing"]);
Assert.AreEqual("appended", d.Dictionary["appended"]);
@@ -2485,8 +2486,8 @@ public void IgnoreIndexedProperties()
{
ThisGenericTest<KeyValueId> g = new ThisGenericTest<KeyValueId>();
- g.Add(new KeyValueId {Id = 1, Key = "key1", Value = "value1"});
- g.Add(new KeyValueId {Id = 2, Key = "key2", Value = "value2"});
+ g.Add(new KeyValueId { Id = 1, Key = "key1", Value = "value1" });
+ g.Add(new KeyValueId { Id = 2, Key = "key2", Value = "value2" });
g.MyProperty = "some value";
@@ -2535,9 +2536,9 @@ public void DeserializeDictionaryWithNoDefaultConstructor()
ExceptionAssert.Throws<JsonSerializationException>(
"Unable to find a default constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+DictionaryWithNoDefaultConstructor. Path 'key1', line 1, position 6.",
() =>
- {
- JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json);
- });
+ {
+ JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json);
+ });
}
public class DictionaryWithNoDefaultConstructor : Dictionary<string, string>
@@ -2554,7 +2555,8 @@ public DictionaryWithNoDefaultConstructor(IEnumerable<KeyValuePair<string, strin
[JsonObject(MemberSerialization.OptIn)]
public class A
{
- [JsonProperty("A1")] private string _A1;
+ [JsonProperty("A1")]
+ private string _A1;
public string A1
{
@@ -2571,7 +2573,8 @@ public class B : A
{
public string B1 { get; set; }
- [JsonProperty("B2")] private string _B2;
+ [JsonProperty("B2")]
+ private string _B2;
public string B2
{
@@ -2621,7 +2624,7 @@ public class Human : Animal
{
public override string Name
{
- get { return typeof (Human).Name; }
+ get { return typeof(Human).Name; }
}
public string Ethnicity { get; set; }
@@ -2644,13 +2647,13 @@ public void DataContractJsonSerializerTest()
DataContractJsonSerializerTestClass c = new DataContractJsonSerializerTestClass();
c.TimeSpanProperty = new TimeSpan(200, 20, 59, 30, 900);
c.GuidProperty = new Guid("66143115-BE2A-4a59-AF0A-348E1EA15B1E");
- c.AnimalProperty = new Human() {Ethnicity = "European"};
+ c.AnimalProperty = new Human() { Ethnicity = "European" };
c.ExceptionProperty = ex;
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(
- typeof (DataContractJsonSerializerTestClass),
- new Type[] {typeof (Human)});
+ typeof(DataContractJsonSerializerTestClass),
+ new Type[] { typeof(Human) });
serializer.WriteObject(ms, c);
byte[] jsonBytes = ms.ToArray();
@@ -2695,7 +2698,7 @@ public int Count
public bool IsReadOnly
{
- get { return ((IDictionary<string, T>) _innerDictionary).IsReadOnly; }
+ get { return ((IDictionary<string, T>)_innerDictionary).IsReadOnly; }
}
public ICollection<string> Keys
@@ -2721,7 +2724,7 @@ public ICollection<T> Values
public void Add(KeyValuePair<string, T> item)
{
- ((IDictionary<string, T>) _innerDictionary).Add(item);
+ ((IDictionary<string, T>)_innerDictionary).Add(item);
}
public void Add(string key, T value)
@@ -2736,7 +2739,7 @@ public void Clear()
public bool Contains(KeyValuePair<string, T> item)
{
- return ((IDictionary<string, T>) _innerDictionary).Contains(item);
+ return ((IDictionary<string, T>)_innerDictionary).Contains(item);
}
public bool ContainsKey(string key)
@@ -2746,7 +2749,7 @@ public bool ContainsKey(string key)
public void CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
{
- ((IDictionary<string, T>) _innerDictionary).CopyTo(array, arrayIndex);
+ ((IDictionary<string, T>)_innerDictionary).CopyTo(array, arrayIndex);
}
public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
@@ -2769,7 +2772,7 @@ public void Merge(ModelStateDictionary<T> dictionary)
public bool Remove(KeyValuePair<string, T> item)
{
- return ((IDictionary<string, T>) _innerDictionary).Remove(item);
+ return ((IDictionary<string, T>)_innerDictionary).Remove(item);
}
public bool Remove(string key)
@@ -2784,7 +2787,7 @@ public bool TryGetValue(string key, out T value)
IEnumerator IEnumerable.GetEnumerator()
{
- return ((IEnumerable) _innerDictionary).GetEnumerator();
+ return ((IEnumerable)_innerDictionary).GetEnumerator();
}
}
@@ -2838,10 +2841,10 @@ protected ISerializableTestObject(SerializationInfo info, StreamingContext conte
{
_stringValue = info.GetString("stringValue");
_intValue = info.GetInt32("intValue");
- _dateTimeOffsetValue = (DateTimeOffset) info.GetValue("dateTimeOffsetValue", typeof (DateTimeOffset));
- _personValue = (Person) info.GetValue("personValue", typeof (Person));
- _nullPersonValue = (Person) info.GetValue("nullPersonValue", typeof (Person));
- _nullableInt = (int?) info.GetValue("nullableInt", typeof (int?));
+ _dateTimeOffsetValue = (DateTimeOffset)info.GetValue("dateTimeOffsetValue", typeof(DateTimeOffset));
+ _personValue = (Person)info.GetValue("personValue", typeof(Person));
+ _nullPersonValue = (Person)info.GetValue("nullPersonValue", typeof(Person));
+ _nullableInt = (int?)info.GetValue("nullableInt", typeof(int?));
_booleanValue = info.GetBoolean("booleanValue");
_byteValue = info.GetByte("byteValue");
@@ -2926,11 +2929,11 @@ public void SerializeISerializableInPartialTrust()
@"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path 'booleanValue', line 1, position 14.",
() =>
- {
- JsonTypeReflector.SetFullyTrusted(false);
+ {
+ JsonTypeReflector.SetFullyTrusted(false);
- JsonConvert.DeserializeObject<ISerializableTestObject>("{booleanValue:true}");
- });
+ JsonConvert.DeserializeObject<ISerializableTestObject>("{booleanValue:true}");
+ });
}
finally
{
@@ -2945,7 +2948,7 @@ public void DeserializeISerializableInPartialTrust()
{
ExceptionAssert.Throws<JsonSerializationException>(
@"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
-To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.",
+To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path ''.",
() =>
{
JsonTypeReflector.SetFullyTrusted(false);
@@ -3146,14 +3149,14 @@ public void DeserializeClassWithInheritedProtectedMember()
""AA_property6"": 2
}");
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetField("AA_field1", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof (AA).GetField("AA_field2", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property1", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property2", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property3", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property4", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property5", BindingFlags.Instance | BindingFlags.NonPublic), myA));
- Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property6", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field1", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field2", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property1", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property2", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property3", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property4", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property5", BindingFlags.Instance | BindingFlags.NonPublic), myA));
+ Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property6", BindingFlags.Instance | BindingFlags.NonPublic), myA));
BB myB = JsonConvert.DeserializeObject<BB>(
@"{
@@ -3177,30 +3180,31 @@ public void DeserializeClassWithInheritedProtectedMember()
""BB_property8"": 3
}");
- Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof (AA).GetField("AA_field1", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof (AA).GetField("AA_field2", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property1", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property2", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property3", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property4", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property5", BindingFlags.Instance | BindingFlags.NonPublic), myB));
- Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof (AA).GetProperty("AA_property6", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field1", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field2", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property1", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property2", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property3", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property4", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property5", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property6", BindingFlags.Instance | BindingFlags.NonPublic), myB));
Assert.AreEqual(4, myB.BB_field1);
Assert.AreEqual(4, myB.BB_field2);
Assert.AreEqual(3, myB.BB_property1);
Assert.AreEqual(3, myB.BB_property2);
- Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof (BB).GetProperty("BB_property3", BindingFlags.Instance | BindingFlags.Public), myB));
- Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof (BB).GetProperty("BB_property4", BindingFlags.Instance | BindingFlags.NonPublic), myB));
+ Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property3", BindingFlags.Instance | BindingFlags.Public), myB));
+ Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property4", BindingFlags.Instance | BindingFlags.NonPublic), myB));
Assert.AreEqual(0, myB.BB_property5);
- Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof (BB).GetProperty("BB_property6", BindingFlags.Instance | BindingFlags.Public), myB));
- Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof (BB).GetProperty("BB_property7", BindingFlags.Instance | BindingFlags.Public), myB));
- Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof (BB).GetProperty("BB_property8", BindingFlags.Instance | BindingFlags.Public), myB));
+ Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property6", BindingFlags.Instance | BindingFlags.Public), myB));
+ Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property7", BindingFlags.Instance | BindingFlags.Public), myB));
+ Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property8", BindingFlags.Instance | BindingFlags.Public), myB));
}
public class AA
{
- [JsonProperty] protected int AA_field1;
+ [JsonProperty]
+ protected int AA_field1;
protected int AA_field2;
[JsonProperty]
@@ -3237,7 +3241,8 @@ public AA(int f)
public class BB : AA
{
- [JsonProperty] public int BB_field1;
+ [JsonProperty]
+ public int BB_field1;
public int BB_field2;
[JsonProperty]
@@ -3353,8 +3358,8 @@ public void FullClientMapSerialization()
{
ClientMap source = new ClientMap()
{
- position = new Pos() {X = 100, Y = 200},
- center = new PosDouble() {X = 251.6, Y = 361.3}
+ position = new Pos() { X = 100, Y = 200 },
+ center = new PosDouble() { X = 251.6, Y = 361.3 }
};
string json = JsonConvert.SerializeObject(source, new PosConverter(), new PosDoubleConverter());
@@ -3383,7 +3388,7 @@ public class PosConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
- Pos p = (Pos) value;
+ Pos p = (Pos)value;
if (p != null)
writer.WriteRawValue(String.Format("new Pos({0},{1})", p.X, p.Y));
@@ -3398,7 +3403,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
public override bool CanConvert(Type objectType)
{
- return objectType.IsAssignableFrom(typeof (Pos));
+ return objectType.IsAssignableFrom(typeof(Pos));
}
}
@@ -3406,7 +3411,7 @@ public class PosDoubleConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
- PosDouble p = (PosDouble) value;
+ PosDouble p = (PosDouble)value;
if (p != null)
writer.WriteRawValue(String.Format(CultureInfo.InvariantCulture, "new PosD({0},{1})", p.X, p.Y));
@@ -3421,7 +3426,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
public override bool CanConvert(Type objectType)
{
- return objectType.IsAssignableFrom(typeof (PosDouble));
+ return objectType.IsAssignableFrom(typeof(PosDouble));
}
}
@@ -3462,7 +3467,7 @@ public void DeserializeExistingGenericList()
GenericListTestClass newValue = JsonConvert.DeserializeObject<GenericListTestClass>(json);
Assert.AreEqual(2, newValue.GenericList.Count);
- Assert.AreEqual(typeof (List<string>), newValue.GenericList.GetType());
+ Assert.AreEqual(typeof(List<string>), newValue.GenericList.GetType());
}
[Test]
@@ -3545,14 +3550,14 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
- List<string> existingStrings = (List<string>) existingValue;
+ List<string> existingStrings = (List<string>)existingValue;
List<string> newStrings = new List<string>(existingStrings);
reader.Read();
while (reader.TokenType != JsonToken.EndArray)
{
- string s = (string) reader.Value;
+ string s = (string)reader.Value;
newStrings.Add(s);
reader.Read();
@@ -3563,7 +3568,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
public override bool CanConvert(Type objectType)
{
- return (objectType == typeof (List<string>));
+ return (objectType == typeof(List<string>));
}
}
@@ -3571,11 +3576,11 @@ public override bool CanConvert(Type objectType)
public void StringListAppenderConverterTest()
{
Movie p = new Movie();
- p.ReleaseCountries = new List<string> {"Existing"};
+ p.ReleaseCountries = new List<string> { "Existing" };
JsonConvert.PopulateObject("{'ReleaseCountries':['Appended']}", p, new JsonSerializerSettings
{
- Converters = new List<JsonConverter> {new StringListAppenderConverter()}
+ Converters = new List<JsonConverter> { new StringListAppenderConverter() }
});
Assert.AreEqual(2, p.ReleaseCountries.Count);
@@ -3592,15 +3597,15 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
- string existingString = (string) existingValue;
- string newString = existingString + (string) reader.Value;
+ string existingString = (string)existingValue;
+ string newString = existingString + (string)reader.Value;
return newString;
}
public override bool CanConvert(Type objectType)
{
- return (objectType == typeof (string));
+ return (objectType == typeof(string));
}
}
@@ -3612,7 +3617,7 @@ public void StringAppenderConverterTest()
JsonConvert.PopulateObject("{'Name':'Appended'}", p, new JsonSerializerSettings
{
- Converters = new List<JsonConverter> {new StringAppenderConverter()}
+ Converters = new List<JsonConverter> { new StringAppenderConverter() }
});
Assert.AreEqual("Existing,Appended", p.Name);
@@ -3637,9 +3642,9 @@ public void SerializeRefAdditionalContent()
ExceptionAssert.Throws<JsonSerializationException>(
"Additional content found in JSON reference object. A JSON reference object should only have a $ref property. Path 'Father.$id', line 6, position 11.",
() =>
- {
- JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
- });
+ {
+ JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
+ });
}
[Test]
@@ -3682,8 +3687,8 @@ public void SerializeRefNull()
Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
Assert.AreEqual(3, result.Count);
- Assert.AreEqual(1, ((JObject) result["Father"]).Count);
- Assert.AreEqual("blah!", (string) ((JObject) result["Father"])["blah"]);
+ Assert.AreEqual(1, ((JObject)result["Father"]).Count);
+ Assert.AreEqual("blah!", (string)((JObject)result["Father"])["blah"]);
}
public class ConstructorCompexIgnoredProperty
@@ -3778,7 +3783,7 @@ public void ShouldSerializeExample()
joe.Manager = mike;
mike.Manager = mike;
- string json = JsonConvert.SerializeObject(new[] {joe, mike}, Formatting.Indented);
+ string json = JsonConvert.SerializeObject(new[] { joe, mike }, Formatting.Indented);
// [
// {
// "Name": "Joe Employee",
@@ -3815,9 +3820,12 @@ public bool NameSpecified
set { _nameSpecified = value; }
}
- [JsonIgnore] public bool WeightSpecified;
+ [JsonIgnore]
+ public bool WeightSpecified;
- [JsonIgnore] [System.Xml.Serialization.XmlIgnoreAttribute] public bool HeightSpecified;
+ [JsonIgnore]
+ [System.Xml.Serialization.XmlIgnoreAttribute]
+ public bool HeightSpecified;
[JsonIgnore]
@@ -3921,7 +3929,8 @@ public class OptionalOrder
public string FirstOrder;
// Use the XmlIgnoreAttribute to ignore the
// special field named "FirstOrderSpecified".
- [System.Xml.Serialization.XmlIgnoreAttribute] public bool FirstOrderSpecified;
+ [System.Xml.Serialization.XmlIgnoreAttribute]
+ public bool FirstOrderSpecified;
}
public class FamilyDetails
@@ -3946,7 +3955,7 @@ public void SpecifiedExample()
martha.NumberOfChildren = 3;
martha.NumberOfChildrenSpecified = false;
- string json = JsonConvert.SerializeObject(new[] {joe, martha}, Formatting.Indented);
+ string json = JsonConvert.SerializeObject(new[] { joe, martha }, Formatting.Indented);
//[
// {
// "Name": "Joe Family Details",
@@ -3980,7 +3989,7 @@ public override string ToString()
public static implicit operator DictionaryKey(string value)
{
- return new DictionaryKey() {Value = value};
+ return new DictionaryKey() { Value = value };
}
}
@@ -3989,8 +3998,8 @@ public void SerializeDeserializeDictionaryKey()
{
Dictionary<DictionaryKey, string> dictionary = new Dictionary<DictionaryKey, string>();
- dictionary.Add(new DictionaryKey() {Value = "First!"}, "First");
- dictionary.Add(new DictionaryKey() {Value = "Second!"}, "Second");
+ dictionary.Add(new DictionaryKey() { Value = "First!" }, "First");
+ dictionary.Add(new DictionaryKey() { Value = "Second!" }, "Second");
string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
@@ -4008,7 +4017,7 @@ public void SerializeDeserializeDictionaryKey()
[Test]
public void SerializeNullableArray()
{
- string jsonText = JsonConvert.SerializeObject(new double?[] {2.4, 4.3, null}, Formatting.Indented);
+ string jsonText = JsonConvert.SerializeObject(new double?[] { 2.4, 4.3, null }, Formatting.Indented);
Assert.AreEqual(@"[
2.4,
@@ -4016,7 +4025,7 @@ public void SerializeNullableArray()
null
]", jsonText);
- double?[] d = (double?[]) JsonConvert.DeserializeObject(jsonText, typeof (double?[]));
+ double?[] d = (double?[])JsonConvert.DeserializeObject(jsonText, typeof(double?[]));
Assert.AreEqual(3, d.Length);
Assert.AreEqual(2.4, d[0]);
@@ -4071,7 +4080,7 @@ public void DeserializeByteArray()
JsonTextReader reader = new JsonTextReader(new StringReader(json));
- MyClass[] z = (MyClass[]) serializer1.Deserialize(reader, typeof (MyClass[]));
+ MyClass[] z = (MyClass[])serializer1.Deserialize(reader, typeof(MyClass[]));
Assert.AreEqual(2, z.Length);
Assert.AreEqual(0, z[0].Prop1.Length);
Assert.AreEqual(0, z[1].Prop1.Length);
@@ -4233,19 +4242,19 @@ public void SerializeExpandoObject()
IDictionary<string, object> newExpando = JsonConvert.DeserializeObject<ExpandoObject>(json);
- CustomAssert.IsInstanceOfType(typeof (long), newExpando["Int"]);
+ CustomAssert.IsInstanceOfType(typeof(long), newExpando["Int"]);
Assert.AreEqual((long)expando.Int, newExpando["Int"]);
- CustomAssert.IsInstanceOfType(typeof (double), newExpando["Decimal"]);
+ CustomAssert.IsInstanceOfType(typeof(double), newExpando["Decimal"]);
Assert.AreEqual(expando.Decimal, newExpando["Decimal"]);
- CustomAssert.IsInstanceOfType(typeof (ExpandoObject), newExpando["Complex"]);
- IDictionary<string, object> o = (ExpandoObject) newExpando["Complex"];
+ CustomAssert.IsInstanceOfType(typeof(ExpandoObject), newExpando["Complex"]);
+ IDictionary<string, object> o = (ExpandoObject)newExpando["Complex"];
- CustomAssert.IsInstanceOfType(typeof (string), o["String"]);
+ CustomAssert.IsInstanceOfType(typeof(string), o["String"]);
Assert.AreEqual(expando.Complex.String, o["String"]);
- CustomAssert.IsInstanceOfType(typeof (DateTime), o["DateTime"]);
+ CustomAssert.IsInstanceOfType(typeof(DateTime), o["DateTime"]);
Assert.AreEqual(expando.Complex.DateTime, o["DateTime"]);
}
#endif
@@ -4309,7 +4318,7 @@ public class VectorParent
public void DeserializeStructProperty()
{
VectorParent obj = new VectorParent();
- obj.Position = new Vector {X = 1, Y = 2, Z = 3};
+ obj.Position = new Vector { X = 1, Y = 2, Z = 3 };
string str = JsonConvert.SerializeObject(obj);
@@ -4368,7 +4377,7 @@ public void PrivateSetterOnBaseClassProperty()
var meh = JsonConvert.DeserializeObject<Base>(json, settings);
- Assert.AreEqual(((Derived) meh).IDoWork, "woo");
+ Assert.AreEqual(((Derived)meh).IDoWork, "woo");
Assert.AreEqual(meh.IDontWork, "meh");
}
@@ -4421,11 +4430,11 @@ public StructISerializable Foo1
public void DeserializeNullableStruct()
{
NullableStructPropertyClass nullableStructPropertyClass = new NullableStructPropertyClass();
- nullableStructPropertyClass.Foo1 = new StructISerializable() {Name = "foo 1"};
- nullableStructPropertyClass.Foo2 = new StructISerializable() {Name = "foo 2"};
+ nullableStructPropertyClass.Foo1 = new StructISerializable() { Name = "foo 1" };
+ nullableStructPropertyClass.Foo2 = new StructISerializable() { Name = "foo 2" };
NullableStructPropertyClass barWithNull = new NullableStructPropertyClass();
- barWithNull.Foo1 = new StructISerializable() {Name = "foo 1"};
+ barWithNull.Foo1 = new StructISerializable() { Name = "foo 1" };
barWithNull.Foo2 = null;
//throws error on deserialization because bar1.Foo2 is of type Foo?
@@ -4599,7 +4608,7 @@ public void DeserializeEnumerable()
{
EnumerableClass c = new EnumerableClass
{
- Enumerable = new List<string> {"One", "Two", "Three"}
+ Enumerable = new List<string> { "One", "Two", "Three" }
};
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
@@ -4673,15 +4682,15 @@ public void DeserializeNullDateTimeValueTest()
ExceptionAssert.Throws<JsonSerializationException>(
"Error converting value {null} to type 'System.DateTime'. Path '', line 1, position 4.",
() =>
- {
- JsonConvert.DeserializeObject("null", typeof (DateTime));
- });
+ {
+ JsonConvert.DeserializeObject("null", typeof(DateTime));
+ });
}
[Test]
public void DeserializeNullNullableDateTimeValueTest()
{
- object dateTime = JsonConvert.DeserializeObject("null", typeof (DateTime?));
+ object dateTime = JsonConvert.DeserializeObject("null", typeof(DateTime?));
Assert.IsNull(dateTime);
}
@@ -4783,7 +4792,7 @@ public IEnumerable<int> Numbers
{
get
{
- return new[] {1, 2, 3}; //fails
+ return new[] { 1, 2, 3 }; //fails
//return new List<int>(new[] { 1, 2, 3 }); //works
}
}
@@ -4879,7 +4888,7 @@ public void ChildWithDifferentOverrideObjectTest()
public interface IInterfaceObject
{
[JsonProperty(PropertyName = "virtualMember")]
- [JsonConverter(typeof (IsoDateTimeConverter))]
+ [JsonConverter(typeof(IsoDateTimeConverter))]
DateTime InterfaceMember { get; set; }
}
@@ -5086,9 +5095,12 @@ public PrivateMembersClassWithAttributes()
_readonlyString = "default!";
}
- [JsonProperty] private string _privateString;
- [JsonProperty] private readonly string _readonlyString;
- [JsonProperty] internal string _internalString;
+ [JsonProperty]
+ private string _privateString;
+ [JsonProperty]
+ private readonly string _readonlyString;
+ [JsonProperty]
+ internal string _internalString;
public string UseValue()
{
@@ -5134,19 +5146,27 @@ public void DeserializeGenericEnumerableProperty()
public class BaseType
{
- [DataMember] public string zebra;
+ [DataMember]
+ public string zebra;
}
[DataContract]
public class DerivedType : BaseType
{
- [DataMember(Order = 0)] public string bird;
- [DataMember(Order = 1)] public string parrot;
- [DataMember] public string dog;
- [DataMember(Order = 3)] public string antelope;
- [DataMember] public string cat;
- [JsonProperty(Order = 1)] public string albatross;
- [JsonProperty(Order = -2)] public string dinosaur;
+ [DataMember(Order = 0)]
+ public string bird;
+ [DataMember(Order = 1)]
+ public string parrot;
+ [DataMember]
+ public string dog;
+ [DataMember(Order = 3)]
+ public string antelope;
+ [DataMember]
+ public string cat;
+ [JsonProperty(Order = 1)]
+ public string albatross;
+ [JsonProperty(Order = -2)]
+ public string dinosaur;
}
[Test]
@@ -5293,7 +5313,7 @@ public void NullableValueGenericDictionary()
public void UsingJsonTextWriter()
{
// The property of the object has to be a number for the cast exception to occure
- object o = new {p = 1};
+ object o = new { p = 1 };
var json = JObject.FromObject(o);
@@ -5386,9 +5406,9 @@ public void DeserializeNullToNonNullableKeyValuePairArray()
ExceptionAssert.Throws<JsonSerializationException>(
"Cannot convert null value to KeyValuePair. Path '[0]', line 1, position 6.",
() =>
- {
- JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
- });
+ {
+ JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
+ });
}
[Test]
@@ -5416,7 +5436,7 @@ public void SerializeUriWithSlashes()
[Test]
public void DeserializeByteArrayWithTypeNameHandling()
{
- TestObject test = new TestObject("Test", new byte[] {72, 63, 62, 71, 92, 55});
+ TestObject test = new TestObject("Test", new byte[] { 72, 63, 62, 71, 92, 55 });
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.All;
@@ -5435,10 +5455,10 @@ public void DeserializeByteArrayWithTypeNameHandling()
using (JsonReader bsonReader = new JsonTextReader(new StreamReader(bsonStream)))
{
// Get exception here
- TestObject newObject = (TestObject) serializer.Deserialize(bsonReader);
+ TestObject newObject = (TestObject)serializer.Deserialize(bsonReader);
Assert.AreEqual("Test", newObject.Name);
- CollectionAssert.AreEquivalent(new byte[] {72, 63, 62, 71, 92, 55}, newObject.Data);
+ CollectionAssert.AreEquivalent(new byte[] { 72, 63, 62, 71, 92, 55 }, newObject.Data);
}
}
@@ -5473,7 +5493,7 @@ public void DeserializeDecimalsWithCulture()
[Test]
public void ReadForTypeHackFixDecimal()
{
- IList<decimal> d1 = new List<decimal> {1.1m};
+ IList<decimal> d1 = new List<decimal> { 1.1m };
string json = JsonConvert.SerializeObject(d1);
@@ -5486,7 +5506,7 @@ public void ReadForTypeHackFixDecimal()
[Test]
public void ReadForTypeHackFixDateTimeOffset()
{
- IList<DateTimeOffset?> d1 = new List<DateTimeOffset?> {null};
+ IList<DateTimeOffset?> d1 = new List<DateTimeOffset?> { null };
string json = JsonConvert.SerializeObject(d1);
@@ -5499,7 +5519,7 @@ public void ReadForTypeHackFixDateTimeOffset()
[Test]
public void ReadForTypeHackFixByteArray()
{
- IList<byte[]> d1 = new List<byte[]> {null};
+ IList<byte[]> d1 = new List<byte[]> { null };
string json = JsonConvert.SerializeObject(d1);
@@ -5539,15 +5559,15 @@ public void DeserializeNullInt()
ExceptionAssert.Throws<JsonSerializationException>(
"Error converting value {null} to type 'System.Int32'. Path '[3]', line 5, position 7.",
() =>
- {
- List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json);
- });
+ {
+ List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json);
+ });
}
[Test]
public void SerializeNullableWidgetStruct()
{
- Widget widget = new Widget {Id = new WidgetId {Value = "id"}};
+ Widget widget = new Widget { Id = new WidgetId { Value = "id" } };
string json = JsonConvert.SerializeObject(widget);
@@ -5561,8 +5581,8 @@ public void DeserializeNullableWidgetStruct()
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);
}
@@ -5572,14 +5592,14 @@ public void DeserializeBoolInt()
ExceptionAssert.Throws<JsonReaderException>(
"Error reading integer. Unexpected token: Boolean. Path 'PreProperty', line 2, position 22.",
() =>
- {
- string json = @"{
+ {
+ string json = @"{
""PreProperty"": true,
""PostProperty"": ""-1""
}";
- JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
- });
+ JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
+ });
}
[Test]
@@ -5588,12 +5608,12 @@ public void DeserializeUnexpectedEndInt()
ExceptionAssert.Throws<JsonSerializationException>(
"Unexpected end when setting PreProperty's value. Path 'PreProperty', line 2, position 18.",
() =>
- {
- string json = @"{
+ {
+ string json = @"{
""PreProperty"": ";
- JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
- });
+ JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
+ });
}
[Test]
@@ -5620,7 +5640,7 @@ public void DeserializeGuid()
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}
+ Payload = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
};
string jsonString = JsonConvert.SerializeObject(expected, Formatting.Indented);
@@ -5686,13 +5706,13 @@ public void DeserializeNullableEnum()
[Test]
public void NullableStructWithConverter()
{
- string json = JsonConvert.SerializeObject(new Widget1 {Id = new WidgetId1 {Value = 1234}});
+ 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);
+ Assert.AreEqual(new WidgetId1 { Value = 1234 }, w.Id);
}
[Test]
@@ -5765,9 +5785,9 @@ public void DeserializeInt32FromEmptyString()
ExceptionAssert.Throws<JsonSerializationException>(
"No JSON content found and type 'System.Int32' is not nullable. Path '', line 0, position 0.",
() =>
- {
- JsonConvert.DeserializeObject<int>("");
- });
+ {
+ JsonConvert.DeserializeObject<int>("");
+ });
}
[Test]
@@ -5784,9 +5804,9 @@ public void DeserializeDoubleFromNullString()
@"Value cannot be null.
Parameter name: value",
() =>
- {
- JsonConvert.DeserializeObject<double>(null);
- });
+ {
+ JsonConvert.DeserializeObject<double>(null);
+ });
}
[Test]
@@ -5891,7 +5911,7 @@ public void MetroBlogPost()
product.Name = "Apple";
product.ExpiryDate = new DateTime(2012, 4, 1);
product.Price = 3.99M;
- product.Sizes = new []{"Small", "Medium", "Large"};
+ product.Sizes = new[] { "Small", "Medium", "Large" };
string json = JsonConvert.SerializeObject(product);
//{
@@ -5924,7 +5944,7 @@ public void MetroBlogPost()
// ":::SIZES:::": [ ":::SMALL:::", ":::MEDIUM:::", ":::LARGE:::" ]
//}
- Color[] colors = new []{ Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.Black, Color.Brown };
+ Color[] colors = new[] { Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.Black, Color.Brown };
string json2 = JsonConvert.SerializeObject(colors, new JsonSerializerSettings
{
@@ -5932,7 +5952,7 @@ public void MetroBlogPost()
Converters = { new MetroStringConverter(), new MetroColorConverter() },
Formatting = Formatting.Indented
});
-
+
Assert.AreEqual(@"[
"":::GRAY:::"",
"":::GRAY:::"",
@@ -5943,37 +5963,11 @@ public void MetroBlogPost()
]", json2);
}
- public class MetroStringConverter : JsonConverter
- {
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
- {
- writer.WriteValue(":::" + value.ToString().ToUpper(CultureInfo.InvariantCulture) + ":::");
- }
-
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- return existingValue;
- }
-
- public override bool CanConvert(Type objectType)
- {
- return objectType == typeof (string);
- }
- }
-
- public class MetroPropertyNameResolver : DefaultContractResolver
- {
- protected internal override string ResolvePropertyName(string propertyName)
- {
- return ":::" + propertyName.ToUpper(CultureInfo.InvariantCulture) + ":::";
- }
- }
-
public class MetroColorConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
- Color color = (Color) value;
+ Color color = (Color)value;
Color fixedColor = (color == Color.White || color == Color.Black) ? color : Color.Gray;
writer.WriteValue(":::" + fixedColor.ToKnownColor().ToString().ToUpper() + ":::");
@@ -5981,12 +5975,12 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
- return Enum.Parse(typeof (Color), reader.Value.ToString());
+ return Enum.Parse(typeof(Color), reader.Value.ToString());
}
public override bool CanConvert(Type objectType)
{
- return objectType == typeof (Color);
+ return objectType == typeof(Color);
}
}