mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-21 22:25:03 -04:00
imported signalr 1.1.3 into NzbDrone.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to serialize and deserialize outgoing/incoming data.
|
||||
/// </summary>
|
||||
public interface IJsonSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes the specified object to a <see cref="System.IO.TextWriter"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The object to serialize</param>
|
||||
/// <param name="writer">The <see cref="System.IO.TextWriter"/> to serialize the object to.</param>
|
||||
void Serialize(object value, TextWriter writer);
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the JSON to a .NET object.
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="System.IO.TextReader"/> to deserialize the object from.</param>
|
||||
/// <param name="targetType">The <see cref="System.Type"/> of object being deserialized.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
object Parse(TextReader reader, Type targetType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a JSON value.
|
||||
/// </summary>
|
||||
public interface IJsonValue
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts the parameter value to the specified <see cref="Type"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The <see cref="Type"/> to convert the parameter to.</param>
|
||||
/// <returns>The converted parameter value.</returns>
|
||||
object ConvertTo(Type type);
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the parameter can be converted to the specified <see cref="Type"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The <see cref="Type"/> to check.</param>
|
||||
/// <returns>True if the parameter can be converted to the specified <see cref="Type"/>, false otherwise.</returns>
|
||||
bool CanConvertTo(Type type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementations handle their own serialization to JSON.
|
||||
/// </summary>
|
||||
public interface IJsonWritable
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes itself to JSON via a <see cref="System.IO.TextWriter"/>.
|
||||
/// </summary>
|
||||
/// <param name="writer">The <see cref="System.IO.TextWriter"/> that receives the JSON serialized object.</param>
|
||||
void WriteJson(TextWriter writer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// An implementation of IJsonValue over JSON.NET
|
||||
/// </summary>
|
||||
internal class JRawValue : IJsonValue
|
||||
{
|
||||
private readonly string _value;
|
||||
|
||||
public JRawValue(JRaw value)
|
||||
{
|
||||
_value = value.ToString();
|
||||
}
|
||||
|
||||
public object ConvertTo(Type type)
|
||||
{
|
||||
// A non generic implementation of ToObject<T> on JToken
|
||||
using (var jsonReader = new StringReader(_value))
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
MaxDepth = 20
|
||||
};
|
||||
var serializer = JsonSerializer.Create(settings);
|
||||
return serializer.Deserialize(jsonReader, type);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanConvertTo(Type type)
|
||||
{
|
||||
// TODO: Implement when we implement better method overload resolution
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Default <see cref="IJsonSerializer"/> implementation over Json.NET.
|
||||
/// </summary>
|
||||
public class JsonNetSerializer : IJsonSerializer
|
||||
{
|
||||
private readonly JsonSerializer _serializer;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonNetSerializer"/> class.
|
||||
/// </summary>
|
||||
public JsonNetSerializer()
|
||||
: this(new JsonSerializerSettings())
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonNetSerializer"/> class.
|
||||
/// </summary>
|
||||
/// <param name="settings">The <see cref="T:Newtonsoft.Json.JsonSerializerSettings"/> to use when serializing and deserializing.</param>
|
||||
public JsonNetSerializer(JsonSerializerSettings settings)
|
||||
{
|
||||
if (settings == null)
|
||||
{
|
||||
throw new ArgumentNullException("settings");
|
||||
}
|
||||
|
||||
// Just override it anyways (we're saving the user)
|
||||
settings.MaxDepth = 20;
|
||||
_serializer = JsonSerializer.Create(settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the JSON to a .NET object.
|
||||
/// </summary>
|
||||
/// <param name="reader">The JSON to deserialize.</param>
|
||||
/// <param name="targetType">The <see cref="System.Type"/> of object being deserialized.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
public object Parse(TextReader reader, Type targetType)
|
||||
{
|
||||
return _serializer.Deserialize(reader, targetType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the specified object to a <see cref="TextWriter"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The object to serialize</param>
|
||||
/// <param name="writer">The <see cref="TextWriter"/> to serialize the object to.</param>
|
||||
public void Serialize(object value, TextWriter writer)
|
||||
{
|
||||
var selfSerializer = value as IJsonWritable;
|
||||
if (selfSerializer != null)
|
||||
{
|
||||
selfSerializer.WriteJson(writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_serializer.Serialize(writer, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Microsoft.AspNet.SignalR.Infrastructure;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="IJsonSerializer"/>.
|
||||
/// </summary>
|
||||
public static class JsonSerializerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Deserializes the JSON to a .NET object.
|
||||
/// </summary>
|
||||
/// <param name="serializer">The serializer</param>
|
||||
/// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam>
|
||||
/// <param name="json">The JSON to deserialize</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
public static T Parse<T>(this IJsonSerializer serializer, string json)
|
||||
{
|
||||
if (serializer == null)
|
||||
{
|
||||
throw new ArgumentNullException("serializer");
|
||||
}
|
||||
|
||||
using (var reader = new StringReader(json))
|
||||
{
|
||||
return (T)serializer.Parse(reader, typeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the JSON to a .NET object.
|
||||
/// </summary>
|
||||
/// <param name="serializer">The serializer</param>
|
||||
/// <typeparam name="T">The <see cref="System.Type"/> of object being deserialized.</typeparam>
|
||||
/// <param name="jsonBuffer">The JSON buffer to deserialize</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <returns>The deserialized object from the JSON string.</returns>
|
||||
public static T Parse<T>(this IJsonSerializer serializer, ArraySegment<byte> jsonBuffer, Encoding encoding)
|
||||
{
|
||||
if (serializer == null)
|
||||
{
|
||||
throw new ArgumentNullException("serializer");
|
||||
}
|
||||
|
||||
using (var reader = new ArraySegmentTextReader(jsonBuffer, encoding))
|
||||
{
|
||||
return (T)serializer.Parse(reader, typeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the specified object to a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="serializer">The serializer</param>
|
||||
/// <param name="value">The object to serailize.</param>
|
||||
/// <returns>A JSON string representation of the object.</returns>
|
||||
public static string Stringify(this IJsonSerializer serializer, object value)
|
||||
{
|
||||
if (serializer == null)
|
||||
{
|
||||
throw new ArgumentNullException("serializer");
|
||||
}
|
||||
|
||||
using (var writer = new StringWriter(CultureInfo.InvariantCulture))
|
||||
{
|
||||
serializer.Serialize(value, writer);
|
||||
return writer.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper class for common JSON operations.
|
||||
/// </summary>
|
||||
public static class JsonUtility
|
||||
{
|
||||
// JavaScript keywords taken from http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
|
||||
// Sections: 7.6.1.1, 7.6.1.2
|
||||
// Plus the implicity globals "NaN", "undefined", "Infinity"
|
||||
private static readonly string[] _jsKeywords = new[] { "break", "do", "instanceof", "typeof", "case", "else", "new", "var", "catch", "finally", "return", "void", "continue", "for", "switch", "while", "debugger", "function", "this", "with", "default", "if", "throw", "delete", "in", "try", "class", "enum", "extends", "super", "const", "export", "import", "implements", "let", "private", "public", "yield", "interface", "package", "protected", "static", "NaN", "undefined", "Infinity" };
|
||||
|
||||
/// <summary>
|
||||
/// Converts the specified name to camel case.
|
||||
/// </summary>
|
||||
/// <param name="name">The name to convert.</param>
|
||||
/// <returns>A camel cased version of the specified name.</returns>
|
||||
public static string CamelCase(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ArgumentNullException("name");
|
||||
}
|
||||
|
||||
return String.Join(".", name.Split('.').Select(n => Char.ToLower(n[0], CultureInfo.InvariantCulture) + n.Substring(1)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string that returns JSON mime type "application/json; charset=UTF-8".
|
||||
/// </summary>
|
||||
public static string JsonMimeType
|
||||
{
|
||||
get { return "application/json; charset=UTF-8"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string that returns JSONP mime type "application/javascript; charset=UTF-8".
|
||||
/// </summary>
|
||||
public static string JavaScriptMimeType
|
||||
{
|
||||
get { return "application/javascript; charset=UTF-8"; }
|
||||
}
|
||||
|
||||
public static string CreateJsonpCallback(string callback, string payload)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
if (!IsValidJavaScriptCallback(callback))
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
sb.AppendFormat("{0}(", callback).Append(payload).Append(");");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
internal static bool IsValidJavaScriptCallback(string callback)
|
||||
{
|
||||
if (String.IsNullOrWhiteSpace(callback))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var identifiers = callback.Split('.');
|
||||
|
||||
// Check each identifier to ensure it's a valid JS identifier
|
||||
foreach (var identifier in identifiers)
|
||||
{
|
||||
if (!IsValidJavaScriptFunctionName(identifier))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static bool IsValidJavaScriptFunctionName(string name)
|
||||
{
|
||||
if (String.IsNullOrWhiteSpace(name) || IsJavaScriptReservedWord(name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// JavaScript identifier must start with a letter or a '$' or an '_' char
|
||||
var firstChar = name[0];
|
||||
if (!IsValidJavaScriptIdentifierStartChar(firstChar))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 1; i < name.Length; i++)
|
||||
{
|
||||
// Characters can be a letter, digit, '$' or '_'
|
||||
if (!IsValidJavaScriptIdenfitierNonStartChar(name[i]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsValidJavaScriptIdentifierStartChar(char startChar)
|
||||
{
|
||||
return Char.IsLetter(startChar) || startChar == '$' || startChar == '_';
|
||||
}
|
||||
|
||||
private static bool IsValidJavaScriptIdenfitierNonStartChar(char identifierChar)
|
||||
{
|
||||
return Char.IsLetterOrDigit(identifierChar) || identifierChar == '$' || identifierChar == '_';
|
||||
}
|
||||
|
||||
private static bool IsJavaScriptReservedWord(string word)
|
||||
{
|
||||
return _jsKeywords.Contains(word);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.SignalR.Infrastructure;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// A converter for dictionaries that uses a SipHash comparer
|
||||
/// </summary>
|
||||
internal class SipHashBasedDictionaryConverter : JsonConverter
|
||||
{
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(IDictionary<string, object>);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
return ReadJsonObject(reader);
|
||||
}
|
||||
|
||||
private object ReadJsonObject(JsonReader reader)
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.StartObject:
|
||||
return ReadObject(reader);
|
||||
case JsonToken.StartArray:
|
||||
return ReadArray(reader);
|
||||
case JsonToken.Integer:
|
||||
case JsonToken.Float:
|
||||
case JsonToken.String:
|
||||
case JsonToken.Boolean:
|
||||
case JsonToken.Undefined:
|
||||
case JsonToken.Null:
|
||||
case JsonToken.Date:
|
||||
case JsonToken.Bytes:
|
||||
return reader.Value;
|
||||
default:
|
||||
throw new NotSupportedException();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private object ReadArray(JsonReader reader)
|
||||
{
|
||||
var array = new List<object>();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
default:
|
||||
object value = ReadJsonObject(reader);
|
||||
|
||||
array.Add(value);
|
||||
break;
|
||||
case JsonToken.EndArray:
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonSerializationException(Resources.Error_ParseObjectFailed);
|
||||
}
|
||||
|
||||
private object ReadObject(JsonReader reader)
|
||||
{
|
||||
var obj = new Dictionary<string, object>(new SipHashBasedStringEqualityComparer());
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.PropertyName:
|
||||
string propertyName = reader.Value.ToString();
|
||||
|
||||
if (!reader.Read())
|
||||
{
|
||||
throw new JsonSerializationException(Resources.Error_ParseObjectFailed);
|
||||
}
|
||||
|
||||
object value = ReadJsonObject(reader);
|
||||
|
||||
obj[propertyName] = value;
|
||||
break;
|
||||
case JsonToken.EndObject:
|
||||
return obj;
|
||||
default:
|
||||
throw new JsonSerializationException(Resources.Error_ParseObjectFailed);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonSerializationException(Resources.Error_ParseObjectFailed);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user