Skip to content

Commit

Permalink
Update Newtonsoft.Json project to use nullable reference types (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Jul 28, 2019
1 parent f940f21 commit cdf1015
Show file tree
Hide file tree
Showing 169 changed files with 2,265 additions and 1,860 deletions.
2 changes: 1 addition & 1 deletion Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$workingName = if ($workingName) {$workingName} else {"Working"}
$assemblyVersion = if ($assemblyVersion) {$assemblyVersion} else {$majorVersion + '.0.0'}
$netCliChannel = "2.0"
$netCliVersion = "2.2.105"
$netCliVersion = "3.0.100-preview8-013317"
$nugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

$baseDir = resolve-path ..
Expand Down
16 changes: 16 additions & 0 deletions Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public void DeserializeXmlNode_DefaultDate()
Assert.AreEqual("<Time>0001-01-01T00:00:00</Time>", xmlNode.OuterXml);
}

[Test]
public void XmlNode_Null()
{
string json = JsonConvert.SerializeXmlNode(null);

Assert.AreEqual("null", json);
}

[Test]
public void XmlNode_Roundtrip_PropertyNameWithColon()
{
Expand Down Expand Up @@ -305,6 +313,14 @@ public void WriteJsonNull()
}

#if !NET20
[Test]
public void XNode_Null()
{
string json = JsonConvert.SerializeXNode(null);

Assert.AreEqual("null", json);
}

[Test]
public void XNode_UnescapeTextContent()
{
Expand Down
17 changes: 9 additions & 8 deletions Src/Newtonsoft.Json.Tests/JsonConvertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,14 +1563,15 @@ public class OverloadWithBaseType
public int Overload { get; set; }
}

[Test]
public void JsonConverterConstructor_OverloadsWithBaseTypes()
{
OverloadWithBaseType value = new OverloadWithBaseType();
string json = JsonConvert.SerializeObject(value);

Assert.AreEqual("{\"Overload\":\"IList<string>\"}", json);
}
//[Test]
//[Ignore("https://github.com/dotnet/roslyn/issues/36974")]
//public void JsonConverterConstructor_OverloadsWithBaseTypes()
//{
// OverloadWithBaseType value = new OverloadWithBaseType();
// string json = JsonConvert.SerializeObject(value);

// Assert.AreEqual("{\"Overload\":\"IList<string>\"}", json);
//}


[Test]
Expand Down
20 changes: 20 additions & 0 deletions Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,26 @@ public void BeforeAfter()
Assert.AreEqual(2, a[2].BeforeSelf().Count());
}

[Test]
public void BeforeSelf_NoParent_ReturnEmpty()
{
JObject o = new JObject();

List<JToken> result = o.BeforeSelf().ToList();
Assert.AreEqual(0, result.Count);
}

[Test]
public void BeforeSelf_OnlyChild_ReturnEmpty()
{
JArray a = new JArray();
JObject o = new JObject();
a.Add(o);

List<JToken> result = o.BeforeSelf().ToList();
Assert.AreEqual(0, result.Count);
}

