-Tests
This commit is contained in:
@@ -52,6 +52,30 @@ namespace Newtonsoft.Json.Tests
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonTextWriterTest : TestFixtureBase
|
public class JsonTextWriterTest : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
|
public void NewLine()
|
||||||
|
{
|
||||||
|
MemoryStream ms = new MemoryStream();
|
||||||
|
|
||||||
|
using (var streamWriter = new StreamWriter(ms, new UTF8Encoding(false)) { NewLine = "\n" })
|
||||||
|
using (var jsonWriter = new JsonTextWriter(streamWriter)
|
||||||
|
{
|
||||||
|
CloseOutput = true,
|
||||||
|
Indentation = 2,
|
||||||
|
Formatting = Formatting.Indented
|
||||||
|
})
|
||||||
|
{
|
||||||
|
jsonWriter.WriteStartObject();
|
||||||
|
jsonWriter.WritePropertyName("prop");
|
||||||
|
jsonWriter.WriteValue(true);
|
||||||
|
jsonWriter.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
string json = Encoding.UTF8.GetString(ms.ToArray());
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{" + '\n' + @" ""prop"": true" + '\n' + "}", json);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void QuoteNameAndStrings()
|
public void QuoteNameAndStrings()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -507,6 +507,77 @@ keyword such as type of business.""
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void FromObjectExample()
|
||||||
|
{
|
||||||
|
Post p = new Post
|
||||||
|
{
|
||||||
|
Title = "How to use FromObject",
|
||||||
|
Categories = new [] { "LINQ to JSON" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// serialize Post to JSON then parse JSON – SLOW!
|
||||||
|
//JObject o = JObject.Parse(JsonConvert.SerializeObject(p));
|
||||||
|
|
||||||
|
// create JObject directly from the Post
|
||||||
|
JObject o = JObject.FromObject(p);
|
||||||
|
|
||||||
|
o["Title"] = o["Title"] + " - Super effective!";
|
||||||
|
|
||||||
|
string json = o.ToString();
|
||||||
|
// {
|
||||||
|
// "Title": "How to use FromObject - It's super effective!",
|
||||||
|
// "Categories": [
|
||||||
|
// "LINQ to JSON"
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""Title"": ""How to use FromObject - Super effective!"",
|
||||||
|
""Description"": null,
|
||||||
|
""Link"": null,
|
||||||
|
""Categories"": [
|
||||||
|
""LINQ to JSON""
|
||||||
|
]
|
||||||
|
}", json);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void QueryingExample()
|
||||||
|
{
|
||||||
|
JArray posts = JArray.Parse(@"[
|
||||||
|
{
|
||||||
|
'Title': 'JSON Serializer Basics',
|
||||||
|
'Date': '2013-12-21T00:00:00',
|
||||||
|
'Categories': []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'Title': 'Querying LINQ to JSON',
|
||||||
|
'Date': '2014-06-03T00:00:00',
|
||||||
|
'Categories': [
|
||||||
|
'LINQ to JSON'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]");
|
||||||
|
|
||||||
|
JToken serializerBasics = posts
|
||||||
|
.Single(p => (string)p["Title"] == "JSON Serializer Basics");
|
||||||
|
// JSON Serializer Basics
|
||||||
|
|
||||||
|
IList<JToken> since2012 = posts
|
||||||
|
.Where(p => (DateTime)p["Date"] > new DateTime(2012, 1, 1)).ToList();
|
||||||
|
// JSON Serializer Basics
|
||||||
|
// Querying LINQ to JSON
|
||||||
|
|
||||||
|
IList<JToken> linqToJson = posts
|
||||||
|
.Where(p => p["Categories"].Any(c => (string)c == "LINQ to JSON")).ToList();
|
||||||
|
// Querying LINQ to JSON
|
||||||
|
|
||||||
|
Assert.IsNotNull(serializerBasics);
|
||||||
|
Assert.AreEqual(2, since2012.Count);
|
||||||
|
Assert.AreEqual(1, linqToJson.Count);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void CreateJTokenTreeNested()
|
public void CreateJTokenTreeNested()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user