updated SimpleJson to v0.34

This commit is contained in:
Brendan Forster
2014-03-05 21:48:42 +11:00
parent 4ca19e52bd
commit 5a6df4afb4
7 changed files with 201 additions and 82 deletions
+67 -27
View File
@@ -17,7 +17,7 @@
// <website>https://github.com/facebook-csharp-sdk/simple-json</website>
//-----------------------------------------------------------------------
// VERSION: 0.30.0
// VERSION: 0.34.0
// NOTE: uncomment the following line to make SimpleJson class internal.
//#define SIMPLE_JSON_INTERNAL
@@ -31,6 +31,9 @@
// NOTE: uncomment the following line to enable DataContract support.
//#define SIMPLE_JSON_DATACONTRACT
// NOTE: uncomment the following line to enable IReadOnlyCollection<T> and IReadOnlyList<T> support.
//#define SIMPLE_JSON_READONLY_COLLECTIONS
// NOTE: uncomment the following line to disable linq expressions/compiled lambda (better performance) instead of method.invoke().
// define if you are using .net framework <= 3.0 or < WP7.5
//#define SIMPLE_JSON_NO_LINQ_EXPRESSION
@@ -51,7 +54,6 @@ using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if !SIMPLE_JSON_NO_LINQ_EXPRESSION
using System.Linq.Expressions;
#endif
@@ -513,6 +515,22 @@ namespace Octokit
private const int TOKEN_NULL = 11;
private const int BUILDER_CAPACITY = 2000;
private static readonly char[] EscapeTable;
private static readonly char[] EscapeCharacters = new char[] { '"', '\\', '\b', '\f', '\n', '\r', '\t' };
private static readonly string EscapeCharactersString = new string(EscapeCharacters);
static SimpleJson()
{
EscapeTable = new char[93];
EscapeTable['"'] = '"';
EscapeTable['\\'] = '\\';
EscapeTable['\b'] = 'b';
EscapeTable['\f'] = 'f';
EscapeTable['\n'] = 'n';
EscapeTable['\r'] = 'r';
EscapeTable['\t'] = 't';
}
/// <summary>
/// Parses the string json into a value
/// </summary>
@@ -1074,29 +1092,50 @@ namespace Octokit
static bool SerializeString(string aString, StringBuilder builder)
{
builder.Append("\"");
// Happy path if there's nothing to be escaped. IndexOfAny is highly optimized (and unmanaged)
if (aString.IndexOfAny(EscapeCharacters) == -1)
{
builder.Append('"');
builder.Append(aString);
builder.Append('"');
return true;
}
builder.Append('"');
int safeCharacterCount = 0;
char[] charArray = aString.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
char c = charArray[i];
if (c == '"')
builder.Append("\\\"");
else if (c == '\\')
builder.Append("\\\\");
else if (c == '\b')
builder.Append("\\b");
else if (c == '\f')
builder.Append("\\f");
else if (c == '\n')
builder.Append("\\n");
else if (c == '\r')
builder.Append("\\r");
else if (c == '\t')
builder.Append("\\t");
// Non ascii characters are fine, buffer them up and send them to the builder
// in larger chunks if possible. The escape table is a 1:1 translation table
// with \0 [default(char)] denoting a safe character.
if (c >= EscapeTable.Length || EscapeTable[c] == default(char))
{
safeCharacterCount++;
}
else
builder.Append(c);
{
if (safeCharacterCount > 0)
{
builder.Append(charArray, i - safeCharacterCount, safeCharacterCount);
safeCharacterCount = 0;
}
builder.Append('\\');
builder.Append(EscapeTable[c]);
}
}
builder.Append("\"");
if (safeCharacterCount > 0)
{
builder.Append(charArray, charArray.Length - safeCharacterCount, safeCharacterCount);
}
builder.Append('"');
return true;
}
@@ -1647,7 +1686,14 @@ namespace Octokit
Type genericDefinition = type.GetGenericTypeDefinition();
return (genericDefinition == typeof(IList<>) || genericDefinition == typeof(ICollection<>) || genericDefinition == typeof(IEnumerable<>));
return (genericDefinition == typeof(IList<>)
|| genericDefinition == typeof(ICollection<>)
|| genericDefinition == typeof(IEnumerable<>)
#if SIMPLE_JSON_READONLY_COLLECTIONS
|| genericDefinition == typeof(IReadOnlyCollection<>)
|| genericDefinition == typeof(IReadOnlyList<>)
#endif
);
}
public static bool IsAssignableFrom(Type type1, Type type2)
@@ -1727,13 +1773,7 @@ namespace Octokit
public static IEnumerable<PropertyInfo> GetProperties(Type type)
{
#if SIMPLE_JSON_TYPEINFO
var info = type.GetTypeInfo();
var baseProperties = info.BaseType != null && info.BaseType != typeof(Object)
? GetProperties(info.BaseType)
: new PropertyInfo[0];
return info.DeclaredProperties.Concat(baseProperties);
return type.GetTypeInfo().DeclaredProperties;
#else
return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
#endif