-Enabled support for converting between XML and JSON on Windows Phone 7
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT
|
||||
#if !SILVERLIGHT || WINDOWS_PHONE
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml;
|
||||
@@ -35,7 +35,8 @@ using System.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
#region Wrappers
|
||||
#region XmlNodeWrappers
|
||||
#if !SILVERLIGHT
|
||||
internal class XmlDocumentWrapper : XmlNodeWrapper, IXmlDocument
|
||||
{
|
||||
private XmlDocument _document;
|
||||
@@ -262,7 +263,10 @@ namespace Newtonsoft.Json.Converters
|
||||
get { return _node.NamespaceURI; }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
#region Interfaces
|
||||
internal interface IXmlDocument : IXmlNode
|
||||
{
|
||||
IXmlNode CreateComment(string text);
|
||||
@@ -304,8 +308,10 @@ namespace Newtonsoft.Json.Converters
|
||||
string NamespaceURI { get; }
|
||||
object WrappedNode { get; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
#if !NET20
|
||||
#region XNodeWrappers
|
||||
#if !NET20
|
||||
internal class XDeclarationWrapper : XObjectWrapper, IXmlDeclaration
|
||||
{
|
||||
internal readonly XDeclaration _declaration;
|
||||
@@ -725,7 +731,7 @@ namespace Newtonsoft.Json.Converters
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Converts an <see cref="XmlNode"/> to and from JSON.
|
||||
/// Converts XML to and from JSON.
|
||||
/// </summary>
|
||||
public class XmlNodeConverter : JsonConverter
|
||||
{
|
||||
@@ -752,16 +758,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// <param name="value">The value.</param>
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
IXmlNode node;
|
||||
|
||||
if (value is XmlNode)
|
||||
node = new XmlNodeWrapper((XmlNode)value);
|
||||
#if !NET20
|
||||
else if (value is XObject)
|
||||
node = XContainerWrapper.WrapNode((XObject)value);
|
||||
#endif
|
||||
else
|
||||
throw new ArgumentException("Value must be an XmlNode", "value");
|
||||
IXmlNode node = WrapXml(value);
|
||||
|
||||
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
|
||||
PushParentNamespaces(node, manager);
|
||||
@@ -771,6 +768,20 @@ namespace Newtonsoft.Json.Converters
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
private IXmlNode WrapXml(object value)
|
||||
{
|
||||
#if !NET20
|
||||
if (value is XObject)
|
||||
return XContainerWrapper.WrapNode((XObject)value);
|
||||
#endif
|
||||
#if !SILVERLIGHT
|
||||
if (value is XmlNode)
|
||||
return new XmlNodeWrapper((XmlNode)value);
|
||||
#endif
|
||||
|
||||
throw new ArgumentException("Value must be an XML object.", "value");
|
||||
}
|
||||
|
||||
private void PushParentNamespaces(IXmlNode node, XmlNamespaceManager manager)
|
||||
{
|
||||
List<IXmlNode> parentElements = null;
|
||||
@@ -1022,20 +1033,11 @@ namespace Newtonsoft.Json.Converters
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
|
||||
IXmlDocument document;
|
||||
IXmlNode rootNode;
|
||||
IXmlDocument document = null;
|
||||
IXmlNode rootNode = null;
|
||||
|
||||
if (typeof (XmlNode).IsAssignableFrom(objectType))
|
||||
{
|
||||
if (objectType != typeof (XmlDocument))
|
||||
throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments");
|
||||
|
||||
XmlDocument d = new XmlDocument();
|
||||
document = new XmlDocumentWrapper(d);
|
||||
rootNode = document;
|
||||
}
|
||||
#if !NET20
|
||||
else if (typeof(XObject).IsAssignableFrom(objectType))
|
||||
if (typeof(XObject).IsAssignableFrom(objectType))
|
||||
{
|
||||
if (objectType != typeof (XDocument) && objectType != typeof (XElement))
|
||||
throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement.");
|
||||
@@ -1045,10 +1047,20 @@ namespace Newtonsoft.Json.Converters
|
||||
rootNode = document;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
#if !SILVERLIGHT
|
||||
if (typeof(XmlNode).IsAssignableFrom(objectType))
|
||||
{
|
||||
throw new JsonSerializationException("Unexpected type when converting XML: " + objectType);
|
||||
if (objectType != typeof (XmlDocument))
|
||||
throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments");
|
||||
|
||||
XmlDocument d = new XmlDocument();
|
||||
document = new XmlDocumentWrapper(d);
|
||||
rootNode = document;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (document == null || rootNode == null)
|
||||
throw new JsonSerializationException("Unexpected type when converting XML: " + objectType);
|
||||
|
||||
if (reader.TokenType != JsonToken.StartObject)
|
||||
throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object.");
|
||||
@@ -1414,12 +1426,14 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
if (typeof(XmlNode).IsAssignableFrom(valueType))
|
||||
return true;
|
||||
#if !NET20
|
||||
if (typeof(XObject).IsAssignableFrom(valueType))
|
||||
return true;
|
||||
#endif
|
||||
#if !SILVERLIGHT
|
||||
if (typeof(XmlNode).IsAssignableFrom(valueType))
|
||||
return true;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ using Newtonsoft.Json.Utilities;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using System.Text;
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE)
|
||||
using System.Xml.Linq;
|
||||
#endif
|
||||
|
||||
@@ -761,55 +761,6 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
/// <summary>
|
||||
/// Serializes the <see cref="XNode"/> to a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to convert to JSON.</param>
|
||||
/// <returns>A JSON string of the XNode.</returns>
|
||||
public static string SerializeXNode(XObject node)
|
||||
{
|
||||
return SerializeXNode(node, Formatting.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the <see cref="XNode"/> to a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to convert to JSON.</param>
|
||||
/// <param name="formatting">Indicates how the output is formatted.</param>
|
||||
/// <returns>A JSON string of the XNode.</returns>
|
||||
public static string SerializeXNode(XObject node, Formatting formatting)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
|
||||
return SerializeObject(node, formatting, converter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the <see cref="XNode"/> from a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <returns>The deserialized XNode</returns>
|
||||
public static XDocument DeserializeXNode(string value)
|
||||
{
|
||||
return DeserializeXNode(value, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the <see cref="XNode"/> from a JSON string nested in a root elment.
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <param name="deserializeRootElementName">The name of the root element to append when deserializing.</param>
|
||||
/// <returns>The deserialized XNode</returns>
|
||||
public static XDocument DeserializeXNode(string value, string deserializeRootElementName)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
converter.DeserializeRootElementName = deserializeRootElementName;
|
||||
|
||||
return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SILVERLIGHT
|
||||
/// <summary>
|
||||
/// Serializes the XML node to a JSON string.
|
||||
@@ -858,5 +809,54 @@ namespace Newtonsoft.Json
|
||||
return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE)
|
||||
/// <summary>
|
||||
/// Serializes the <see cref="XNode"/> to a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to convert to JSON.</param>
|
||||
/// <returns>A JSON string of the XNode.</returns>
|
||||
public static string SerializeXNode(XObject node)
|
||||
{
|
||||
return SerializeXNode(node, Formatting.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the <see cref="XNode"/> to a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to convert to JSON.</param>
|
||||
/// <param name="formatting">Indicates how the output is formatted.</param>
|
||||
/// <returns>A JSON string of the XNode.</returns>
|
||||
public static string SerializeXNode(XObject node, Formatting formatting)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
|
||||
return SerializeObject(node, formatting, converter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the <see cref="XNode"/> from a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <returns>The deserialized XNode</returns>
|
||||
public static XDocument DeserializeXNode(string value)
|
||||
{
|
||||
return DeserializeXNode(value, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the <see cref="XNode"/> from a JSON string nested in a root elment.
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <param name="deserializeRootElementName">The name of the root element to append when deserializing.</param>
|
||||
/// <returns>The deserialized XNode</returns>
|
||||
public static XDocument DeserializeXNode(string value, string deserializeRootElementName)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
converter.DeserializeRootElementName = deserializeRootElementName;
|
||||
|
||||
return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Net" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonBinaryType.cs" />
|
||||
|
||||
Reference in New Issue
Block a user