Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2010-12-08 03:04:27 +0300
committerJamesNK <james@newtonking.com>2010-12-08 03:04:27 +0300
commit2e7b8e5279722460eca81abb26fb58539d2d70d6 (patch)
tree00ddd7e00e4c0610773326fa0bf14b125a991ac5
parent117748ed3334deb97c9c4dbf3a06f74d083db9cc (diff)
-Enabled support for converting between XML and JSON on Windows Phone 7
-rw-r--r--Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs74
-rw-r--r--Src/Newtonsoft.Json/JsonConvert.cs74
-rw-r--r--Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj1
3 files changed, 82 insertions, 67 deletions
diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
index e8b5c22..d9b9292 100644
--- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
@@ -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;
-
- if (typeof (XmlNode).IsAssignableFrom(objectType))
- {
- if (objectType != typeof (XmlDocument))
- throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments");
+ IXmlDocument document = null;
+ IXmlNode rootNode = null;
- 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;
}
diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs
index 54b9458..38b56cd 100644
--- a/Src/Newtonsoft.Json/JsonConvert.cs
+++ b/Src/Newtonsoft.Json/JsonConvert.cs
@@ -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,24 +761,24 @@ namespace Newtonsoft.Json
}
}
-#if !NET20 && !SILVERLIGHT
+#if !SILVERLIGHT
/// <summary>
- /// Serializes the <see cref="XNode"/> to a JSON string.
+ /// Serializes the XML node 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)
+ /// <param name="node">The node to serialize.</param>
+ /// <returns>A JSON string of the XmlNode.</returns>
+ public static string SerializeXmlNode(XmlNode node)
{
- return SerializeXNode(node, Formatting.None);
+ return SerializeXmlNode(node, Formatting.None);
}
/// <summary>
- /// Serializes the <see cref="XNode"/> to a JSON string.
+ /// Serializes the XML node to a JSON string.
/// </summary>
- /// <param name="node">The node to convert to JSON.</param>
+ /// <param name="node">The node to serialize.</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)
+ /// <returns>A JSON string of the XmlNode.</returns>
+ public static string SerializeXmlNode(XmlNode node, Formatting formatting)
{
XmlNodeConverter converter = new XmlNodeConverter();
@@ -786,48 +786,48 @@ namespace Newtonsoft.Json
}
/// <summary>
- /// Deserializes the <see cref="XNode"/> from a JSON string.
+ /// Deserializes the XmlNode from a JSON string.
/// </summary>
/// <param name="value">The JSON string.</param>
- /// <returns>The deserialized XNode</returns>
- public static XDocument DeserializeXNode(string value)
+ /// <returns>The deserialized XmlNode</returns>
+ public static XmlDocument DeserializeXmlNode(string value)
{
- return DeserializeXNode(value, null);
+ return DeserializeXmlNode(value, null);
}
/// <summary>
- /// Deserializes the <see cref="XNode"/> from a JSON string nested in a root elment.
+ /// Deserializes the XmlNode 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)
+ /// <returns>The deserialized XmlNode</returns>
+ public static XmlDocument DeserializeXmlNode(string value, string deserializeRootElementName)
{
XmlNodeConverter converter = new XmlNodeConverter();
converter.DeserializeRootElementName = deserializeRootElementName;
- return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
+ return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter);
}
#endif
-#if !SILVERLIGHT
+#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE)
/// <summary>
- /// Serializes the XML node to a JSON string.
+ /// Serializes the <see cref="XNode"/> to a JSON string.
/// </summary>
- /// <param name="node">The node to serialize.</param>
- /// <returns>A JSON string of the XmlNode.</returns>
- public static string SerializeXmlNode(XmlNode node)
+ /// <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 SerializeXmlNode(node, Formatting.None);
+ return SerializeXNode(node, Formatting.None);
}
/// <summary>
- /// Serializes the XML node to a JSON string.
+ /// Serializes the <see cref="XNode"/> to a JSON string.
/// </summary>
- /// <param name="node">The node to serialize.</param>
+ /// <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 XmlNode.</returns>
- public static string SerializeXmlNode(XmlNode node, Formatting formatting)
+ /// <returns>A JSON string of the XNode.</returns>
+ public static string SerializeXNode(XObject node, Formatting formatting)
{
XmlNodeConverter converter = new XmlNodeConverter();
@@ -835,27 +835,27 @@ namespace Newtonsoft.Json
}
/// <summary>
- /// Deserializes the XmlNode from a JSON string.
+ /// Deserializes the <see cref="XNode"/> from a JSON string.
/// </summary>
/// <param name="value">The JSON string.</param>
- /// <returns>The deserialized XmlNode</returns>
- public static XmlDocument DeserializeXmlNode(string value)
+ /// <returns>The deserialized XNode</returns>
+ public static XDocument DeserializeXNode(string value)
{
- return DeserializeXmlNode(value, null);
+ return DeserializeXNode(value, null);
}
/// <summary>
- /// Deserializes the XmlNode from a JSON string nested in a root elment.
+ /// 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 XmlNode</returns>
- public static XmlDocument DeserializeXmlNode(string value, string deserializeRootElementName)
+ /// <returns>The deserialized XNode</returns>
+ public static XDocument DeserializeXNode(string value, string deserializeRootElementName)
{
XmlNodeConverter converter = new XmlNodeConverter();
converter.DeserializeRootElementName = deserializeRootElementName;
- return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter);
+ return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
}
#endif
}
diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj
index a686d4c..6604d0f 100644
--- a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj
+++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj
@@ -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" />