-Examples!

-Fixed build
This commit is contained in:
James Newton-King
2013-01-25 00:02:58 +13:00
parent 290e7543af
commit 7899af09c9
13 changed files with 302 additions and 15 deletions
@@ -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<DataSet>(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
}
}
}
@@ -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<Event>(reader);
Console.WriteLine(e.Name);
// Movie premier
// Movie Premiere
}
}
}
@@ -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<string> Offices { get; private set; }
public UserViewModel()
{
Offices = new List<string>
{
"Auckland",
"Wellington",
"Christchurch"
};
}
}
public void Example()
{
string json = @"{
""Name"": ""James"",
""Offices"": [
""Auckland"",
""Wellington"",
""Christchurch""
]
}";
UserViewModel model1 = JsonConvert.DeserializeObject<UserViewModel>(json);
foreach (string office in model1.Offices)
{
Console.WriteLine(office);
}
// Auckland
// Wellington
// Christchurch
// Auckland
// Wellington
// Christchurch
UserViewModel model2 = JsonConvert.DeserializeObject<UserViewModel>(json, new JsonSerializerSettings
{
ObjectCreationHandling = ObjectCreationHandling.Replace
});
foreach (string office in model2.Offices)
{
Console.WriteLine(office);
}
// Auckland
// Wellington
// Christchurch
}
}
}
@@ -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<string>
{
public string Name { get; set; }
public IList<string> Files { get; set; }
public Directory()
{
Files = new List<string>();
}
public IEnumerator<string> 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"
// ]
// }
}
}
}
@@ -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"
// }
@@ -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<List<IList<IList<string>>>>(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.
}
}
}
}
@@ -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"
// }
// ]
// }
}
}
}
@@ -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); }
// }
}
}
}
@@ -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<Event> es = new List<Event>
{
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==
}
}
}
@@ -122,15 +122,19 @@
<Compile Include="Documentation\Examples\ConvertJsonToXml.cs" />
<Compile Include="Documentation\Examples\ConvertXmlToJson.cs" />
<Compile Include="Documentation\Examples\ConvertXmlToJsonForceArray.cs" />
<Compile Include="Documentation\Examples\DeserializeDataSet.cs" />
<Compile Include="Documentation\Examples\JsonObjectAttributeOverrideIEnumerable.cs" />
<Compile Include="Documentation\Examples\CustomJsonConverter.cs" />
<Compile Include="Documentation\Examples\CustomTraceWriter.cs" />
<Compile Include="Documentation\Examples\DeserializeFromBson.cs" />
<Compile Include="Documentation\Examples\DeserializeFromBsonCollection.cs" />
<Compile Include="Documentation\Examples\DeserializeObjectCreationHandling.cs" />
<Compile Include="Documentation\Examples\ErrorHandlingAttribute.cs" />
<Compile Include="Documentation\Examples\Linq\QueryJsonLinq.cs" />
<Compile Include="Documentation\Examples\Linq\QueryJson.cs" />
<Compile Include="Documentation\Examples\Linq\QueryJsonSelectTokenWithLinq.cs" />
<Compile Include="Documentation\Examples\Linq\QueryJsonSelectToken.cs" />
<Compile Include="Documentation\Examples\MaxDepth.cs" />
<Compile Include="Documentation\Examples\ReadJsonWithJsonTextReader.cs" />
<Compile Include="Documentation\Examples\Schema\JTokenValidateWithEvent.cs" />
<Compile Include="Documentation\Examples\Schema\JTokenIsValidWithMessages.cs" />
@@ -172,6 +176,7 @@
<Compile Include="Documentation\Examples\Linq\WriteToJsonFile.cs" />
<Compile Include="Documentation\Examples\Linq\ReadJson.cs" />
<Compile Include="Documentation\Examples\Schema\LoadJsonSchemaFromFile.cs" />
<Compile Include="Documentation\Examples\SerializeDataSet.cs" />
<Compile Include="Documentation\Examples\SerializeDictionary.cs" />
<Compile Include="Documentation\Examples\CustomContractResolver.cs" />
<Compile Include="Documentation\Examples\DataContractAndDataMember.cs" />
@@ -202,6 +207,7 @@
<Compile Include="Documentation\Examples\SerializeContractResolver.cs" />
<Compile Include="Documentation\Examples\SerializeDateTimeZoneHandling.cs" />
<Compile Include="Documentation\Examples\JsonConverterAttributeProperty.cs" />
<Compile Include="Documentation\Examples\SerializeRawJson.cs" />
<Compile Include="Documentation\Examples\SerializeSerializationBinder.cs" />
<Compile Include="Documentation\Examples\SerializeCollection.cs" />
<Compile Include="Documentation\Examples\SerializeDateFormatHandling.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
@@ -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",
""<Unbeantwortet>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
{
@@ -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)]