File: JavaScriptSerializer.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (539 lines) | stat: -rw-r--r-- 23,525 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//------------------------------------------------------------------------------
// <copyright file="JavaScriptSerializer.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
 
namespace System.Web.Script.Serialization {
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Reflection;
    using System.Text;
    using System.Web;
    using System.Web.Resources;

    public class JavaScriptSerializer {
        internal const string ServerTypeFieldName = "__type";
        internal const int DefaultRecursionLimit = 100;
        internal const int DefaultMaxJsonLength = 2097152;

        internal static string SerializeInternal(object o) {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(o);
        }

        internal static object Deserialize(JavaScriptSerializer serializer, string input, Type type, int depthLimit) {
            if (input == null) {
                throw new ArgumentNullException("input");
            }
            if (input.Length > serializer.MaxJsonLength) {
                throw new ArgumentException(AtlasWeb.JSON_MaxJsonLengthExceeded, "input");
            }

            object o = JavaScriptObjectDeserializer.BasicDeserialize(input, depthLimit, serializer);
            return ObjectConverter.ConvertObjectToType(o, type, serializer);
        }

        // INSTANCE fields/methods
        private JavaScriptTypeResolver _typeResolver;
        private int _recursionLimit;
        private int _maxJsonLength;

        public JavaScriptSerializer() : this(null) { }

        public JavaScriptSerializer(JavaScriptTypeResolver resolver) {
            _typeResolver = resolver;
            RecursionLimit = DefaultRecursionLimit;
            MaxJsonLength = DefaultMaxJsonLength;
        }

        public int MaxJsonLength {
            get {
                return _maxJsonLength;
            }
            set {
                if (value < 1) {
                    throw new ArgumentOutOfRangeException(AtlasWeb.JSON_InvalidMaxJsonLength);
                }
                _maxJsonLength = value;
            }
        }

        public int RecursionLimit {
            get {
                return _recursionLimit;
            }
            set {
                if (value < 1) {
                    throw new ArgumentOutOfRangeException(AtlasWeb.JSON_InvalidRecursionLimit);
                }
                _recursionLimit = value;
            }
        }

        internal JavaScriptTypeResolver TypeResolver {
            get {
                return _typeResolver;
            }
        }

        private Dictionary<Type, JavaScriptConverter> _converters;
        private Dictionary<Type, JavaScriptConverter> Converters {
            get {
                if (_converters == null) {
                    _converters = new Dictionary<Type, JavaScriptConverter>();
                }
                return _converters;
            }
        }

        [SuppressMessage("Microsoft.Usage", "CA2301:EmbeddableTypesInContainersRule", MessageId = "Converters", Justification = "This is for managed types which need to have custom type converters for JSon serialization, I don't think there will be any com interop types for this scenario.")]
        public void RegisterConverters(IEnumerable<JavaScriptConverter> converters)
        {
            if (converters == null) {
                throw new ArgumentNullException("converters");
            }

            foreach (JavaScriptConverter converter in converters) {
                IEnumerable<Type> supportedTypes = converter.SupportedTypes;
                if (supportedTypes != null) {
                    foreach (Type supportedType in supportedTypes) {
                        Converters[supportedType] = converter;
                    }
                }
            }
        }

        [SuppressMessage("Microsoft.Usage", "CA2301:EmbeddableTypesInContainersRule", MessageId = "_converters", Justification = "This is for managed types which need to have custom type converters for JSon serialization, I don't think there will be any com interop types for this scenario.")]
        private JavaScriptConverter GetConverter(Type t)
        {
            if (_converters != null) {
                while (t != null) {
                    if (_converters.ContainsKey(t)) {
                        return _converters[t];
                    }
                    t = t.BaseType;
                }
            }
            return null;
        }

        internal bool ConverterExistsForType(Type t, out JavaScriptConverter converter) {
            converter = GetConverter(t);
            return converter != null;
        }

        public object DeserializeObject(string input) {
            return Deserialize(this, input, null /*type*/, RecursionLimit);
        }

        [
        SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter",
            Justification = "Generic parameter is preferable to forcing caller to downcast. " +
                "Has has been approved by API review board. " +
                "Dev10 701126: Overload added afterall, to allow runtime determination of the type.")
        ]
        public T Deserialize<T>(string input) {
            return (T)Deserialize(this, input, typeof(T), RecursionLimit);
        }

        public object Deserialize(string input, Type targetType) {
            return Deserialize(this, input, targetType, RecursionLimit);
        }

