-Documentation

This commit is contained in:
James Newton-King
2014-10-20 20:18:22 +13:00
parent ac98329006
commit 70d3e9327d
2 changed files with 42 additions and 0 deletions
@@ -19,6 +19,7 @@ using NUnit.Framework;
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
{ {
#region Types
public class XmlJsonReader : JsonReader public class XmlJsonReader : JsonReader
{ {
private readonly Stack<JTokenType> _stateStack; private readonly Stack<JTokenType> _stateStack;
@@ -217,11 +218,13 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
return (Value != null) ? (DateTimeOffset?)Convert.ToDateTime(Value) : null; return (Value != null) ? (DateTimeOffset?)Convert.ToDateTime(Value) : null;
} }
} }
#endregion
public class CustomJsonReader public class CustomJsonReader
{ {
public void Example() public void Example()
{ {
#region Usage
string xml = @"<Root type=""Object""> string xml = @"<Root type=""Object"">
<Null type=""Null"" /> <Null type=""Null"" />
<String type=""String"">This is a string!</String> <String type=""String"">This is a string!</String>
@@ -280,6 +283,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
// "Constructor": new Date(2000, 12, 30) // "Constructor": new Date(2000, 12, 30)
//} //}
} }
#endregion
using (XmlReader xmlReader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { IgnoreWhitespace = true })) using (XmlReader xmlReader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { IgnoreWhitespace = true }))
using (XmlJsonReader reader = new XmlJsonReader(xmlReader)) using (XmlJsonReader reader = new XmlJsonReader(xmlReader))
@@ -9,6 +9,7 @@ using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
{ {
#region Types
public class XmlJsonWriter : JsonWriter public class XmlJsonWriter : JsonWriter
{ {
private readonly XmlWriter _writer; private readonly XmlWriter _writer;
@@ -272,13 +273,50 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
} }
} }
} }
#endregion
public class CustomJsonWriter public class CustomJsonWriter
{ {
public void Example() public void Example()
{ {
#region Usage
var user = new
{
Name = "James",
Age = 30,
Enabled = true,
Roles = new[]
{
"Publisher",
"Administrator"
}
};
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
using (XmlJsonWriter writer = new XmlJsonWriter(xmlWriter))
{
writer.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, user);
}
Console.WriteLine(sw.ToString());
//<Root type="Object">
// <Name type="String">James</Name>
// <Age type="Integer">30</Age>
// <Enabled type="Boolean">true</Enabled>
// <Roles type="Array">
// <Item type="String">Publisher</Item>
// <Item type="String">Administrator</Item>
// </Roles>
//</Root>
#endregion
sw = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true })) using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
using (XmlJsonWriter writer = new XmlJsonWriter(xmlWriter)) using (XmlJsonWriter writer = new XmlJsonWriter(xmlWriter))
{ {