-Documentation

This commit is contained in:
James Newton-King
2016-05-15 19:49:42 +12:00
parent 1c4160cd84
commit 819d705fbc
5 changed files with 143 additions and 28 deletions
+3 -3
View File
@@ -2,7 +2,7 @@
$zipFileName = "Json80r4.zip" $zipFileName = "Json80r4.zip"
$majorVersion = "8.0" $majorVersion = "8.0"
$majorWithReleaseVersion = "8.0.4" $majorWithReleaseVersion = "8.0.4"
$nugetPrelease = "beta4" $nugetPrerelease = "beta4"
$version = GetVersion $majorWithReleaseVersion $version = GetVersion $majorWithReleaseVersion
$packageId = "Newtonsoft.Json" $packageId = "Newtonsoft.Json"
$signAssemblies = $false $signAssemblies = $false
@@ -262,9 +262,9 @@ function NUnitTests($build)
function GetNuGetVersion() function GetNuGetVersion()
{ {
$nugetVersion = $majorWithReleaseVersion $nugetVersion = $majorWithReleaseVersion
if ($nugetPrelease -ne $null) if ($nugetPrerelease -ne $null)
{ {
$nugetVersion = $nugetVersion + "-" + $nugetPrelease $nugetVersion = $nugetVersion + "-" + $nugetPrerelease
} }
return $nugetVersion return $nugetVersion
@@ -54,8 +54,10 @@ using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
namespace Newtonsoft.Json.Tests.Documentation namespace Newtonsoft.Json.Tests.Documentation
{ {
public class ConvertingJsonAndXmlTests [TestFixture]
public class ConvertingJsonAndXmlTests : TestFixtureBase
{ {
[Test]
public void SerializeXmlNode() public void SerializeXmlNode()
{ {
#region SerializeXmlNode #region SerializeXmlNode
@@ -98,6 +100,7 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void DeserializeXmlNode() public void DeserializeXmlNode()
{ {
#region DeserializeXmlNode #region DeserializeXmlNode
@@ -137,6 +140,7 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void ForceJsonArray() public void ForceJsonArray()
{ {
#region ForceJsonArray #region ForceJsonArray
@@ -58,7 +58,7 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
public static StreamReader OpenText(string path) public static StreamReader OpenText(string path)
{ {
return null; return new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes("{}")));
} }
public static StreamWriter CreateText(string path) public static StreamWriter CreateText(string path)
@@ -76,7 +76,8 @@ namespace Newtonsoft.Json.Tests.Documentation
} }
} }
public class LinqToJsonTests [TestFixture]
public class LinqToJsonTests : TestFixtureBase
{ {
public void LinqToJsonBasic() public void LinqToJsonBasic()
{ {
@@ -129,9 +130,10 @@ namespace Newtonsoft.Json.Tests.Documentation
private List<Post> GetPosts() private List<Post> GetPosts()
{ {
return null; return new List<Post>();
} }
[Test]
public void LinqToJsonCreateDeclaratively() public void LinqToJsonCreateDeclaratively()
{ {
#region LinqToJsonCreateDeclaratively #region LinqToJsonCreateDeclaratively
@@ -163,7 +165,7 @@ namespace Newtonsoft.Json.Tests.Documentation
// "channel": { // "channel": {
// "title": "James Newton-King", // "title": "James Newton-King",
// "link": "http://james.newtonking.com", // "link": "http://james.newtonking.com",
// "description": "James Newton-King's blog.", // "description": "James Newton-King\'s blog.",
// "item": [ // "item": [
// { // {
// "title": "Json.NET 1.3 + New license + Now on CodePlex", // "title": "Json.NET 1.3 + New license + Now on CodePlex",
@@ -189,9 +191,10 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void LinqToJsonCreateFromObject() public void LinqToJsonCreateFromObject()
{ {
List<Post> posts = null; List<Post> posts = GetPosts();
#region LinqToJsonCreateFromObject #region LinqToJsonCreateFromObject
JObject o = JObject.FromObject(new JObject o = JObject.FromObject(new
@@ -216,6 +219,7 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void LinqToJsonCreateParse() public void LinqToJsonCreateParse()
{ {
#region LinqToJsonCreateParse #region LinqToJsonCreateParse
@@ -231,6 +235,7 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void LinqToJsonCreateParseArray() public void LinqToJsonCreateParseArray()
{ {
#region LinqToJsonCreateParseArray #region LinqToJsonCreateParseArray
@@ -244,6 +249,7 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void LinqToJsonReadObject() public void LinqToJsonReadObject()
{ {
#region LinqToJsonReadObject #region LinqToJsonReadObject
@@ -255,6 +261,7 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void LinqToJsonSimpleQuerying() public void LinqToJsonSimpleQuerying()
{ {
#region LinqToJsonSimpleQuerying #region LinqToJsonSimpleQuerying
@@ -262,7 +269,7 @@ namespace Newtonsoft.Json.Tests.Documentation
'channel': { 'channel': {
'title': 'James Newton-King', 'title': 'James Newton-King',
'link': 'http://james.newtonking.com', 'link': 'http://james.newtonking.com',
'description': 'James Newton-King's blog.', 'description': 'James Newton-King\'s blog.',
'item': [ 'item': [
{ {
'title': 'Json.NET 1.3 + New license + Now on CodePlex', 'title': 'Json.NET 1.3 + New license + Now on CodePlex',
@@ -303,9 +310,36 @@ namespace Newtonsoft.Json.Tests.Documentation
#endregion #endregion
} }
[Test]
public void LinqToJsonQuerying() public void LinqToJsonQuerying()
{ {
JObject rss = new JObject(); JObject rss = JObject.Parse(@"{
'channel': {
'title': 'James Newton-King',
'link': 'http://james.newtonking.com',
'description': 'James Newton-King\'s blog.',
'item': [
{
'title': 'Json.NET 1.3 + New license + Now on CodePlex',
'description': 'Annoucing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'CodePlex'
]
},
{
'title': 'LINQ to JSON beta',
'description': 'Annoucing LINQ to JSON',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'LINQ'
]
}
]
}
}");
#region LinqToJsonQuerying #region LinqToJsonQuerying
var postTitles = var postTitles =
@@ -321,7 +355,7 @@ namespace Newtonsoft.Json.Tests.Documentation
//Json.NET 1.3 + New license + Now on CodePlex //Json.NET 1.3 + New license + Now on CodePlex
var categories = var categories =
from c in rss["channel"]["item"].Children()["category"].Values<string>() from c in rss["channel"]["item"].SelectMany(i => i["categories"]).Values<string>()
group c by c group c by c
into g into g
orderby g.Count() descending orderby g.Count() descending
@@ -336,6 +370,9 @@ namespace Newtonsoft.Json.Tests.Documentation
//LINQ - Count: 1 //LINQ - Count: 1
//CodePlex - Count: 1 //CodePlex - Count: 1
#endregion #endregion
Assert.AreEqual(2, postTitles.Count());
Assert.AreEqual(3, categories.Count());
} }
#region LinqToJsonDeserializeObject #region LinqToJsonDeserializeObject
@@ -354,6 +391,7 @@ namespace Newtonsoft.Json.Tests.Documentation
} }
#endregion #endregion
[Test]
public void LinqToJsonDeserializeExample() public void LinqToJsonDeserializeExample()
{ {
#region LinqToJsonDeserializeExample #region LinqToJsonDeserializeExample
@@ -365,6 +403,7 @@ namespace Newtonsoft.Json.Tests.Documentation
'code': 0, 'code': 0,
'msg': 'No action taken' 'msg': 'No action taken'
} }
}
}"; }";
JObject json = JObject.Parse(jsonText); JObject json = JObject.Parse(jsonText);
@@ -386,17 +425,53 @@ namespace Newtonsoft.Json.Tests.Documentation
Console.WriteLine(shortie.Error.ErrorMessage); Console.WriteLine(shortie.Error.ErrorMessage);
// No action taken // No action taken
#endregion #endregion
Assert.AreEqual("http://www.foo.com/", shortie.Original);
Assert.AreEqual("No action taken", shortie.Error.ErrorMessage);
} }
[Test]
public void SelectTokenSimple() public void SelectTokenSimple()
{ {
JObject o = new JObject(); JObject o = JObject.Parse(@"{
'Stores': [
'Lambton Quay',
'Willis Street'
],
'Manufacturers': [
{
'Name': 'Acme Co',
'Products': [
{
'Name': 'Anvil',
'Price': 50
}
]
},
{
'Name': 'Contoso',
'Products': [
{
'Name': 'Elbow Grease',
'Price': 99.95
},
{
'Name': 'Headlight Fluid',
'Price': 4
}
]
}
]
}");
#region SelectTokenSimple #region SelectTokenSimple
string name = (string)o.SelectToken("Manufacturers[0].Name"); string name = (string)o.SelectToken("Manufacturers[0].Name");
#endregion #endregion
Assert.AreEqual("Acme Co", name);
} }
[Test]
public void SelectTokenComplex() public void SelectTokenComplex()
{ {
#region SelectTokenComplex #region SelectTokenComplex
@@ -440,11 +515,45 @@ namespace Newtonsoft.Json.Tests.Documentation
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name"); string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease // Elbow Grease
#endregion #endregion
Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);
} }
[Test]
public void SelectTokenLinq() public void SelectTokenLinq()
{ {
JObject o = new JObject(); JObject o = JObject.Parse(@"{
'Stores': [
'Lambton Quay',
'Willis Street'
],
'Manufacturers': [
{
'Name': 'Acme Co',
'Products': [
{
'Name': 'Anvil',
'Price': 50
}
]
},
{
'Name': 'Contoso',
'Products': [
{
'Name': 'Elbow Grease',
'Price': 99.95
},
{
'Name': 'Headlight Fluid',
'Price': 4
}
]
}
]
}");
#region SelectTokenLinq #region SelectTokenLinq
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList(); IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
@@ -458,6 +567,10 @@ namespace Newtonsoft.Json.Tests.Documentation
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price")); decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
// 149.95 // 149.95
#endregion #endregion
Assert.AreEqual(2, storeNames.Count);
Assert.AreEqual(2, firstProductNames.Count);
Assert.AreEqual(149.95m, totalPrice);
} }
} }
} }
@@ -9,9 +9,8 @@
<ProjectGuid>13e906ae-5a47-4354-8caf-e63e8ac541ed</ProjectGuid> <ProjectGuid>13e906ae-5a47-4354-8caf-e63e8ac541ed</ProjectGuid>
<RootNamespace>Newtonsoft.Json.Tests</RootNamespace> <RootNamespace>Newtonsoft.Json.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
</PropertyGroup> </PropertyGroup>
@@ -9,9 +9,8 @@
<ProjectGuid>779e052e-7218-4da3-b419-ffb0f4c95081</ProjectGuid> <ProjectGuid>779e052e-7218-4da3-b419-ffb0f4c95081</ProjectGuid>
<RootNamespace>Newtonsoft.Json</RootNamespace> <RootNamespace>Newtonsoft.Json</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
</PropertyGroup> </PropertyGroup>