[Test]
public void Casting()
{
Expand Down
134 changes: 14 additions & 120 deletions Src/Newtonsoft.Json.Tests/Linq/JsonPath/QueryExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,12 @@ public class QueryExpressionTests : TestFixtureBase
[Test]
public void AndExpressionTest()
{
CompositeExpression compositeExpression = new CompositeExpression
CompositeExpression compositeExpression = new CompositeExpression(QueryOperator.And)
{
Operator = QueryOperator.And,
Expressions = new List<QueryExpression>
{
new BooleanQueryExpression
{
Operator = QueryOperator.Exists,
Left = new List<PathFilter>
{
new FieldFilter
{
Name = "FirstName"
}
}
},
new BooleanQueryExpression
{
Operator = QueryOperator.Exists,
Left = new List<PathFilter>
{
new FieldFilter
{
Name = "LastName"
}
}
}
new BooleanQueryExpression(QueryOperator.Exists, new List<PathFilter> { new FieldFilter("FirstName") }, null),
new BooleanQueryExpression(QueryOperator.Exists, new List<PathFilter> { new FieldFilter("LastName") }, null)
}
};

Expand Down Expand Up @@ -103,33 +82,12 @@ public void AndExpressionTest()
[Test]
public void OrExpressionTest()
{
CompositeExpression compositeExpression = new CompositeExpression
CompositeExpression compositeExpression = new CompositeExpression(QueryOperator.Or)
{
Operator = QueryOperator.Or,
Expressions = new List<QueryExpression>
{
new BooleanQueryExpression
{
Operator = QueryOperator.Exists,
Left = new List<PathFilter>
{
new FieldFilter
{
Name = "FirstName"
}
}
},
new BooleanQueryExpression
{
Operator = QueryOperator.Exists,
Left = new List<PathFilter>
{
new FieldFilter
{
Name = "LastName"
}
}
}
new BooleanQueryExpression(QueryOperator.Exists, new List<PathFilter> { new FieldFilter("FirstName") }, null),
new BooleanQueryExpression(QueryOperator.Exists, new List<PathFilter> { new FieldFilter("LastName") }, null)
}
};

Expand Down Expand Up @@ -161,30 +119,14 @@ public void OrExpressionTest()
[Test]
public void BooleanExpressionTest_RegexEqualsOperator()
{
BooleanQueryExpression e1 = new BooleanQueryExpression
{
Operator = QueryOperator.RegexEquals,
Right = new JValue("/foo.*d/"),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e1 = new BooleanQueryExpression(QueryOperator.RegexEquals, new List<PathFilter> { new ArrayIndexFilter() }, new JValue("/foo.*d/"));

Assert.IsTrue(e1.IsMatch(null, new JArray("food")));
Assert.IsTrue(e1.IsMatch(null, new JArray("fooood and drink")));
Assert.IsFalse(e1.IsMatch(null, new JArray("FOOD")));
Assert.IsFalse(e1.IsMatch(null, new JArray("foo", "foog", "good")));

BooleanQueryExpression e2 = new BooleanQueryExpression
{
Operator = QueryOperator.RegexEquals,
Right = new JValue("/Foo.*d/i"),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e2 = new BooleanQueryExpression(QueryOperator.RegexEquals, new List<PathFilter> { new ArrayIndexFilter() }, new JValue("/Foo.*d/i"));

Assert.IsTrue(e2.IsMatch(null, new JArray("food")));
Assert.IsTrue(e2.IsMatch(null, new JArray("fooood and drink")));
Expand All @@ -195,28 +137,12 @@ public void BooleanExpressionTest_RegexEqualsOperator()
[Test]
public void BooleanExpressionTest_RegexEqualsOperator_CornerCase()
{
BooleanQueryExpression e1 = new BooleanQueryExpression
{
Operator = QueryOperator.RegexEquals,
Right = new JValue("/// comment/"),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e1 = new BooleanQueryExpression(QueryOperator.RegexEquals, new List<PathFilter> { new ArrayIndexFilter() }, new JValue("/// comment/"));

Assert.IsTrue(e1.IsMatch(null, new JArray("// comment")));
Assert.IsFalse(e1.IsMatch(null, new JArray("//comment", "/ comment")));

BooleanQueryExpression e2 = new BooleanQueryExpression
{
Operator = QueryOperator.RegexEquals,
Right = new JValue("/<tag>.*</tag>/i"),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e2 = new BooleanQueryExpression(QueryOperator.RegexEquals, new List<PathFilter> { new ArrayIndexFilter() }, new JValue("/<tag>.*</tag>/i"));

Assert.IsTrue(e2.IsMatch(null, new JArray("<Tag>Test</Tag>", "")));
Assert.IsFalse(e2.IsMatch(null, new JArray("<tag>Test<tag>")));
Expand All @@ -225,31 +151,15 @@ public void BooleanExpressionTest_RegexEqualsOperator_CornerCase()
[Test]
public void BooleanExpressionTest()
{
BooleanQueryExpression e1 = new BooleanQueryExpression
{
Operator = QueryOperator.LessThan,
Right = new JValue(3),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e1 = new BooleanQueryExpression(QueryOperator.LessThan, new List<PathFilter> { new ArrayIndexFilter() }, new JValue(3));

Assert.IsTrue(e1.IsMatch(null, new JArray(1, 2, 3, 4, 5)));
Assert.IsTrue(e1.IsMatch(null, new JArray(2, 3, 4, 5)));
Assert.IsFalse(e1.IsMatch(null, new JArray(3, 4, 5)));
Assert.IsFalse(e1.IsMatch(null, new JArray(4, 5)));
Assert.IsFalse(e1.IsMatch(null, new JArray("11", 5)));

BooleanQueryExpression e2 = new BooleanQueryExpression
{
Operator = QueryOperator.LessThanOrEquals,
Right = new JValue(3),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e2 = new BooleanQueryExpression(QueryOperator.LessThanOrEquals, new List<PathFilter> { new ArrayIndexFilter() }, new JValue(3));

Assert.IsTrue(e2.IsMatch(null, new JArray(1, 2, 3, 4, 5)));
Assert.IsTrue(e2.IsMatch(null, new JArray(2, 3, 4, 5)));
Expand All @@ -261,15 +171,7 @@ public void BooleanExpressionTest()
[Test]
public void BooleanExpressionTest_GreaterThanOperator()
{
BooleanQueryExpression e1 = new BooleanQueryExpression
{
Operator = QueryOperator.GreaterThan,
Right = new JValue(3),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e1 = new BooleanQueryExpression(QueryOperator.GreaterThan, new List<PathFilter> { new ArrayIndexFilter() }, new JValue(3));

Assert.IsTrue(e1.IsMatch(null, new JArray("2", "26")));
Assert.IsTrue(e1.IsMatch(null, new JArray(2, 26)));
Expand All @@ -280,15 +182,7 @@ public void BooleanExpressionTest_GreaterThanOperator()
[Test]
public void BooleanExpressionTest_GreaterThanOrEqualsOperator()
{
BooleanQueryExpression e1 = new BooleanQueryExpression
{
Operator = QueryOperator.GreaterThanOrEquals,
Right = new JValue(3),
Left = new List<PathFilter>
{
new ArrayIndexFilter()
}
};
BooleanQueryExpression e1 = new BooleanQueryExpression(QueryOperator.GreaterThanOrEquals, new List<PathFilter> { new ArrayIndexFilter() }, new JValue(3));

Assert.IsTrue(e1.IsMatch(null, new JArray("2", "26")));
Assert.IsTrue(e1.IsMatch(null, new JArray(2, 26)));
Expand Down
24 changes: 12 additions & 12 deletions Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net46'">
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.10" />
<PackageReference Include="FSharp.Core" Version="4.2.3" />
Expand All @@ -66,8 +66,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net451'">
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="FSharp.Core" Version="4.2.3" />
<PackageReference Include="System.Buffers" Version="4.4.0" />
Expand All @@ -89,8 +89,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net452'">
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Autofac" Version="4.6.2" />
<PackageReference Include="FSharp.Core" Version="4.2.3" />
<PackageReference Include="System.Buffers" Version="4.4.0" />
Expand All @@ -112,8 +112,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Web" />
Expand All @@ -129,8 +129,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net35'">
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<Reference Include="System.Web" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Data.Entity" />
Expand All @@ -144,8 +144,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net20'">
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<Reference Include="System.Web" />
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net20'">
Expand Down
12 changes: 12 additions & 0 deletions Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
Expand Down Expand Up @@ -727,6 +728,17 @@ protected virtual bool OnProp4Specified()
}
}

[Test]
public void NonGenericDictionary_KeyValueTypes()
{
DefaultContractResolver resolver = new DefaultContractResolver();

JsonDictionaryContract c = (JsonDictionaryContract)resolver.ResolveContract(typeof(IDictionary));

Assert.IsNull(c.DictionaryKeyType);
Assert.IsNull(c.DictionaryValueType);
}

[Test]
public void DefaultContractResolverIgnoreIsSpecifiedTrue()
{
Expand Down
Loading

0 comments on commit cdf1015

Please sign in to comment.