-Changed blank XML elements to be written as an empty string instead of null
-Added support for XML document type -Changed string JValue with a null value to be written as null instead of empty string -Fixed StringEnumConverter thread safety
This commit is contained in:
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
public class StringEnumConverter : JsonConverter
|
||||
{
|
||||
private readonly Dictionary<Type, BidirectionalDictionary<string, string>> _enumMemberNamesPerType = new Dictionary<Type, BidirectionalDictionary<string, string>>();
|
||||
private static readonly ThreadSafeStore<Type, BidirectionalDictionary<string, string>> EnumMemberNamesPerType = new ThreadSafeStore<Type, BidirectionalDictionary<string, string>>(InitializeEnumType);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the written enum text should be camel case.
|
||||
@@ -90,7 +90,7 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
else
|
||||
{
|
||||
BidirectionalDictionary<string, string> map = GetEnumNameMap(e.GetType());
|
||||
BidirectionalDictionary<string, string> map = EnumMemberNamesPerType.Get(e.GetType());
|
||||
|
||||
string[] names = enumName.Split(',');
|
||||
for (int i = 0; i < names.Length; i++)
|
||||
@@ -144,7 +144,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
string finalEnumText;
|
||||
|
||||
BidirectionalDictionary<string, string> map = GetEnumNameMap(t);
|
||||
BidirectionalDictionary<string, string> map = EnumMemberNamesPerType.Get(t);
|
||||
if (enumText.IndexOf(',') != -1)
|
||||
{
|
||||
string[] names = enumText.Split(',');
|
||||
@@ -190,52 +190,6 @@ namespace Newtonsoft.Json.Converters
|
||||
return resolvedEnumName;
|
||||
}
|
||||
|
||||
private BidirectionalDictionary<string, string> GetEnumNameMap(Type t)
|
||||
{
|
||||
BidirectionalDictionary<string, string> map;
|
||||
|
||||
if (!_enumMemberNamesPerType.TryGetValue(t, out map))
|
||||
{
|
||||
lock (_enumMemberNamesPerType)
|
||||
{
|
||||
if (_enumMemberNamesPerType.TryGetValue(t, out map))
|
||||
return map;
|
||||
|
||||
map = new BidirectionalDictionary<string, string>(
|
||||
StringComparer.OrdinalIgnoreCase,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (FieldInfo f in t.GetFields())
|
||||
{
|
||||
string n1 = f.Name;
|
||||
string n2;
|
||||
|
||||
#if !NET20
|
||||
n2 = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
|
||||
.Cast<EnumMemberAttribute>()
|
||||
.Select(a => a.Value)
|
||||
.SingleOrDefault() ?? f.Name;
|
||||
#else
|
||||
n2 = f.Name;
|
||||
#endif
|
||||
|
||||
string s;
|
||||
if (map.TryGetBySecond(n2, out s))
|
||||
{
|
||||
throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'."
|
||||
.FormatWith(CultureInfo.InvariantCulture, n2, t.Name));
|
||||
}
|
||||
|
||||
map.Set(n1, n2);
|
||||
}
|
||||
|
||||
_enumMemberNamesPerType[t] = map;
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified object type.
|
||||
/// </summary>
|
||||
@@ -251,5 +205,35 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
return t.IsEnum();
|
||||
}
|
||||
|
||||
private static BidirectionalDictionary<string, string> InitializeEnumType(Type type)
|
||||
{
|
||||
BidirectionalDictionary<string, string> map = new BidirectionalDictionary<string, string>(
|
||||
StringComparer.OrdinalIgnoreCase,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (FieldInfo f in type.GetFields())
|
||||
{
|
||||
string n1 = f.Name;
|
||||
string n2;
|
||||
|
||||
#if !NET20
|
||||
n2 = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
|
||||
.Cast<EnumMemberAttribute>()
|
||||
.Select(a => a.Value)
|
||||
.SingleOrDefault() ?? f.Name;
|
||||
#else
|
||||
n2 = f.Name;
|
||||
#endif
|
||||
|
||||
string s;
|
||||
if (map.TryGetBySecond(n2, out s))
|
||||
throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, n2, type.Name));
|
||||
|
||||
map.Set(n1, n2);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,6 @@ using System.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
|
||||
#region XmlNodeWrappers
|
||||
#if !NETFX_CORE && !PORTABLE && !PORTABLE40
|
||||
internal class XmlDocumentWrapper : XmlNodeWrapper, IXmlDocument
|
||||
@@ -81,7 +80,12 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public IXmlNode CreateXmlDeclaration(string version, string encoding, string standalone)
|
||||
{
|
||||
return new XmlNodeWrapper(_document.CreateXmlDeclaration(version, encoding, standalone));
|
||||
return new XmlDeclarationWrapper(_document.CreateXmlDeclaration(version, encoding, standalone));
|
||||
}
|
||||
|
||||
public IXmlNode CreateXmlDocumentType(string name, string publicId, string systemId, string internalSubset)
|
||||
{
|
||||
return new XmlDocumentTypeWrapper(_document.CreateDocumentType(name, publicId, systemId, null));
|
||||
}
|
||||
|
||||
public IXmlNode CreateProcessingInstruction(string target, string data)
|
||||
@@ -148,6 +152,11 @@ namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
return _element.GetPrefixOfNamespace(namespaceUri);
|
||||
}
|
||||
|
||||
public bool IsEmpty
|
||||
{
|
||||
get { return _element.IsEmpty; }
|
||||
}
|
||||
}
|
||||
|
||||
internal class XmlDeclarationWrapper : XmlNodeWrapper, IXmlDeclaration
|
||||
@@ -178,6 +187,42 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
}
|
||||
|
||||
internal class XmlDocumentTypeWrapper : XmlNodeWrapper, IXmlDocumentType
|
||||
{
|
||||
private readonly XmlDocumentType _documentType;
|
||||
|
||||
public XmlDocumentTypeWrapper(XmlDocumentType documentType)
|
||||
: base(documentType)
|
||||
{
|
||||
_documentType = documentType;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return _documentType.Name; }
|
||||
}
|
||||
|
||||
public string System
|
||||
{
|
||||
get { return _documentType.SystemId; }
|
||||
}
|
||||
|
||||
public string Public
|
||||
{
|
||||
get { return _documentType.PublicId; }
|
||||
}
|
||||
|
||||
public string InternalSubset
|
||||
{
|
||||
get { return _documentType.InternalSubset; }
|
||||
}
|
||||
|
||||
public new string LocalName
|
||||
{
|
||||
get { return "DOCTYPE"; }
|
||||
}
|
||||
}
|
||||
|
||||
internal class XmlNodeWrapper : IXmlNode
|
||||
{
|
||||
private readonly XmlNode _node;
|
||||
@@ -215,6 +260,8 @@ namespace Newtonsoft.Json.Converters
|
||||
return new XmlElementWrapper((XmlElement)node);
|
||||
case XmlNodeType.XmlDeclaration:
|
||||
return new XmlDeclarationWrapper((XmlDeclaration)node);
|
||||
case XmlNodeType.DocumentType:
|
||||
return new XmlDocumentTypeWrapper((XmlDocumentType)node);
|
||||
default:
|
||||
return new XmlNodeWrapper(node);
|
||||
}
|
||||
@@ -277,6 +324,7 @@ namespace Newtonsoft.Json.Converters
|
||||
IXmlNode CreateWhitespace(string text);
|
||||
IXmlNode CreateSignificantWhitespace(string text);
|
||||
IXmlNode CreateXmlDeclaration(string version, string encoding, string standalone);
|
||||
IXmlNode CreateXmlDocumentType(string name, string publicId, string systemId, string internalSubset);
|
||||
IXmlNode CreateProcessingInstruction(string target, string data);
|
||||
IXmlElement CreateElement(string elementName);
|
||||
IXmlElement CreateElement(string qualifiedName, string namespaceUri);
|
||||
@@ -293,10 +341,19 @@ namespace Newtonsoft.Json.Converters
|
||||
string Standalone { get; set; }
|
||||
}
|
||||
|
||||
internal interface IXmlDocumentType : IXmlNode
|
||||
{
|
||||
string Name { get; }
|
||||
string System { get; }
|
||||
string Public { get; }
|
||||
string InternalSubset { get; }
|
||||
}
|
||||
|
||||
internal interface IXmlElement : IXmlNode
|
||||
{
|
||||
void SetAttributeNode(IXmlNode attribute);
|
||||
string GetPrefixOfNamespace(string namespaceUri);
|
||||
bool IsEmpty { get; }
|
||||
}
|
||||
|
||||
internal interface IXmlNode
|
||||
@@ -348,6 +405,42 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
}
|
||||
|
||||
internal class XDocumentTypeWrapper : XObjectWrapper, IXmlDocumentType
|
||||
{
|
||||
private readonly XDocumentType _documentType;
|
||||
|
||||
public XDocumentTypeWrapper(XDocumentType documentType)
|
||||
: base(documentType)
|
||||
{
|
||||
_documentType = documentType;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return _documentType.Name; }
|
||||
}
|
||||
|
||||
public string System
|
||||
{
|
||||
get { return _documentType.SystemId; }
|
||||
}
|
||||
|
||||
public string Public
|
||||
{
|
||||
get { return _documentType.PublicId; }
|
||||
}
|
||||
|
||||
public string InternalSubset
|
||||
{
|
||||
get { return _documentType.InternalSubset; }
|
||||
}
|
||||
|
||||
public new string LocalName
|
||||
{
|
||||
get { return "DOCTYPE"; }
|
||||
}
|
||||
}
|
||||
|
||||
internal class XDocumentWrapper : XContainerWrapper, IXmlDocument
|
||||
{
|
||||
private XDocument Document
|
||||
@@ -403,6 +496,11 @@ namespace Newtonsoft.Json.Converters
|
||||
return new XDeclarationWrapper(new XDeclaration(version, encoding, standalone));
|
||||
}
|
||||
|
||||
public IXmlNode CreateXmlDocumentType(string name, string publicId, string systemId, string internalSubset)
|
||||
{
|
||||
return new XDocumentTypeWrapper(new XDocumentType(name, publicId, systemId, internalSubset));
|
||||
}
|
||||
|
||||
public IXmlNode CreateProcessingInstruction(string target, string data)
|
||||
{
|
||||
return new XProcessingInstructionWrapper(new XProcessingInstruction(target, data));
|
||||
@@ -584,6 +682,8 @@ namespace Newtonsoft.Json.Converters
|
||||
return new XCommentWrapper((XComment)node);
|
||||
else if (node is XAttribute)
|
||||
return new XAttributeWrapper((XAttribute)node);
|
||||
else if (node is XDocumentType)
|
||||
return new XDocumentTypeWrapper((XDocumentType)node);
|
||||
else
|
||||
return new XObjectWrapper(node);
|
||||
}
|
||||
@@ -734,6 +834,11 @@ namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
return Element.GetPrefixOfNamespace(namespaceUri);
|
||||
}
|
||||
|
||||
public bool IsEmpty
|
||||
{
|
||||
get { return Element.IsEmpty; }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
@@ -868,6 +973,8 @@ namespace Newtonsoft.Json.Converters
|
||||
return ResolveFullName(node, manager);
|
||||
case XmlNodeType.ProcessingInstruction:
|
||||
return "?" + ResolveFullName(node, manager);
|
||||
case XmlNodeType.DocumentType:
|
||||
return "!" + ResolveFullName(node, manager);
|
||||
case XmlNodeType.XmlDeclaration:
|
||||
return DeclarationName;
|
||||
case XmlNodeType.SignificantWhitespace:
|
||||
@@ -990,8 +1097,13 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty(node.Attributes))
|
||||
{
|
||||
IXmlElement element = (IXmlElement)node;
|
||||
|
||||
// empty element
|
||||
writer.WriteNull();
|
||||
if (element.IsEmpty)
|
||||
writer.WriteNull();
|
||||
else
|
||||
writer.WriteValue(string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1055,6 +1167,34 @@ namespace Newtonsoft.Json.Converters
|
||||
writer.WriteValue(declaration.Standalone);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
break;
|
||||
case XmlNodeType.DocumentType:
|
||||
IXmlDocumentType documentType = (IXmlDocumentType)node;
|
||||
writer.WritePropertyName(GetPropertyName(node, manager));
|
||||
writer.WriteStartObject();
|
||||
|
||||
if (!string.IsNullOrEmpty(documentType.Name))
|
||||
{
|
||||
writer.WritePropertyName("@name");
|
||||
writer.WriteValue(documentType.Name);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(documentType.Public))
|
||||
{
|
||||
writer.WritePropertyName("@public");
|
||||
writer.WriteValue(documentType.Public);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(documentType.System))
|
||||
{
|
||||
writer.WritePropertyName("@system");
|
||||
writer.WriteValue(documentType.System);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(documentType.InternalSubset))
|
||||
{
|
||||
writer.WritePropertyName("@internalSubset");
|
||||
writer.WriteValue(documentType.InternalSubset);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
break;
|
||||
default:
|
||||
@@ -1157,6 +1297,10 @@ namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
CreateInstruction(reader, document, currentNode, propertyName);
|
||||
}
|
||||
else if (string.Equals(propertyName, "!DOCTYPE", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
CreateDocumentType(reader, document, currentNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (reader.TokenType == JsonToken.StartArray)
|
||||
@@ -1185,12 +1329,12 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
if (propertyName.StartsWith("@"))
|
||||
{
|
||||
var attributeName = propertyName.Substring(1);
|
||||
var attributeValue = reader.Value.ToString();
|
||||
string attributeName = propertyName.Substring(1);
|
||||
string attributeValue = reader.Value.ToString();
|
||||
|
||||
var attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
|
||||
string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName);
|
||||
|
||||
var attribute = (!string.IsNullOrEmpty(attributePrefix))
|
||||
IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix))
|
||||
? document.CreateAttribute(attributeName, manager.LookupNamespace(attributePrefix), attributeValue)
|
||||
: document.CreateAttribute(attributeName, attributeValue);
|
||||
|
||||
@@ -1449,6 +1593,41 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateDocumentType(JsonReader reader, IXmlDocument document, IXmlNode currentNode)
|
||||
{
|
||||
string name = null;
|
||||
string publicId = null;
|
||||
string systemId = null;
|
||||
string internalSubset = null;
|
||||
while (reader.Read() && reader.TokenType != JsonToken.EndObject)
|
||||
{
|
||||
switch (reader.Value.ToString())
|
||||
{
|
||||
case "@name":
|
||||
reader.Read();
|
||||
name = reader.Value.ToString();
|
||||
break;
|
||||
case "@public":
|
||||
reader.Read();
|
||||
publicId = reader.Value.ToString();
|
||||
break;
|
||||
case "@system":
|
||||
reader.Read();
|
||||
systemId = reader.Value.ToString();
|
||||
break;
|
||||
case "@internalSubset":
|
||||
reader.Read();
|
||||
internalSubset = reader.Value.ToString();
|
||||
break;
|
||||
default:
|
||||
throw new JsonSerializationException("Unexpected property name encountered while deserializing XmlDeclaration: " + reader.Value);
|
||||
}
|
||||
}
|
||||
|
||||
IXmlNode documentType = document.CreateXmlDocumentType(name, publicId, systemId, internalSubset);
|
||||
currentNode.AppendChild(documentType);
|
||||
}
|
||||
|
||||
private IXmlElement CreateElement(string elementName, IXmlDocument document, string elementPrefix, XmlNamespaceManager manager)
|
||||
{
|
||||
string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix);
|
||||
|
||||
Reference in New Issue
Block a user