2 // JavaScriptSerializer.cs
5 // Konstantin Triger <kostat@mainsoft.com>
6 // Marek Safar <marek.safar@gmail.com>
8 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
9 // Copyright 2012 Xamarin Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
34 using Newtonsoft.Json;
36 using System.Collections;
37 using System.Reflection;
38 using Newtonsoft.Json.Utilities;
39 using System.ComponentModel;
40 using System.Configuration;
41 using System.Web.Configuration;
43 namespace System.Web.Script.Serialization
45 public class JavaScriptSerializer
47 internal const string SerializedTypeNameKey = "__type";
49 List<IEnumerable<JavaScriptConverter>> _converterList;
52 JavaScriptTypeResolver _typeResolver;
53 internal static readonly JavaScriptSerializer DefaultSerializer = new JavaScriptSerializer (null, false);
55 public JavaScriptSerializer () : this (null, false)
59 public JavaScriptSerializer (JavaScriptTypeResolver resolver) : this (resolver, false)
63 internal JavaScriptSerializer (JavaScriptTypeResolver resolver, bool registerConverters)
65 _typeResolver = resolver;
67 ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection) ConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/jsonSerialization");
68 if (section == null) {
70 _maxJsonLength = 2097152;
72 _maxJsonLength = 102400;
74 _recursionLimit = 100;
76 _maxJsonLength = section.MaxJsonLength;
77 _recursionLimit = section.RecursionLimit;
79 if (registerConverters) {
80 ConvertersCollection converters = section.Converters;
81 if (converters != null && converters.Count > 0) {
82 var cvtlist = new List <JavaScriptConverter> ();
85 JavaScriptConverter jsc;
87 foreach (Converter cvt in converters) {
88 typeName = cvt != null ? cvt.Type : null;
92 type = HttpApplication.LoadType (typeName, true);
93 if (type == null || !typeof (JavaScriptConverter).IsAssignableFrom (type))
96 jsc = Activator.CreateInstance (type) as JavaScriptConverter;
100 RegisterConverters (cvtlist);
107 public int MaxJsonLength {
109 return _maxJsonLength;
112 _maxJsonLength = value;
116 public int RecursionLimit {
118 return _recursionLimit;
121 _recursionLimit = value;
125 internal JavaScriptTypeResolver TypeResolver {
126 get { return _typeResolver; }
129 public T ConvertToType<T> (object obj) {
133 return (T) ConvertToType (obj, typeof (T));
141 object ConvertToType (object obj, Type targetType)
146 if (obj is IDictionary<string, object>) {
147 if (targetType == null)
148 obj = EvaluateDictionary ((IDictionary<string, object>) obj);
150 JavaScriptConverter converter = GetConverter (targetType);
151 if (converter != null)
152 return converter.Deserialize (
153 EvaluateDictionary ((IDictionary<string, object>) obj),
157 return ConvertToObject ((IDictionary<string, object>) obj, targetType);
159 if (obj is ArrayList)
160 return ConvertToList ((ArrayList) obj, targetType);
162 if (targetType == null)
165 Type sourceType = obj.GetType ();
166 if (targetType.IsAssignableFrom (sourceType))
169 if (targetType.IsEnum)
171 return Enum.Parse (targetType, (string) obj, true);
173 return Enum.ToObject (targetType, obj);
175 TypeConverter c = TypeDescriptor.GetConverter (targetType);
176 if (c.CanConvertFrom (sourceType)) {
178 return c.ConvertFromInvariantString ((string) obj);
180 return c.ConvertFrom (obj);
183 if ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition () == typeof (Nullable<>))) {
186 * Take care of the special case whereas in JSON an empty string ("") really means
188 * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
190 if(String.IsNullOrEmpty ((String)obj))
192 } else if (c.CanConvertFrom (typeof (string))) {
193 TypeConverter objConverter = TypeDescriptor.GetConverter (obj);
194 string s = objConverter.ConvertToInvariantString (obj);
195 return c.ConvertFromInvariantString (s);
199 return Convert.ChangeType (obj, targetType);
202 public T Deserialize<T> (string input) {
203 return ConvertToType<T> (DeserializeObjectInternal(input));
206 public object Deserialize (string input, Type targetType) {
207 object obj = DeserializeObjectInternal (input);
210 return Activator.CreateInstance (targetType);
212 return ConvertToType (obj, targetType);
215 static object Evaluate (object value) {
216 return Evaluate (value, false);
219 static object Evaluate (object value, bool convertListToArray) {
220 if (value is IDictionary<string, object>)
221 value = EvaluateDictionary ((IDictionary<string, object>) value, convertListToArray);
222 else if (value is ArrayList)
223 value = EvaluateList ((ArrayList) value, convertListToArray);
227 static object EvaluateList (ArrayList e) {
228 return EvaluateList (e, false);
231 static object EvaluateList (ArrayList e, bool convertListToArray) {
232 ArrayList list = new ArrayList ();
233 foreach (object value in e)
234 list.Add (Evaluate (value, convertListToArray));
236 return convertListToArray ? (object) list.ToArray () : list;
239 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict) {
240 return EvaluateDictionary (dict, false);
243 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict, bool convertListToArray) {
244 Dictionary<string, object> d = new Dictionary<string, object> (StringComparer.Ordinal);
245 foreach (KeyValuePair<string, object> entry in dict) {
246 d.Add (entry.Key, Evaluate (entry.Value, convertListToArray));
252 static readonly Type typeofObject = typeof(object);
253 static readonly Type typeofGenList = typeof (List<>);
255 object ConvertToList (ArrayList col, Type type) {
256 Type elementType = null;
257 if (type != null && type.HasElementType)
258 elementType = type.GetElementType ();
261 if (type == null || type.IsArray || typeofObject == type || typeof (ArrayList).IsAssignableFrom (type))
262 list = new ArrayList ();
263 else if (ReflectionUtils.IsInstantiatableType (type))
264 // non-generic typed list
265 list = (IList) Activator.CreateInstance (type, true);
266 else if (ReflectionUtils.IsAssignable (type, typeofGenList)) {
267 if (type.IsGenericType) {
268 Type [] genArgs = type.GetGenericArguments ();
269 elementType = genArgs [0];
271 list = (IList) Activator.CreateInstance (typeofGenList.MakeGenericType (genArgs));
273 list = new ArrayList ();
275 throw new InvalidOperationException (String.Format ("Deserializing list type '{0}' not supported.", type.GetType ().Name));
277 if (list.IsReadOnly) {
282 if (elementType == null)
283 elementType = typeof (object);
285 foreach (object value in col)
286 list.Add (ConvertToType (value, elementType));
288 if (type != null && type.IsArray)
289 list = ((ArrayList) list).ToArray (elementType);
294 object ConvertToObject (IDictionary<string, object> dict, Type type)
296 if (_typeResolver != null) {
297 if (dict.Keys.Contains(SerializedTypeNameKey)) {
299 type = _typeResolver.ResolveType ((string) dict [SerializedTypeNameKey]);
303 if (type.IsGenericType) {
304 if (type.GetGenericTypeDefinition ().IsAssignableFrom (typeof (IDictionary <,>))) {
305 Type[] arguments = type.GetGenericArguments ();
306 if (arguments == null || arguments.Length != 2 || (arguments [0] != typeof (object) && arguments [0] != typeof (string)))
307 throw new InvalidOperationException (
308 "Type '" + type + "' is not not supported for serialization/deserialization of a dictionary, keys must be strings or objects.");
309 if (type.IsAbstract) {
310 Type dictType = typeof (Dictionary <,>);
311 type = dictType.MakeGenericType (arguments [0], arguments [1]);
314 } else if (type.IsAssignableFrom (typeof (IDictionary)))
315 type = typeof (Dictionary <string, object>);
317 object target = Activator.CreateInstance (type, true);
319 foreach (KeyValuePair<string, object> entry in dict) {
320 object value = entry.Value;
321 if (target is IDictionary) {
322 Type valueType = ReflectionUtils.GetTypedDictionaryValueType (type);
323 if (value != null && valueType == typeof (System.Object))
324 valueType = value.GetType ();
326 ((IDictionary) target).Add (entry.Key, ConvertToType (value, valueType));
329 MemberInfo [] memberCollection = type.GetMember (entry.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
330 if (memberCollection == null || memberCollection.Length == 0) {
331 //must evaluate value
336 MemberInfo member = memberCollection [0];
338 if (!ReflectionUtils.CanSetMemberValue (member)) {
339 //must evaluate value
344 Type memberType = ReflectionUtils.GetMemberUnderlyingType (member);
346 if (memberType.IsInterface) {
347 if (memberType.IsGenericType)
348 memberType = ResolveGenericInterfaceToType (memberType);
350 memberType = ResolveInterfaceToType (memberType);
352 if (memberType == null)
353 throw new InvalidOperationException ("Unable to deserialize a member, as its type is an unknown interface.");
356 ReflectionUtils.SetMemberValue (member, target, ConvertToType(value, memberType));
362 Type ResolveGenericInterfaceToType (Type type)
364 Type[] genericArgs = type.GetGenericArguments ();
366 if (ReflectionUtils.IsSubClass (type, typeof (IDictionary <,>)))
367 return typeof (Dictionary <,>).MakeGenericType (genericArgs);
369 if (ReflectionUtils.IsSubClass (type, typeof (IList <>)) ||
370 ReflectionUtils.IsSubClass (type, typeof (ICollection <>)) ||
371 ReflectionUtils.IsSubClass (type, typeof (IEnumerable <>))
373 return typeof (List <>).MakeGenericType (genericArgs);
375 if (ReflectionUtils.IsSubClass (type, typeof (IComparer <>)))
376 return typeof (Comparer <>).MakeGenericType (genericArgs);
378 if (ReflectionUtils.IsSubClass (type, typeof (IEqualityComparer <>)))
379 return typeof (EqualityComparer <>).MakeGenericType (genericArgs);
384 Type ResolveInterfaceToType (Type type)
386 if (typeof (IDictionary).IsAssignableFrom (type))
387 return typeof (Hashtable);
389 if (typeof (IList).IsAssignableFrom (type) ||
390 typeof (ICollection).IsAssignableFrom (type) ||
391 typeof (IEnumerable).IsAssignableFrom (type))
392 return typeof (ArrayList);
394 if (typeof (IComparer).IsAssignableFrom (type))
395 return typeof (Comparer);
400 public object DeserializeObject (string input) {
401 object obj = Evaluate (DeserializeObjectInternal (input), true);
402 IDictionary dictObj = obj as IDictionary;
403 if (dictObj != null && dictObj.Contains(SerializedTypeNameKey)){
404 if (_typeResolver == null) {
405 throw new ArgumentNullException ("resolver", "Must have a type resolver to deserialize an object that has an '__type' member");
408 obj = ConvertToType(obj, null);
413 internal object DeserializeObjectInternal (string input) {
414 return Json.Deserialize (input, this);
417 internal object DeserializeObjectInternal (TextReader input) {
418 return Json.Deserialize (input, this);
421 public void RegisterConverters (IEnumerable<JavaScriptConverter> converters) {
422 if (converters == null)
423 throw new ArgumentNullException ("converters");
425 if (_converterList == null)
426 _converterList = new List<IEnumerable<JavaScriptConverter>> ();
427 _converterList.Add (converters);
430 internal JavaScriptConverter GetConverter (Type type) {
431 if (_converterList != null)
432 for (int i = 0; i < _converterList.Count; i++) {
433 foreach (JavaScriptConverter converter in _converterList [i])
434 foreach (Type supportedType in converter.SupportedTypes)
435 if (supportedType.IsAssignableFrom (type))
442 public string Serialize (object obj) {
443 StringBuilder b = new StringBuilder ();
445 return b.ToString ();
448 public void Serialize (object obj, StringBuilder output) {
449 Json.Serialize (obj, this, output);
452 internal void Serialize (object obj, TextWriter output) {
453 Json.Serialize (obj, this, output);