internal static dynamic JsonMethod(OAuthConsumerConfig config, string url, string authToken, string tokenSecret, JavaScriptConverter jsConverter, List<KeyValuePair<string, string>> requestParams = null) { dynamic result = null; if (requestParams == null) requestParams = new List<KeyValuePair<string, string>>(); bool useAuthorized = false; if (!String.IsNullOrEmpty(authToken) && !String.IsNullOrEmpty(tokenSecret)) { useAuthorized = true; string timeStamp = _helpers.BuildTimestamp(); string nonce = _helpers.BuildNonce(); requestParams.Add(new KeyValuePair<string, string>("oauth_consumer_key", config.ConsumerKey)); requestParams.Add(new KeyValuePair<string, string>("oauth_nonce", nonce)); requestParams.Add(new KeyValuePair<string, string>("oauth_signature_method", SIGNATURE_METHOD)); requestParams.Add(new KeyValuePair<string, string>("oauth_timestamp", timeStamp)); requestParams.Add(new KeyValuePair<string, string>("oauth_token", authToken)); requestParams.Add(new KeyValuePair<string, string>("oauth_version", OAUTH_VERSION)); } string response = _requestImplementation.BuildAndExecuteRequest(url, config.ConsumerSecret + "&" + tokenSecret, requestParams, useAuthorized); if (!String.IsNullOrEmpty(response)) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { jsConverter }); try { result = jss.Deserialize(response, typeof(object)) as dynamic; } catch (Exception exc) { throw new InvalidJsonInputException(); } } return result; }
private static JavaScriptConverter[] GetConverters() { JavaScriptConverter[] contextConverters = JSONSerializerExecute.GetContextConverters(); JavaScriptConverter[] globalConverters = CreateGlobalCacheConverters(); JavaScriptConverter[] configConverters = GetGlobalConvertersInConfig().ToArray(); //CreateConfigConverters(); JavaScriptConverter[] converters = new JavaScriptConverter[contextConverters.Length + globalConverters.Length + configConverters.Length]; globalConverters.CopyTo(converters, 0); configConverters.CopyTo(converters, globalConverters.Length); contextConverters.CopyTo(converters, globalConverters.Length + configConverters.Length); return converters; }
public static JavaScriptConverter GetJavaScriptConverter(Type converterType, Type compareType, JavaScriptConverter[] converters) { JavaScriptConverter result = null; converters = converters ?? GetConverters(); foreach (JavaScriptConverter c in converters) { foreach (Type t in c.SupportedTypes) { if (t == compareType) { result = c; break; } } } return result; }
void SerializeValueImpl(object obj, StringBuilder output) { if (recursionDepth > recursionLimit) { throw new ArgumentException("Recursion limit has been exceeded while serializing object of type '{0}'", obj != null ? obj.GetType().ToString() : "[null]"); } if (obj == null || DBNull.Value.Equals(obj)) { StringBuilderExtensions.AppendCount(output, maxJsonLength, "null"); return; } Type valueType = obj.GetType(); JavaScriptConverter jsc = serializer.GetConverter(valueType); if (jsc != null) { IDictionary <string, object> result = jsc.Serialize(obj, serializer); if (result == null) { StringBuilderExtensions.AppendCount(output, maxJsonLength, "null"); return; } if (typeResolver != null) { string typeId = typeResolver.ResolveTypeId(valueType); if (!String.IsNullOrEmpty(typeId)) { result [JavaScriptSerializer.SerializedTypeNameKey] = typeId; } } SerializeValue(result, output); return; } TypeCode typeCode = Type.GetTypeCode(valueType); switch (typeCode) { case TypeCode.String: WriteValue(output, (string)obj); return; case TypeCode.Char: WriteValue(output, (char)obj); return; case TypeCode.Boolean: WriteValue(output, (bool)obj); return; case TypeCode.SByte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: if (valueType.IsEnum) { WriteEnumValue(output, obj, typeCode); return; } goto case TypeCode.Decimal; case TypeCode.Single: WriteValue(output, (float)obj); return; case TypeCode.Double: WriteValue(output, (double)obj); return; case TypeCode.Decimal: WriteValue(output, obj as IConvertible); return; case TypeCode.DateTime: WriteValue(output, (DateTime)obj); return; } if (typeof(Uri).IsAssignableFrom(valueType)) { WriteValue(output, (Uri)obj); return; } if (typeof(Guid).IsAssignableFrom(valueType)) { WriteValue(output, (Guid)obj); return; } IConvertible convertible = obj as IConvertible; if (convertible != null) { WriteValue(output, convertible); return; } try { if (objectCache.ContainsKey(obj)) { throw new InvalidOperationException("Circular reference detected."); } objectCache.Add(obj, true); Type closedIDict = GetClosedIDictionaryBase(valueType); if (closedIDict != null) { if (serializeGenericDictionaryMethods == null) { serializeGenericDictionaryMethods = new Dictionary <Type, MethodInfo> (); } MethodInfo mi; if (!serializeGenericDictionaryMethods.TryGetValue(closedIDict, out mi)) { Type[] types = closedIDict.GetGenericArguments(); mi = serializeGenericDictionary.MakeGenericMethod(types [0], types [1]); serializeGenericDictionaryMethods.Add(closedIDict, mi); } mi.Invoke(this, new object[] { output, obj }); return; } IDictionary dict = obj as IDictionary; if (dict != null) { SerializeDictionary(output, dict); return; } IEnumerable enumerable = obj as IEnumerable; if (enumerable != null) { SerializeEnumerable(output, enumerable); return; } SerializeArbitraryObject(output, obj, valueType); } finally { objectCache.Remove(obj); } }
object ConvertToType(object obj, Type targetType) { if (obj == null) { return(null); } if (obj is IDictionary <string, object> ) { if (targetType == null) { obj = EvaluateDictionary((IDictionary <string, object>)obj); } else { JavaScriptConverter converter = GetConverter(targetType); if (converter != null) { return(converter.Deserialize( EvaluateDictionary((IDictionary <string, object>)obj), targetType, this)); } } return(ConvertToObject((IDictionary <string, object>)obj, targetType)); } if (obj is ArrayList) { return(ConvertToList((ArrayList)obj, targetType)); } if (targetType == null) { return(obj); } Type sourceType = obj.GetType(); if (targetType.IsAssignableFrom(sourceType)) { return(obj); } if (targetType.IsEnum) { if (obj is string) { return(Enum.Parse(targetType, (string)obj, true)); } else { return(Enum.ToObject(targetType, obj)); } } TypeConverter c = TypeDescriptor.GetConverter(targetType); if (c.CanConvertFrom(sourceType)) { if (obj is string) { return(c.ConvertFromInvariantString((string)obj)); } return(c.ConvertFrom(obj)); } if ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition() == typeof(Nullable <>))) { if (obj is String) { /* * Take care of the special case whereas in JSON an empty string ("") really means * an empty value * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836) */ if (String.IsNullOrEmpty((String)obj)) { return(null); } } else if (c.CanConvertFrom(typeof(string))) { TypeConverter objConverter = TypeDescriptor.GetConverter(obj); string s = objConverter.ConvertToInvariantString(obj); return(c.ConvertFromInvariantString(s)); } } return(Convert.ChangeType(obj, targetType)); }
internal bool ConverterExistsForType(Type t, out JavaScriptConverter converter) { converter = this.GetConverter(t); return(converter != null); }
private static bool ConvertDictionaryToObject(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject) { object obj2; Type t = type; string id = null; object o = dictionary; if (dictionary.TryGetValue("__type", out obj2)) { if (!ConvertObjectToTypeMain(obj2, typeof(string), serializer, throwOnError, out obj2)) { convertedObject = false; return(false); } id = (string)obj2; if (id != null) { if (serializer.TypeResolver != null) { t = serializer.TypeResolver.ResolveType(id); if (t == null) { if (throwOnError) { throw new InvalidOperationException(); } convertedObject = null; return(false); } } dictionary.Remove("__type"); } } JavaScriptConverter converter = null; if ((t != null) && serializer.ConverterExistsForType(t, out converter)) { try { convertedObject = converter.Deserialize(dictionary, t, serializer); return(true); } catch { if (throwOnError) { throw; } convertedObject = null; return(false); } } if ((id != null) || IsClientInstantiatableType(t, serializer)) { o = Activator.CreateInstance(t); } List <string> list = new List <string>(dictionary.Keys); if (IsGenericDictionary(type)) { Type type3 = type.GetGenericArguments()[0]; if ((type3 != typeof(string)) && (type3 != typeof(object))) { if (throwOnError) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, new object[] { type.FullName })); } convertedObject = null; return(false); } Type type4 = type.GetGenericArguments()[1]; IDictionary dictionary2 = null; if (IsClientInstantiatableType(type, serializer)) { dictionary2 = (IDictionary)Activator.CreateInstance(type); } else { dictionary2 = (IDictionary)Activator.CreateInstance(_dictionaryGenericType.MakeGenericType(new Type[] { type3, type4 })); } if (dictionary2 != null) { foreach (string str2 in list) { object obj4; if (!ConvertObjectToTypeMain(dictionary[str2], type4, serializer, throwOnError, out obj4)) { convertedObject = null; return(false); } dictionary2[str2] = obj4; } convertedObject = dictionary2; return(true); } } if ((type != null) && !type.IsAssignableFrom(o.GetType())) { if (!throwOnError) { convertedObject = null; return(false); } if (type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null) == null) { throw new MissingMethodException(string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_NoConstructor, new object[] { type.FullName })); } throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DeserializerTypeMismatch, new object[] { type.FullName })); } foreach (string str3 in list) { object propertyValue = dictionary[str3]; if (!AssignToPropertyOrField(propertyValue, o, str3, serializer, throwOnError)) { convertedObject = null; return(false); } } convertedObject = o; return(true); }
// Method that converts an IDictionary<string, object> to an object of the right type private static bool ConvertDictionaryToObject(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject) { // The target type to instantiate. Type targetType = type; object s; string serverTypeName = null; object o = dictionary; // Check if __serverType exists in the dictionary, use it as the type. if (dictionary.TryGetValue(JavaScriptSerializer.ServerTypeFieldName, out s)) { // Convert the __serverType value to a string. if (!ConvertObjectToTypeMain(s, typeof(String), serializer, throwOnError, out s)) { convertedObject = false; return(false); } serverTypeName = (string)s; if (serverTypeName != null) { // If we don't have the JavaScriptTypeResolver, we can't use it if (serializer.TypeResolver != null) { // Get the actual type from the resolver. targetType = serializer.TypeResolver.ResolveType(serverTypeName); // In theory, we should always find the type. If not, it may be some kind of attack. if (targetType == null) { if (throwOnError) { throw new InvalidOperationException(); } convertedObject = null; return(false); } } // Remove the serverType from the dictionary, even if the resolver was null dictionary.Remove(JavaScriptSerializer.ServerTypeFieldName); } } JavaScriptConverter converter = null; if (targetType != null && serializer.ConverterExistsForType(targetType, out converter)) { try { convertedObject = converter.Deserialize(dictionary, targetType, serializer); return(true); } catch { if (throwOnError) { throw; } convertedObject = null; return(false); } } // Instantiate the type if it's coming from the __serverType argument. if (serverTypeName != null || IsClientInstantiatableType(targetType, serializer)) { // First instantiate the object based on the type. o = Activator.CreateInstance(targetType); } #if INDIGO StructuralContract contract = null; if (suggestedType != null && suggestedType.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0) { contract = StructuralContract.Create(suggestedType); } #endif // Use a different collection to avoid modifying the original during keys enumeration. List <String> memberNames = new List <String>(dictionary.Keys); // Try to handle the IDictionary<K, V> case if (IsGenericDictionary(type)) { Type keyType = type.GetGenericArguments()[0]; if (keyType != typeof(string) && keyType != typeof(object)) { if (throwOnError) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, type.FullName)); } convertedObject = null; return(false); } Type valueType = type.GetGenericArguments()[1]; IDictionary dict = null; if (IsClientInstantiatableType(type, serializer)) { dict = (IDictionary)Activator.CreateInstance(type); } else { // Get the strongly typed Dictionary<K, V> Type t = _dictionaryGenericType.MakeGenericType(keyType, valueType); dict = (IDictionary)Activator.CreateInstance(t); } if (dict != null) { foreach (string memberName in memberNames) { object memberObject; if (!ConvertObjectToTypeMain(dictionary[memberName], valueType, serializer, throwOnError, out memberObject)) { convertedObject = null; return(false); } dict[memberName] = memberObject; } convertedObject = dict; return(true); } } // Fail if we know we cannot possibly return the required type. if (type != null && !type.IsAssignableFrom(o.GetType())) { if (!throwOnError) { convertedObject = null; return(false); } ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null); if (constructorInfo == null) { throw new MissingMethodException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_NoConstructor, type.FullName)); } throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DeserializerTypeMismatch, type.FullName)); } foreach (string memberName in memberNames) { object propertyValue = dictionary[memberName]; #if INDIGO if (contract != null) { Member member = contract.FindMember(memberName); // if (member == null) { throw new InvalidOperationException(); } if (member.MemberType == MemberTypes.Field) { member.SetValue(o, propertyValue); } else { member.SetValue(o, propertyValue); } continue; } #endif // Assign the value into a property or field of the object if (!AssignToPropertyOrField(propertyValue, o, memberName, serializer, throwOnError)) { convertedObject = null; return(false); } } convertedObject = o; return(true); }