-Fixed error when deserializing nullable structs

-Fixed writing JSON Schema option label
-Improved error message when converting JSON with an empty property to XML
This commit is contained in:
JamesNK
2010-10-22 23:23:49 +00:00
parent 1703992773
commit 15ea91fb63
8 changed files with 283 additions and 58 deletions
@@ -1118,6 +1118,9 @@ namespace Newtonsoft.Json.Converters
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager)
{
if (string.IsNullOrEmpty(propertyName))
throw new JsonSerializationException("XmlNodeConverter cannot convert JSON with an empty property name to XML.");
Dictionary<string, string> attributeNameValues = ReadAttributeElements(reader, manager);
string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName);
@@ -1214,50 +1217,59 @@ namespace Newtonsoft.Json.Converters
case JsonToken.PropertyName:
string attributeName = reader.Value.ToString();
string attributeValue;
char firstChar = attributeName[0];
switch (firstChar)
if (!string.IsNullOrEmpty(attributeName))
{
case '@':
attributeName = attributeName.Substring(1);
reader.Read();
attributeValue = reader.Value.ToString();
attributeNameValues.Add(attributeName, attributeValue);
char firstChar = attributeName[0];
string namespacePrefix;
if (IsNamespaceAttribute(attributeName, out namespacePrefix))
{
manager.AddNamespace(namespacePrefix, attributeValue);
}
break;
case '$':
attributeName = attributeName.Substring(1);
reader.Read();
attributeValue = reader.Value.ToString();
switch (firstChar)
{
case '@':
attributeName = attributeName.Substring(1);
reader.Read();
attributeValue = reader.Value.ToString();
attributeNameValues.Add(attributeName, attributeValue);
// check that JsonNamespaceUri is in scope
// if it isn't then add it to document and namespace manager
string jsonPrefix = manager.LookupPrefix(JsonNamespaceUri);
if (jsonPrefix == null)
{
// ensure that the prefix used is free
int? i = null;
while (manager.LookupNamespace("json" + i) != null)
string namespacePrefix;
if (IsNamespaceAttribute(attributeName, out namespacePrefix))
{
i = i.GetValueOrDefault() + 1;
manager.AddNamespace(namespacePrefix, attributeValue);
}
jsonPrefix = "json" + i;
break;
case '$':
attributeName = attributeName.Substring(1);
reader.Read();
attributeValue = reader.Value.ToString();
attributeNameValues.Add("xmlns:" + jsonPrefix, JsonNamespaceUri);
manager.AddNamespace(jsonPrefix, JsonNamespaceUri);
}
// check that JsonNamespaceUri is in scope
// if it isn't then add it to document and namespace manager
string jsonPrefix = manager.LookupPrefix(JsonNamespaceUri);
if (jsonPrefix == null)
{
// ensure that the prefix used is free
int? i = null;
while (manager.LookupNamespace("json" + i) != null)
{
i = i.GetValueOrDefault() + 1;
}
jsonPrefix = "json" + i;
attributeNameValues.Add(jsonPrefix + ":" + attributeName, attributeValue);
break;
default:
finishedAttributes = true;
break;
attributeNameValues.Add("xmlns:" + jsonPrefix, JsonNamespaceUri);
manager.AddNamespace(jsonPrefix, JsonNamespaceUri);
}
attributeNameValues.Add(jsonPrefix + ":" + attributeName, attributeValue);
break;
default:
finishedAttributes = true;
break;
}
}
else
{
finishedAttributes = true;
}
break;
case JsonToken.EndObject:
finishedElement = true;
@@ -136,7 +136,7 @@ namespace Newtonsoft.Json.Schema
option.Key.WriteTo(_writer);
if (option.Value != null)
{
_writer.WritePropertyName(JsonSchemaConstants.OptionValuePropertyName);
_writer.WritePropertyName(JsonSchemaConstants.OptionLabelPropertyName);
_writer.WriteValue(option.Value);
}
_writer.WriteEndObject();
@@ -486,38 +486,40 @@ namespace Newtonsoft.Json.Serialization
/// <returns>A <see cref="JsonContract"/> for the given type.</returns>
protected virtual JsonContract CreateContract(Type objectType)
{
if (JsonConvert.IsJsonPrimitiveType(objectType))
return CreatePrimitiveContract(objectType);
Type t = ReflectionUtils.EnsureNotNullableType(objectType);
if (JsonTypeReflector.GetJsonObjectAttribute(objectType) != null)
return CreateObjectContract(objectType);
if (JsonConvert.IsJsonPrimitiveType(t))
return CreatePrimitiveContract(t);
if (JsonTypeReflector.GetJsonArrayAttribute(objectType) != null)
return CreateArrayContract(objectType);
if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
return CreateObjectContract(t);
if (objectType.IsSubclassOf(typeof(JToken)))
return CreateLinqContract(objectType);
if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
return CreateArrayContract(t);
if (CollectionUtils.IsDictionaryType(objectType))
return CreateDictionaryContract(objectType);
if (t.IsSubclassOf(typeof(JToken)))
return CreateLinqContract(t);
if (typeof(IEnumerable).IsAssignableFrom(objectType))
return CreateArrayContract(objectType);
if (CollectionUtils.IsDictionaryType(t))
return CreateDictionaryContract(t);
if (CanConvertToString(objectType))
return CreateStringContract(objectType);
if (typeof(IEnumerable).IsAssignableFrom(t))
return CreateArrayContract(t);
if (CanConvertToString(t))
return CreateStringContract(t);
#if !SILVERLIGHT && !PocketPC
if (typeof(ISerializable).IsAssignableFrom(objectType))
return CreateISerializableContract(objectType);
if (typeof(ISerializable).IsAssignableFrom(t))
return CreateISerializableContract(t);
#endif
#if !(NET35 || NET20 || SILVERLIGHT)
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(objectType))
return CreateDynamicContract(objectType);
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(t))
return CreateDynamicContract(t);
#endif
return CreateObjectContract(objectType);
return CreateObjectContract(t);
}
internal static bool CanConvertToString(Type type)
@@ -169,6 +169,13 @@ namespace Newtonsoft.Json.Utilities
return (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
public static Type EnsureNotNullableType(Type t)
{
return (IsNullableType(t))
? Nullable.GetUnderlyingType(t)
: t;
}
//public static bool IsValueTypeUnitializedValue(ValueType value)
//{
// if (value == null)