-Fixed converting dollar JSON properties to XML

This commit is contained in:
James Newton-King
2016-04-29 23:19:42 +12:00
parent ad7752fedf
commit b71ca75905
3 changed files with 95 additions and 48 deletions
@@ -130,6 +130,36 @@ namespace Newtonsoft.Json.Tests.Converters
} }
#if !NET20 #if !NET20
[Test]
public void SerializeDollarProperty()
{
string json1 = @"{""$"":""test""}";
var doc = JsonConvert.DeserializeXNode(json1);
Assert.AreEqual(@"<_x0024_>test</_x0024_>", doc.ToString());
var json2 = JsonConvert.SerializeXNode(doc);
Assert.AreEqual(json1, json2);
}
[Test]
public void SerializeNonKnownDollarProperty()
{
string json1 = @"{""$JELLY"":""test""}";
var doc = JsonConvert.DeserializeXNode(json1);
Console.WriteLine(doc.ToString());
Assert.AreEqual(@"<_x0024_JELLY>test</_x0024_JELLY>", doc.ToString());
var json2 = JsonConvert.SerializeXNode(doc);
Assert.AreEqual(json1, json2);
}
public class MyModel public class MyModel
{ {
public string MyProperty { get; set; } public string MyProperty { get; set; }
@@ -23,10 +23,9 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#endregion #endregion
#if !(NET20 || NET35 || NET40 || NETFX_CORE || PORTABLE || PORTABLE40 || DNXCORE50) #if !(NET20 || NET35 || NET40 || NETFX_CORE || PORTABLE40 || DNXCORE50)
using System.Xml; using System.Xml;
using System; using System;
using System.Buffers;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@@ -258,6 +257,7 @@ namespace Newtonsoft.Json.Tests
} }
#if !(PORTABLE40) #if !(PORTABLE40)
#if !(PORTABLE)
[Test] [Test]
public void ConvertXmlNode() public void ConvertXmlNode()
{ {
@@ -269,6 +269,7 @@ namespace Newtonsoft.Json.Tests
JsonConvert.SerializeXmlNode(doc); JsonConvert.SerializeXmlNode(doc);
} }
#endif
[Test] [Test]
public void ConvertXNode() public void ConvertXNode()
@@ -1540,27 +1540,31 @@ namespace Newtonsoft.Json.Converters
string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName); string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix); AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix);
return;
} }
else if (propertyName.StartsWith('$'))
if (propertyName.StartsWith('$'))
{ {
if (propertyName == JsonTypeReflector.ArrayValuesPropertyName) switch (propertyName)
{ {
case JsonTypeReflector.ArrayValuesPropertyName:
propertyName = propertyName.Substring(1); propertyName = propertyName.Substring(1);
elementPrefix = manager.LookupPrefix(JsonNamespaceUri); elementPrefix = manager.LookupPrefix(JsonNamespaceUri);
CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues); CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues);
} return;
else case JsonTypeReflector.IdPropertyName:
{ case JsonTypeReflector.RefPropertyName:
case JsonTypeReflector.TypePropertyName:
case JsonTypeReflector.ValuePropertyName:
string attributeName = propertyName.Substring(1); string attributeName = propertyName.Substring(1);
string attributePrefix = manager.LookupPrefix(JsonNamespaceUri); string attributePrefix = manager.LookupPrefix(JsonNamespaceUri);
AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix); AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix);
return;
} }
} }
else
{
CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues); CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues);
} }
}
private void CreateElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string elementName, XmlNamespaceManager manager, string elementPrefix, Dictionary<string, string> attributeNameValues) private void CreateElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string elementName, XmlNamespaceManager manager, string elementPrefix, Dictionary<string, string> attributeNameValues)
{ {
@@ -1776,6 +1780,13 @@ namespace Newtonsoft.Json.Converters
} }
break; break;
case '$': case '$':
switch (attributeName)
{
case JsonTypeReflector.ArrayValuesPropertyName:
case JsonTypeReflector.IdPropertyName:
case JsonTypeReflector.RefPropertyName:
case JsonTypeReflector.TypePropertyName:
case JsonTypeReflector.ValuePropertyName:
// check that JsonNamespaceUri is in scope // check that JsonNamespaceUri is in scope
// if it isn't then add it to document and namespace manager // if it isn't then add it to document and namespace manager
string jsonPrefix = manager.LookupPrefix(JsonNamespaceUri); string jsonPrefix = manager.LookupPrefix(JsonNamespaceUri);
@@ -1815,6 +1826,11 @@ namespace Newtonsoft.Json.Converters
finishedAttributes = true; finishedAttributes = true;
break; break;
} }
break;
default:
finishedAttributes = true;
break;
}
} }
else else
{ {