-Fixed converting dollar JSON properties to XML
This commit is contained in:
@@ -130,6 +130,36 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
|
||||
#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 string MyProperty { get; set; }
|
||||
|
||||
@@ -23,10 +23,9 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !(NET20 || NET35 || NET40 || NETFX_CORE || PORTABLE || PORTABLE40 || DNXCORE50)
|
||||
#if !(NET20 || NET35 || NET40 || NETFX_CORE || PORTABLE40 || DNXCORE50)
|
||||
using System.Xml;
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
@@ -258,6 +257,7 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
#if !(PORTABLE40)
|
||||
#if !(PORTABLE)
|
||||
[Test]
|
||||
public void ConvertXmlNode()
|
||||
{
|
||||
@@ -269,6 +269,7 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
JsonConvert.SerializeXmlNode(doc);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void ConvertXNode()
|
||||
|
||||
@@ -1540,27 +1540,31 @@ namespace Newtonsoft.Json.Converters
|
||||
string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
|
||||
|
||||
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);
|
||||
elementPrefix = manager.LookupPrefix(JsonNamespaceUri);
|
||||
CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues);
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
case JsonTypeReflector.IdPropertyName:
|
||||
case JsonTypeReflector.RefPropertyName:
|
||||
case JsonTypeReflector.TypePropertyName:
|
||||
case JsonTypeReflector.ValuePropertyName:
|
||||
string attributeName = propertyName.Substring(1);
|
||||
string attributePrefix = manager.LookupPrefix(JsonNamespaceUri);
|
||||
AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -1776,6 +1780,13 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
break;
|
||||
case '$':
|
||||
switch (attributeName)
|
||||
{
|
||||
case JsonTypeReflector.ArrayValuesPropertyName:
|
||||
case JsonTypeReflector.IdPropertyName:
|
||||
case JsonTypeReflector.RefPropertyName:
|
||||
case JsonTypeReflector.TypePropertyName:
|
||||
case JsonTypeReflector.ValuePropertyName:
|
||||
// check that JsonNamespaceUri is in scope
|
||||
// if it isn't then add it to document and namespace manager
|
||||
string jsonPrefix = manager.LookupPrefix(JsonNamespaceUri);
|
||||
@@ -1815,6 +1826,11 @@ namespace Newtonsoft.Json.Converters
|
||||
finishedAttributes = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
finishedAttributes = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user