-Minor fixes

This commit is contained in:
James Newton-King
2016-05-29 01:26:00 +12:00
parent 52fbb613d7
commit 1ae2912ee7
6 changed files with 23 additions and 47 deletions
@@ -503,7 +503,7 @@ If attributes are not mentioned, default values are used in each case.
{
for (int i = 0; i < interations; i++)
{
using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(text) ?? 16))
using (StringWriter w = StringUtils.CreateStringWriter(text.Length))
{
char[] buffer = null;
JavaScriptUtils.WriteEscapedJavaScriptString(w, text, '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, null, ref buffer);
@@ -57,7 +57,7 @@ namespace Newtonsoft.Json.Converters
public string DateTimeFormat
{
get { return _dateTimeFormat ?? string.Empty; }
set { _dateTimeFormat = StringUtils.NullEmptyString(value); }
set { _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; }
}
/// <summary>
@@ -70,23 +70,23 @@ namespace Newtonsoft.Json
public object[] ItemConverterParameters { get; set; }
/// <summary>
/// Gets the <see cref="Type"/> of the <see cref="Serialization.NamingStrategy"/>.
/// Gets the <see cref="Type"/> of the <see cref="NamingStrategy"/>.
/// </summary>
/// <value>The <see cref="Type"/> of the <see cref="Serialization.NamingStrategy"/>.</value>
/// <value>The <see cref="Type"/> of the <see cref="NamingStrategy"/>.</value>
public Type NamingStrategyType
{
get { return _namingStrategyType; }
set
{
_namingStrategyType = value;
NamingStrategy = null;
NamingStrategyInstance = null;
}
}
/// <summary>
/// The parameter list to use when constructing the <see cref="Serialization.NamingStrategy"/> described by NamingStrategyType.
/// The parameter list to use when constructing the <see cref="NamingStrategy"/> described by NamingStrategyType.
/// If null, the default constructor is used.
/// When non-null, there must be a constructor defined in the <see cref="Serialization.NamingStrategy"/> that exactly matches the number,
/// When non-null, there must be a constructor defined in the <see cref="NamingStrategy"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
@@ -98,11 +98,11 @@ namespace Newtonsoft.Json
set
{
_namingStrategyParameters = value;
NamingStrategy = null;
NamingStrategyInstance = null;
}
}
internal NamingStrategy NamingStrategy { get; set; }
internal NamingStrategy NamingStrategyInstance { get; set; }
// yuck. can't set nullable properties on an attribute in C#
// have to use this approach to get an unset default state
@@ -192,18 +192,17 @@ namespace Newtonsoft.Json.Serialization
public static NamingStrategy GetContainerNamingStrategy(JsonContainerAttribute containerAttribute)
{
if (containerAttribute.NamingStrategy == null)
if (containerAttribute.NamingStrategyInstance == null)
{
if (containerAttribute.NamingStrategyType == null)
{
return null;
}
containerAttribute.NamingStrategy = CreateNamingStrategyInstance(containerAttribute.NamingStrategyType, containerAttribute.NamingStrategyParameters);
containerAttribute.NamingStrategyInstance = CreateNamingStrategyInstance(containerAttribute.NamingStrategyType, containerAttribute.NamingStrategyParameters);
}
return containerAttribute.NamingStrategy;
return containerAttribute.NamingStrategyInstance;
}
private static Func<object[], object> GetCreator(Type type)
@@ -307,7 +307,7 @@ namespace Newtonsoft.Json.Utilities
{
bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter);
using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(value) ?? 16))
using (StringWriter w = StringUtils.CreateStringWriter(value?.Length ?? 16))
{
char[] buffer = null;
WriteEscapedJavaScriptString(w, value, delimiter, appendDelimiters, charEscapeFlags, stringEscapeHandling, null, ref buffer);
+10 -33
View File
@@ -33,7 +33,6 @@ using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json.Utilities
{
@@ -104,16 +103,6 @@ namespace Newtonsoft.Json.Utilities
return true;
}
/// <summary>
/// Nulls an empty string.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>Null if the string was null, otherwise the string unchanged.</returns>
public static string NullEmptyString(string s)
{
return (string.IsNullOrEmpty(s)) ? null : s;
}
public static StringWriter CreateStringWriter(int capacity)
{
StringBuilder sb = new StringBuilder(capacity);
@@ -122,18 +111,6 @@ namespace Newtonsoft.Json.Utilities
return sw;
}
public static int? GetLength(string value)
{
if (value == null)
{
return null;
}
else
{
return value.Length;
}
}
public static void ToCharAsUnicode(char c, char[] buffer)
{
buffer[0] = '\\';
@@ -233,17 +210,17 @@ namespace Newtonsoft.Json.Utilities
{
switch (state)
{
case SnakeCaseState.Upper:
bool hasNext = (i + 1 < s.Length);
if (i > 0 && hasNext && !char.IsUpper(s[i + 1]))
{
case SnakeCaseState.Upper:
bool hasNext = (i + 1 < s.Length);
if (i > 0 && hasNext && !char.IsUpper(s[i + 1]))
{
sb.Append('_');
}
break;
case SnakeCaseState.Lower:
case SnakeCaseState.NewWord:
sb.Append('_');
}
break;
case SnakeCaseState.Lower:
case SnakeCaseState.NewWord:
sb.Append('_');
break;
break;
}
char c;