-Formatting
-Build
This commit is contained in:
@@ -36,13 +36,15 @@ using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
|
||||
#endif
|
||||
|
||||
#if !(DNXCORE50 || NET20)
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
{
|
||||
#region Types
|
||||
|
||||
#region Types
|
||||
public class XmlJsonReader : JsonReader
|
||||
{
|
||||
private readonly Stack<JTokenType> _stateStack;
|
||||
@@ -65,7 +67,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override bool Read()
|
||||
{
|
||||
if (HandleValueType())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
while (_reader.Read())
|
||||
{
|
||||
@@ -74,7 +78,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
case XmlNodeType.Element:
|
||||
string typeName = _reader.GetAttribute("type");
|
||||
if (typeName == null)
|
||||
{
|
||||
throw new Exception("No type specified.");
|
||||
}
|
||||
|
||||
_valueType = (JTokenType)Enum.Parse(typeof(JTokenType), typeName, true);
|
||||
|
||||
@@ -100,25 +106,33 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
SetToken(JsonToken.EndObject);
|
||||
_stateStack.Pop();
|
||||
if (PeekState() == JTokenType.Property)
|
||||
{
|
||||
_stateStack.Pop();
|
||||
}
|
||||
return true;
|
||||
case JTokenType.Array:
|
||||
SetToken(JsonToken.EndArray);
|
||||
_stateStack.Pop();
|
||||
if (PeekState() == JTokenType.Property)
|
||||
{
|
||||
_stateStack.Pop();
|
||||
}
|
||||
return true;
|
||||
case JTokenType.Constructor:
|
||||
SetToken(JsonToken.EndConstructor);
|
||||
_stateStack.Pop();
|
||||
if (PeekState() == JTokenType.Property)
|
||||
{
|
||||
_stateStack.Pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
_stateStack.Pop();
|
||||
if (PeekState() == JTokenType.Property)
|
||||
{
|
||||
_stateStack.Pop();
|
||||
}
|
||||
|
||||
break;
|
||||
case XmlNodeType.Text:
|
||||
@@ -168,7 +182,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
_valueType = null;
|
||||
|
||||
if (PeekState() == JTokenType.Property)
|
||||
{
|
||||
_stateStack.Pop();
|
||||
}
|
||||
return true;
|
||||
case JTokenType.Object:
|
||||
SetToken(JsonToken.StartObject);
|
||||
@@ -183,7 +199,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
case JTokenType.Constructor:
|
||||
string constructorName = _reader.GetAttribute("name");
|
||||
if (constructorName == null)
|
||||
{
|
||||
throw new Exception("No constructor name specified.");
|
||||
}
|
||||
|
||||
SetToken(JsonToken.StartConstructor, constructorName);
|
||||
_stateStack.Push(JTokenType.Constructor);
|
||||
@@ -196,7 +214,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override int? ReadAsInt32()
|
||||
{
|
||||
if (!Read())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Value != null) ? (int?)Convert.ToInt32(Value) : null;
|
||||
}
|
||||
@@ -204,7 +224,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override string ReadAsString()
|
||||
{
|
||||
if (!Read())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string)Value;
|
||||
}
|
||||
@@ -212,7 +234,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override byte[] ReadAsBytes()
|
||||
{
|
||||
if (!Read())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (byte[])Value;
|
||||
}
|
||||
@@ -220,7 +244,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override decimal? ReadAsDecimal()
|
||||
{
|
||||
if (!Read())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Value != null) ? (decimal?)Convert.ToDecimal(Value) : null;
|
||||
}
|
||||
@@ -228,7 +254,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override DateTime? ReadAsDateTime()
|
||||
{
|
||||
if (!Read())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Value != null) ? (DateTime?)Convert.ToDateTime(Value) : null;
|
||||
}
|
||||
@@ -236,12 +264,14 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
public override DateTimeOffset? ReadAsDateTimeOffset()
|
||||
{
|
||||
if (!Read())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (Value != null) ? (DateTimeOffset?)Convert.ToDateTime(Value) : null;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
[TestFixture]
|
||||
public class CustomJsonReader : TestFixtureBase
|
||||
@@ -249,7 +279,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
#region Usage
|
||||
string xml = @"<Root type=""Object"">
|
||||
<Null type=""Null"" />
|
||||
<String type=""String"">This is a string!</String>
|
||||
@@ -308,7 +338,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
// "Constructor": new Date(2000, 12, 30)
|
||||
//}
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
using (XmlReader xmlReader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { IgnoreWhitespace = true }))
|
||||
using (XmlJsonReader reader = new XmlJsonReader(xmlReader))
|
||||
|
||||
@@ -36,13 +36,15 @@ using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
|
||||
#endif
|
||||
|
||||
#if !(DNXCORE50 || NET20)
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
{
|
||||
#region Types
|
||||
|
||||
#region Types
|
||||
public class XmlJsonWriter : JsonWriter
|
||||
{
|
||||
private readonly XmlWriter _writer;
|
||||
@@ -234,9 +236,13 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
base.WriteStartArray();
|
||||
|
||||
if (isStart)
|
||||
{
|
||||
WriteValueElement("Root", JTokenType.Array);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteValueElement(JTokenType.Array);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteStartObject()
|
||||
@@ -246,9 +252,13 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
base.WriteStartObject();
|
||||
|
||||
if (isStart)
|
||||
{
|
||||
WriteValueElement("Root", JTokenType.Object);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteValueElement(JTokenType.Object);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteStartConstructor(string name)
|
||||
@@ -258,9 +268,13 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
base.WriteStartConstructor(name);
|
||||
|
||||
if (isStart)
|
||||
{
|
||||
WriteValueElement("Root", JTokenType.Constructor);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteValueElement(JTokenType.Constructor);
|
||||
}
|
||||
|
||||
_writer.WriteAttributeString("name", name);
|
||||
}
|
||||
@@ -306,7 +320,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
[TestFixture]
|
||||
public class CustomJsonWriter : TestFixtureBase
|
||||
@@ -314,17 +328,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
#region Usage
|
||||
var user = new
|
||||
{
|
||||
Name = "James",
|
||||
Age = 30,
|
||||
Enabled = true,
|
||||
Roles = new[]
|
||||
{
|
||||
"Publisher",
|
||||
"Administrator"
|
||||
}
|
||||
{
|
||||
"Publisher",
|
||||
"Administrator"
|
||||
}
|
||||
};
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
@@ -348,7 +362,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
// <Item type="String">Administrator</Item>
|
||||
// </Roles>
|
||||
//</Root>
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
sw = new StringWriter();
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
@@ -59,9 +60,13 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.Value != null)
|
||||
{
|
||||
Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Token: {0}", reader.TokenType);
|
||||
}
|
||||
}
|
||||
|
||||
// Token: StartObject
|
||||
|
||||
+3
@@ -33,6 +33,7 @@ using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
@@ -61,7 +62,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
while (true)
|
||||
{
|
||||
if (!reader.Read())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
Role role = serializer.Deserialize<Role>(reader);
|
||||
|
||||
@@ -33,6 +33,7 @@ using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
|
||||
|
||||
Reference in New Issue
Block a user