diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index aba09f59..b035ea29 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -52,6 +52,30 @@ namespace Newtonsoft.Json.Tests [TestFixture] 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] public void QuoteNameAndStrings() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 41bd408a..48556f02 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -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 since2012 = posts + .Where(p => (DateTime)p["Date"] > new DateTime(2012, 1, 1)).ToList(); + // JSON Serializer Basics + // Querying LINQ to JSON + + IList 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] public void CreateJTokenTreeNested() {