-Fixed generating generic simple type names

-Changed JsonObjectAttribute to be compatible with structs
This commit is contained in:
JamesNK
2010-07-06 10:20:10 +00:00
parent c2800930ab
commit 08b8477f5e
9 changed files with 117 additions and 4 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Instructs the <see cref="JsonSerializer"/> how to serialize the object.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)]
public sealed class JsonObjectAttribute : JsonContainerAttribute
{
private MemberSerialization _memberSerialization = MemberSerialization.OptOut;
+2 -2
View File
@@ -161,10 +161,10 @@ namespace Newtonsoft.Json.Linq
/// </returns>
public override JEnumerable<JToken> Children()
{
return new JEnumerable<JToken>(ChildrenInternal());
return new JEnumerable<JToken>(GetValueEnumerable());
}
private IEnumerable<JToken> ChildrenInternal()
private IEnumerable<JToken> GetValueEnumerable()
{
yield return Value;
}
@@ -30,6 +30,8 @@ using System.Collections;
using System.Linq;
using System.Globalization;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Text.RegularExpressions;
namespace Newtonsoft.Json.Utilities
{
@@ -45,7 +47,7 @@ namespace Newtonsoft.Json.Utilities
switch (assemblyFormat)
{
case FormatterAssemblyStyle.Simple:
return t.FullName + ", " + t.Assembly.GetName().Name;
return GetSimpleTypeName(t);
case FormatterAssemblyStyle.Full:
return t.AssemblyQualifiedName;
default:
@@ -53,6 +55,55 @@ namespace Newtonsoft.Json.Utilities
}
}
private static string GetSimpleTypeName(Type type)
{
string fullyQualifiedTypeName = type.FullName + ", " + type.Assembly.GetName().Name;
// for type names with no nested type names then return
if (!type.IsGenericType || type.IsGenericTypeDefinition)
return fullyQualifiedTypeName;
StringBuilder builder = new StringBuilder();
// loop through the type name and filter out qualified assembly details from nested type names
bool writingAssemblyName = false;
bool skippingAssemblyDetails = false;
for (int i = 0; i < fullyQualifiedTypeName.Length; i++)
{
char current = fullyQualifiedTypeName[i];
switch (current)
{
case '[':
writingAssemblyName = false;
skippingAssemblyDetails = false;
builder.Append(current);
break;
case ']':
writingAssemblyName = false;
skippingAssemblyDetails = false;
builder.Append(current);
break;
case ',':
if (!writingAssemblyName)
{
writingAssemblyName = true;
builder.Append(current);
}
else
{
skippingAssemblyDetails = true;
}
break;
default:
if (!skippingAssemblyDetails)
builder.Append(current);
break;
}
}
return builder.ToString();
}
public static bool IsInstantiatableType(Type t)
{
ValidationUtils.ArgumentNotNull(t, "t");