diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeDataSet.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeDataSet.cs new file mode 100644 index 00000000..4799154f --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeDataSet.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using NUnit.Framework; + +namespace Newtonsoft.Json.Tests.Documentation.Examples +{ + public class DeserializeDataSet + { + public void Example() + { + string json = @"{ + ""Table1"": [ + { + ""id"": 0, + ""item"": ""item 0"" + }, + { + ""id"": 1, + ""item"": ""item 1"" + } + ] + }"; + + DataSet dataSet = JsonConvert.DeserializeObject(json); + + DataTable dataTable = dataSet.Tables["Table1"]; + + Console.WriteLine(dataTable.Rows.Count); + // 2 + + foreach (DataRow row in dataTable.Rows) + { + Console.WriteLine(row["id"] + " - " + row["item"]); + } + // 0 - item 0 + // 1 - item 1 + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeFromBson.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeFromBson.cs index 28131084..34806b84 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeFromBson.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeFromBson.cs @@ -17,7 +17,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples public void Example() { - byte[] data = Convert.FromBase64String("MAAAAAJOYW1lAA4AAABNb3ZpZSBwcmVtaWVyAAlTdGFydERhdGUAwOApYTwBAAAA"); + byte[] data = Convert.FromBase64String("MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAMDgKWE8AQAAAA=="); MemoryStream ms = new MemoryStream(data); using (BsonReader reader = new BsonReader(ms)) @@ -27,7 +27,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples Event e = serializer.Deserialize(reader); Console.WriteLine(e.Name); - // Movie premier + // Movie Premiere } } } diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeObjectCreationHandling.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeObjectCreationHandling.cs new file mode 100644 index 00000000..d9802f67 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeObjectCreationHandling.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Newtonsoft.Json.Tests.Documentation.Examples +{ + public class DeserializeObjectCreationHandling + { + public class UserViewModel + { + public string Name { get; set; } + public IList Offices { get; private set; } + + public UserViewModel() + { + Offices = new List + { + "Auckland", + "Wellington", + "Christchurch" + }; + } + } + + public void Example() + { + string json = @"{ + ""Name"": ""James"", + ""Offices"": [ + ""Auckland"", + ""Wellington"", + ""Christchurch"" + ] + }"; + + UserViewModel model1 = JsonConvert.DeserializeObject(json); + + foreach (string office in model1.Offices) + { + Console.WriteLine(office); + } + // Auckland + // Wellington + // Christchurch + // Auckland + // Wellington + // Christchurch + + UserViewModel model2 = JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + ObjectCreationHandling = ObjectCreationHandling.Replace + }); + + foreach (string office in model2.Offices) + { + Console.WriteLine(office); + } + // Auckland + // Wellington + // Christchurch + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonObjectAttributeOverrideIEnumerable.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonObjectAttributeOverrideIEnumerable.cs new file mode 100644 index 00000000..62183741 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonObjectAttributeOverrideIEnumerable.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Collections; + +namespace Newtonsoft.Json.Tests.Documentation.Examples +{ + public class JsonObjectAttributeOverrideIEnumerable + { + [JsonObject] + public class Directory : IEnumerable + { + public string Name { get; set; } + public IList Files { get; set; } + + public Directory() + { + Files = new List(); + } + + public IEnumerator GetEnumerator() + { + return Files.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } + + public void Example() + { + Directory directory = new Directory + { + Name = "My Documents", + Files = + { + "ImportantLegalDocuments.docx", + "WiseFinancalAdvice.xlsx" + } + }; + + string json = JsonConvert.SerializeObject(directory, Formatting.Indented); + + Console.WriteLine(json); + // { + // "Name": "My Documents", + // "Files": [ + // "ImportantLegalDocuments.docx", + // "WiseFinancalAdvice.xlsx" + // ] + // } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonPropertyItemLevelSetting.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonPropertyItemLevelSetting.cs index 8d1c758a..f14d6236 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonPropertyItemLevelSetting.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/JsonPropertyItemLevelSetting.cs @@ -31,7 +31,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples }; Employee worker = new Employee { - Name = "Mabae", + Name = "Maeby", Manager = manager }; @@ -58,7 +58,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples // }, // { // "$id": "2", - // "Name": "Mabae", + // "Name": "Maeby", // "Manager": { // "$ref": "1" // } diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/MaxDepth.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/MaxDepth.cs new file mode 100644 index 00000000..343d79eb --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/MaxDepth.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Newtonsoft.Json.Tests.Documentation.Examples +{ + public class MaxDepth + { + public void Example() + { + string json = @"[ + [ + [ + ""1"", + ""Two"", + ""III"" + ] + ] + ]"; + + try + { + JsonConvert.DeserializeObject>>>(json, new JsonSerializerSettings + { + MaxDepth = 2 + }); + } + catch (JsonReaderException ex) + { + Console.WriteLine(ex.Message); + // The reader's MaxDepth of 2 has been exceeded. Path '[0][0]', line 3, position 12. + } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeDataSet.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeDataSet.cs new file mode 100644 index 00000000..bd78a874 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeDataSet.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using NUnit.Framework; + +namespace Newtonsoft.Json.Tests.Documentation.Examples +{ + public class SerializeDataSet + { + public void Example() + { + DataSet dataSet = new DataSet("dataSet"); + dataSet.Namespace = "NetFrameWork"; + DataTable table = new DataTable(); + DataColumn idColumn = new DataColumn("id", typeof(int)); + idColumn.AutoIncrement = true; + + DataColumn itemColumn = new DataColumn("item"); + table.Columns.Add(idColumn); + table.Columns.Add(itemColumn); + dataSet.Tables.Add(table); + + for (int i = 0; i < 2; i++) + { + DataRow newRow = table.NewRow(); + newRow["item"] = "item " + i; + table.Rows.Add(newRow); + } + + dataSet.AcceptChanges(); + + string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented); + + Console.WriteLine(json); + // { + // "Table1": [ + // { + // "id": 0, + // "item": "item 0" + // }, + // { + // "id": 1, + // "item": "item 1" + // } + // ] + // } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeRawJson.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeRawJson.cs new file mode 100644 index 00000000..f55a8ad1 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeRawJson.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Newtonsoft.Json.Linq; + +namespace Newtonsoft.Json.Tests.Documentation.Examples +{ + public class SerializeRawJson + { + public class JavaScriptSettings + { + public JRaw OnLoadFunction { get; set; } + public JRaw OnUnloadFunction { get; set; } + } + + public void Example() + { + JavaScriptSettings settings = new JavaScriptSettings + { + OnLoadFunction = new JRaw("OnLoad"), + OnUnloadFunction = new JRaw("function(e) { alert(e); }") + }; + + string json = JsonConvert.SerializeObject(settings, Formatting.Indented); + + Console.WriteLine(json); + // { + // "OnLoadFunction": OnLoad, + // "OnUnloadFunction": function(e) { alert(e); } + // } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeToBson.cs b/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeToBson.cs index a11bd944..5e6a449b 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeToBson.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/Examples/SerializeToBson.cs @@ -20,26 +20,21 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples { Event e = new Event { - Name = "Movie premier", + Name = "Movie Premiere", StartDate = new DateTime(2013, 1, 22, 20, 30, 0) }; - IList es = new List - { - e, e - }; - MemoryStream ms = new MemoryStream(); using (BsonWriter writer = new BsonWriter(ms)) { JsonSerializer serializer = new JsonSerializer(); - serializer.Serialize(writer, es); + serializer.Serialize(writer, e); } string data = Convert.ToBase64String(ms.ToArray()); Console.WriteLine(data); - // MAAAAAJOYW1lAA4AAABNb3ZpZSBwcmVtaWVyAAlTdGFydERhdGUAwOApYTwBAAAA + // MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAMDgKWE8AQAAAA== } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index f98de600..f42742ed 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -122,15 +122,19 @@ + + + + @@ -172,6 +176,7 @@ + @@ -202,6 +207,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 5b3624f8..3bc7f1f6 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -76,5 +76,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.11.15716")] +[assembly: AssemblyFileVersion("4.5.11.15724")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 9217e875..1791dc78 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -7228,6 +7228,7 @@ Parameter name: value", Assert.AreEqual(1234567890.123456m, d); } +#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || WINDOWS_PHONE) [Test] public void DontSerializeStaticFields() { @@ -7249,6 +7250,7 @@ Parameter name: value", ""k__BackingField"": false }", json); } +#endif } public enum Antworten @@ -7264,6 +7266,7 @@ Parameter name: value", public bool Selected { get; set; } } +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) [Serializable] public class AnswerFilterModel { @@ -7320,7 +7323,6 @@ Parameter name: value", } } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) [Serializable] public class PersonSerializable { diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 9dafe9d6..b00ab633 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -90,7 +90,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.11.15716")] +[assembly: AssemblyFileVersion("4.5.11.15724")] #endif [assembly: CLSCompliant(true)]