        [
        SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter",
            Justification = "Generic parameter is preferable to forcing caller to downcast. " +
                "Has has been approved by API review board. " + 
                "Dev10 701126: Overload added afterall, to allow runtime determination of the type."),
        SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj",
            Justification = "Cannot change parameter name as would break binary compatibility with legacy apps.")
        ]
        public T ConvertToType<T>(object obj) {
            return (T)ObjectConverter.ConvertObjectToType(obj, typeof(T), this);
        }

        [
        SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj",
            Justification = "Consistent with previously existing overload which cannot be changed.")
        ]
        public object ConvertToType(object obj, Type targetType) {
            return ObjectConverter.ConvertObjectToType(obj, targetType, this);
        }

        [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj",
            Justification = "Cannot change parameter name as would break binary compatibility with legacy apps.")]
        public string Serialize(object obj) {
            return Serialize(obj, SerializationFormat.JSON);
        }

        internal string Serialize(object obj, SerializationFormat serializationFormat) {
            StringBuilder sb = new StringBuilder();
            Serialize(obj, sb, serializationFormat);
            return sb.ToString();
        }

        [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj",
            Justification = "Cannot change parameter name as would break binary compatibility with legacy apps.")]
        public void Serialize(object obj, StringBuilder output) {
            Serialize(obj, output, SerializationFormat.JSON);
        }

        internal void Serialize(object obj, StringBuilder output, SerializationFormat serializationFormat) {
            SerializeValue(obj, output, 0, null, serializationFormat);
            // DevDiv Bugs 96574: Max JSON length does not apply when serializing to Javascript for ScriptDescriptors
            if (serializationFormat == SerializationFormat.JSON && output.Length > MaxJsonLength) {
                throw new InvalidOperationException(AtlasWeb.JSON_MaxJsonLengthExceeded);
            }
        }

        private static void SerializeBoolean(bool o, StringBuilder sb) {
            if (o) {
                sb.Append("true");
            }
            else {
                sb.Append("false");
            }
        }

        private static void SerializeUri(Uri uri, StringBuilder sb) {
            sb.Append("\"").Append(uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped)).Append("\"");
        }

        private static void SerializeGuid(Guid guid, StringBuilder sb) {
            sb.Append("\"").Append(guid.ToString()).Append("\"");
        }

        internal static readonly long DatetimeMinTimeTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;
        private static void SerializeDateTime(DateTime datetime, StringBuilder sb, SerializationFormat serializationFormat) {
            Debug.Assert(serializationFormat == SerializationFormat.JSON || serializationFormat == SerializationFormat.JavaScript);
            if (serializationFormat == SerializationFormat.JSON) {
                // DevDiv 41127: Never confuse atlas serialized strings with dates
                // Serialized date: "\/Date(123)\/"
                sb.Append(@"""\/Date(");
                sb.Append((datetime.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000);
                sb.Append(@")\/""");
            }
            else {
                // DevDiv 96574: Need to be able to serialize to javascript dates for script descriptors 
                // new Date(ticks)
                sb.Append("new Date(");
                sb.Append((datetime.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000);
                sb.Append(@")");
            }
        }

        // Serialize custom object graph
        private void SerializeCustomObject(object o, StringBuilder sb, int depth, Hashtable objectsInUse, SerializationFormat serializationFormat) {
            bool first = true;
            Type type = o.GetType();
            sb.Append('{');

            // Serialize the object type if we have a type resolver
            if (TypeResolver != null) {

                // Only do this if the context is actually aware of this type
                string typeString = TypeResolver.ResolveTypeId(type);
                if (typeString != null) {
                    SerializeString(ServerTypeFieldName, sb);
                    sb.Append(':');
                    SerializeValue(typeString, sb, depth, objectsInUse, serializationFormat);
                    first = false;
                }
            }

            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo fieldInfo in fields) {

                // Ignore all fields marked as [ScriptIgnore]
                if (CheckScriptIgnoreAttribute(fieldInfo)) {
                    continue;
                }

                if (!first) sb.Append(',');
                SerializeString(fieldInfo.Name, sb);
                sb.Append(':');
                SerializeValue(SecurityUtils.FieldInfoGetValue(fieldInfo, o), sb, depth, objectsInUse, serializationFormat, currentMember: fieldInfo);
                first = false;
            }

            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
            foreach (PropertyInfo propInfo in props) {

                // Ignore all properties marked as [ScriptIgnore]
                if (CheckScriptIgnoreAttribute(propInfo))
                    continue;

                MethodInfo getMethodInfo = propInfo.GetGetMethod();

                // Skip property if it has no get
                if (getMethodInfo == null) {
                    continue;
                }

                // Ignore indexed properties
                if (getMethodInfo.GetParameters().Length > 0) continue;

                if (!first) sb.Append(',');
                SerializeString(propInfo.Name, sb);
                sb.Append(':');
                SerializeValue(SecurityUtils.MethodInfoInvoke(getMethodInfo, o, null), sb, depth, objectsInUse, serializationFormat, currentMember: propInfo);
                first = false;
            }

            sb.Append('}');
        }

        private bool CheckScriptIgnoreAttribute(MemberInfo memberInfo) {
            // Ignore all members marked as [ScriptIgnore]
            if (memberInfo.IsDefined(typeof(ScriptIgnoreAttribute), true /*inherits*/)) return true;

            //The above API does not consider the inherit boolean and the right API for that is Attribute.IsDefined.
            //However, to keep the backward compatibility with 4.0, we decided to define an opt-in mechanism (ApplyToOverrides property on ScriptIgnoreAttribute)
            //to consider the inherit boolean.
            ScriptIgnoreAttribute scriptIgnoreAttr = (ScriptIgnoreAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ScriptIgnoreAttribute), inherit: true);
            if (scriptIgnoreAttr != null && scriptIgnoreAttr.ApplyToOverrides) {
                return true;
            }

            return false;
        }

        private void SerializeDictionary(IDictionary o, StringBuilder sb, int depth, Hashtable objectsInUse, SerializationFormat serializationFormat) {
            sb.Append('{');
            bool isFirstElement = true;
            bool isTypeEntrySet = false;

            //make sure __type field is the first to be serialized if it exists
            if (o.Contains(ServerTypeFieldName)) {
                isFirstElement = false;
                isTypeEntrySet = true;
                SerializeDictionaryKeyValue(ServerTypeFieldName, o[ServerTypeFieldName], sb, depth, objectsInUse, serializationFormat);
            }

            foreach (DictionaryEntry entry in (IDictionary)o) {
                
                string key = entry.Key as string;
                if (key == null) {
                   throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, o.GetType().FullName));
                }
                if (isTypeEntrySet && String.Equals(key, ServerTypeFieldName, StringComparison.Ordinal)) {
                    // The dictionay only contains max one entry for __type key, and it has been iterated
                    // through, so don't need to check for is anymore.
                    isTypeEntrySet = false;
                    continue;
                }
                if (!isFirstElement) {
                    sb.Append(',');
                }
                SerializeDictionaryKeyValue(key, entry.Value, sb, depth, objectsInUse, serializationFormat);
                isFirstElement = false;
            }
            sb.Append('}');
        }

        private void SerializeDictionaryKeyValue(string key, object value, StringBuilder sb, int depth, Hashtable objectsInUse, SerializationFormat serializationFormat) {
            
            SerializeString(key, sb);
            sb.Append(':');
            SerializeValue(value, sb, depth, objectsInUse, serializationFormat);
        }

        private void SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, int depth, Hashtable objectsInUse, SerializationFormat serializationFormat) {
            sb.Append('[');
            bool isFirstElement = true;
            foreach (object o in enumerable) {
                if (!isFirstElement) {
                    sb.Append(',');
                }

                SerializeValue(o, sb, depth, objectsInUse, serializationFormat);
                isFirstElement = false;
            }
            sb.Append(']');
        }

        private static void SerializeString(string input, StringBuilder sb) {
            sb.Append('"');
            sb.Append(HttpUtility.JavaScriptStringEncode(input));
            sb.Append('"');
        }

        private void SerializeValue(object o, StringBuilder sb, int depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember = null) {
            if (++depth > _recursionLimit) {
                throw new ArgumentException(AtlasWeb.JSON_DepthLimitExceeded);
            }

            // Check whether a custom converter is available for this type.
            JavaScriptConverter converter = null;
            if (o != null && ConverterExistsForType(o.GetType(), out converter)) {
                IDictionary<string, object> dict = converter.Serialize(o, this);

                if (TypeResolver != null) {
                    string typeString = TypeResolver.ResolveTypeId(o.GetType());
                    if (typeString != null) {
                        dict[ServerTypeFieldName] = typeString;
                    }
                }

                sb.Append(Serialize(dict, serializationFormat));
                return;
            }

            SerializeValueInternal(o, sb, depth, objectsInUse, serializationFormat, currentMember);
        }

        // We use this for our cycle detection for the case where objects override equals/gethashcode
        private class ReferenceComparer : IEqualityComparer {
            bool IEqualityComparer.Equals(object x, object y) {
                return x == y;
            }

            int IEqualityComparer.GetHashCode(object obj) {
                return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
            }
        }

        private void SerializeValueInternal(object o, StringBuilder sb, int depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) {
            // 'null' is a special JavaScript token
            if (o == null || DBNull.Value.Equals(o)) {
                sb.Append("null");
                return;
            }

            // Strings and chars are represented as quoted (single or double) in Javascript.
            string os = o as String;
            if (os != null) {
                SerializeString(os, sb);
                return;
            }

            if (o is Char) {
                // Special case the null char as we don't want it to turn into a null string
                if ((char)o == '\0') {
                    sb.Append("null");
                    return;
                }
                SerializeString(o.ToString(), sb);
                return;
            }

            // Bools are represented as 'true' and 'false' (no quotes) in Javascript.
            if (o is bool) {
                SerializeBoolean((bool)o, sb);
                return;
            }

            if (o is DateTime) {
                SerializeDateTime((DateTime)o, sb, serializationFormat);
                return;
            }

            if (o is DateTimeOffset) {
                // DateTimeOffset is converted to a UTC DateTime and serialized as usual.
                SerializeDateTime(((DateTimeOffset)o).UtcDateTime, sb, serializationFormat);
                return;
            }

            if (o is Guid) {
                SerializeGuid((Guid)o, sb);
                return;
            }

            Uri uri = o as Uri;
            if (uri != null) {
                SerializeUri(uri, sb);
                return;
            }

            // Have to special case floats to get full precision
            if (o is double) {
                sb.Append(((double)o).ToString("r", CultureInfo.InvariantCulture));
                return;
            }

            if (o is float) {
                sb.Append(((float)o).ToString("r", CultureInfo.InvariantCulture));
                return;
            }

            // Deal with any server type that can be represented as a number in JavaScript
            if (o.GetType().IsPrimitive || o is Decimal) {
                IConvertible convertible = o as IConvertible;
                if (convertible != null) {
                    sb.Append(convertible.ToString(CultureInfo.InvariantCulture));
                }
                else {
                    // In theory, all primitive types implement IConvertible
                    Debug.Assert(false);
                    sb.Append(o.ToString());
                }
                return;
            }

            // Serialize enums as their integer value
            Type type = o.GetType();
            if (type.IsEnum) {
                // Int64 and UInt64 result in numbers too big for JavaScript
                Type underlyingType = Enum.GetUnderlyingType(type);
                if ((underlyingType == typeof(Int64)) || (underlyingType == typeof(UInt64))) {
                    // DevDiv #286382 - Try to provide a better error message by saying exactly what property failed.
                    string errorMessage = (currentMember != null)
                        ? String.Format(CultureInfo.CurrentCulture, AtlasWeb.JSON_CannotSerializeMemberGeneric, currentMember.Name, currentMember.ReflectedType.FullName) + " " + AtlasWeb.JSON_InvalidEnumType
                        : AtlasWeb.JSON_InvalidEnumType;
                    throw new InvalidOperationException(errorMessage);
                }
                // DevDiv Bugs 154763: call ToString("D") rather than cast to int
                // to support enums that are based on other integral types
                sb.Append(((Enum)o).ToString("D"));
                return;
            }

            try {
                // The following logic performs circular reference detection
                if (objectsInUse == null) {
                    // Create the table on demand
                    objectsInUse = new Hashtable(new ReferenceComparer());
                }
                else if (objectsInUse.ContainsKey(o)) {
                    // If the object is already there, we have a circular reference!
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, AtlasWeb.JSON_CircularReference, type.FullName));
                }
                // Add the object to the objectsInUse
                objectsInUse.Add(o, null);

                // Dictionaries are represented as Javascript objects.  e.g. { name1: val1, name2: val2 }
                IDictionary od = o as IDictionary;
                if (od != null) {
                    SerializeDictionary(od, sb, depth, objectsInUse, serializationFormat);
                    return;
                }

                // Enumerations are represented as Javascript arrays.  e.g. [ val1, val2 ]
                IEnumerable oenum = o as IEnumerable;
                if (oenum != null) {
                    SerializeEnumerable(oenum, sb, depth, objectsInUse, serializationFormat);
                    return;
                }

                // Serialize all public fields and properties.
                SerializeCustomObject(o, sb, depth, objectsInUse, serializationFormat);
            }
            finally {
                // Remove the object from the circular reference detection table
                if (objectsInUse != null) {
                    objectsInUse.Remove(o);
                }
            }
        }

        internal enum SerializationFormat {
            JSON,
            JavaScript
        }
    }
}