JsonDataContract.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / Runtime / Serialization / Json / JsonDataContract.cs / 1 / JsonDataContract.cs

                            //---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------

namespace System.Runtime.Serialization.Json 
{
    using System.Runtime.Serialization; 
    using System.Collections.Generic; 
    using System.Reflection;
    using System.ServiceModel; 
    using System.Xml;
    using System.Security;
    using System.ServiceModel.Web;
 
#if USE_REFEMIT
    public class JsonDataContract 
#else 
    class JsonDataContract
#endif 
    {

        // 
        // Critical - holds instance of CriticalHelper which keeps state that is cached statically for serialization. 
        //            Static fields are marked SecurityCritical or readonly to prevent
        //            data from being modified or leaked to other components in appdomain. 
        //  
        [SecurityCritical]
        JsonDataContractCriticalHelper helper; 

        // 
        // Critical - initializes SecurityCritical field 'helper'
        // Safe - doesn't leak anything 
        // 
        [SecurityCritical, SecurityTreatAsSafe] 
        protected JsonDataContract(DataContract traditionalDataContract) 
        {
            this.helper = new JsonDataContractCriticalHelper(traditionalDataContract); 
        }

        // 
        // Critical - initializes SecurityCritical field 'helper' 
        // Safe - doesn't leak anything
        //  
        [SecurityCritical, SecurityTreatAsSafe] 
        protected JsonDataContract(JsonDataContractCriticalHelper helper)
        { 
            this.helper = helper;
        }

        internal virtual string TypeName 
        {
            get { return null; } 
        } 

        protected JsonDataContractCriticalHelper Helper 
        {
            // 
            // Critical - holds instance of CriticalHelper which keeps state that is cached statically for serialization.
            //            Static fields are marked SecurityCritical or readonly to prevent 
            //            data from being modified or leaked to other components in appdomain.
            //  
            [SecurityCritical] 
            get { return helper; }
        } 

        protected DataContract TraditionalDataContract
        {
            //  
            // Critical - fetches the critical TraditionalDataContract from the helper
            // Safe - TraditionalDataContract needs to be protected for only write 
            //  
            [SecurityCritical, SecurityTreatAsSafe]
            get { return this.helper.TraditionalDataContract; } 
        }

        Dictionary KnownDataContracts
        { 
            // 
            // Critical - fetches the critical KnownDataContracts from the helper 
            // Safe - KnownDataContracts needs to be protected for only write 
            // 
            [SecurityCritical, SecurityTreatAsSafe] 
            get { return this.helper.KnownDataContracts; }
        }

        //  
        // Critical - fetches the critical JsonDataContract from the helper
        // Safe - Needs to be protected for only write 
        //  
        [SecurityCritical, SecurityTreatAsSafe]
        public static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract) 
        {
            return JsonDataContractCriticalHelper.GetJsonDataContract(traditionalDataContract);
        }
 
        public object ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
        { 
            PushKnownDataContracts(context); 
            object deserializedObject = ReadJsonValueCore(jsonReader, context);
            PopKnownDataContracts(context); 
            return deserializedObject;
        }

        public virtual object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
        {
            return TraditionalDataContract.ReadXmlValue(jsonReader, context); 
        } 

        public void WriteJsonValue(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) 
        {
            PushKnownDataContracts(context);
            WriteJsonValueCore(jsonWriter, obj, context, declaredTypeHandle);
            PopKnownDataContracts(context); 
        }
 
        public virtual void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) 
        {
            TraditionalDataContract.WriteXmlValue(jsonWriter, obj, context); 
        }

        // 
        // Review - checks type visibility to calculate if access to it requires MemberAccessPermission. 
        //          since this information is used to determine whether to give the generated code access
        //          permissions to private members, any changes to the logic should be reviewed. 
        //  
        [SecurityRequiresReview]
        internal static bool IsTypeVisibleInJsonSerializationModule(Type type) 
        {
            return type.Module.Equals(JsonGlobals.JsonSerializationModule);
        }
 
        protected static object HandleReadValue(object obj, XmlObjectSerializerReadContext context)
        { 
            context.AddNewObject(obj); 
            return obj;
        } 

        protected static bool TryReadNullAtTopLevel(XmlReaderDelegator reader)
        {
            while (reader.MoveToAttribute(JsonGlobals.typeString) && (reader.Value == JsonGlobals.nullString)) 
            {
                reader.Skip(); 
                reader.MoveToElement(); 
                return true;
            } 

            reader.MoveToElement();
            return false;
        } 

        protected void PopKnownDataContracts(XmlObjectSerializerContext context) 
        { 
            if (KnownDataContracts != null)
            { 
                context.scopedKnownTypes.Pop();
            }
        }
 
        protected void PushKnownDataContracts(XmlObjectSerializerContext context)
        { 
            if (KnownDataContracts != null) 
            {
                context.scopedKnownTypes.Push(KnownDataContracts); 
            }
        }

        //  
        // Critical - holds all state used for (de)serializing types.
        //            since the data is cached statically, we lock down access to it. 
        //  
        [SecurityCritical(SecurityCriticalScope.Everything)]
        internal class JsonDataContractCriticalHelper 
        {

            static object cacheLock = new object();
            static object createDataContractLock = new object(); 

            static JsonDataContract[] dataContractCache = new JsonDataContract[32]; 
            static int dataContractID = 0; 

            static TypeHandleRef typeHandleRef = new TypeHandleRef(); 
            static Dictionary typeToIDCache = new Dictionary(new TypeHandleRefEqualityComparer());
            Dictionary knownDataContracts;
            DataContract traditionalDataContract;
            string typeName; 

            internal JsonDataContractCriticalHelper(DataContract traditionalDataContract) 
            { 
                this.traditionalDataContract = traditionalDataContract;
                AddCollectionItemContractsToKnownDataContracts(); 
                this.typeName = string.IsNullOrEmpty(traditionalDataContract.Namespace.Value) ? traditionalDataContract.Name.Value : string.Concat(traditionalDataContract.Name.Value, JsonGlobals.NameValueSeparatorString, XmlObjectSerializerWriteContextComplexJson.TruncateDefaultDataContractNamespace(traditionalDataContract.Namespace.Value));
            }

            internal Dictionary KnownDataContracts 
            {
                get { return this.knownDataContracts; } 
            } 

            internal DataContract TraditionalDataContract 
            {
                get { return this.traditionalDataContract; }
            }
 
            internal virtual string TypeName
            { 
                get { return this.typeName; } 
            }
 
            public static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract)
            {
                int id = JsonDataContractCriticalHelper.GetId(traditionalDataContract.UnderlyingType.TypeHandle);
                JsonDataContract dataContract = dataContractCache[id]; 
                if (dataContract == null)
                { 
                    dataContract = CreateJsonDataContract(id, traditionalDataContract); 
                    dataContractCache[id] = dataContract;
                } 
                return dataContract;
            }

            internal static int GetId(RuntimeTypeHandle typeHandle) 
            {
                lock (cacheLock) 
                { 
                    IntRef id;
                    typeHandleRef.Value = typeHandle; 
                    if (!typeToIDCache.TryGetValue(typeHandleRef, out id))
                    {
                        int value = dataContractID++;
                        if (value >= dataContractCache.Length) 
                        {
                            int newSize = (value < Int32.MaxValue / 2) ? value * 2 : Int32.MaxValue; 
                            if (newSize <= value) 
                            {
                                Fx.Assert("DataContract cache overflow"); 
                                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.DataContractCacheOverflow)));
                            }
                            Array.Resize(ref dataContractCache, newSize);
                        } 
                        id = new IntRef(value);
                        try 
                        { 
                            typeToIDCache.Add(new TypeHandleRef(typeHandle), id);
                        } 
                        catch (Exception ex)
                        {
                            if (System.ServiceModel.DiagnosticUtility.IsFatal(ex))
                            { 
                                throw;
                            } 
                            throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperFatal(ex.Message, ex); 
                        }
                    } 
                    return id.Value;
                }
            }
 
            static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract)
            { 
                lock (createDataContractLock) 
                {
                    JsonDataContract dataContract = dataContractCache[id]; 
                    if (dataContract == null)
                    {
                        Type traditionalDataContractType = traditionalDataContract.GetType();
                        if (traditionalDataContractType == typeof(ObjectDataContract)) 
                        {
                            dataContract = new JsonObjectDataContract(traditionalDataContract); 
                        } 
                        else if (traditionalDataContractType == typeof(StringDataContract))
                        { 
                            dataContract = new JsonStringDataContract((StringDataContract) traditionalDataContract);
                        }
                        else if (traditionalDataContractType == typeof(UriDataContract))
                        { 
                            dataContract = new JsonUriDataContract((UriDataContract) traditionalDataContract);
                        } 
                        else if (traditionalDataContractType == typeof(QNameDataContract)) 
                        {
                            dataContract = new JsonQNameDataContract((QNameDataContract) traditionalDataContract); 
                        }
                        else if (traditionalDataContractType == typeof(ByteArrayDataContract))
                        {
                            dataContract = new JsonByteArrayDataContract((ByteArrayDataContract) traditionalDataContract); 
                        }
                        else if (traditionalDataContract.IsPrimitive || 
                            traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName) 
                        {
                            dataContract = new JsonDataContract(traditionalDataContract); 
                        }
                        else if (traditionalDataContractType == typeof(ClassDataContract))
                        {
                            dataContract = new JsonClassDataContract((ClassDataContract) traditionalDataContract); 
                        }
                        else if (traditionalDataContractType == typeof(EnumDataContract)) 
                        { 
                            dataContract = new JsonEnumDataContract((EnumDataContract) traditionalDataContract);
                        } 
                        else if ((traditionalDataContractType == typeof(GenericParameterDataContract)) ||
                            (traditionalDataContractType == typeof(SpecialTypeDataContract)))
                        {
                            dataContract = new JsonDataContract(traditionalDataContract); 
                        }
                        else if (traditionalDataContractType == typeof(CollectionDataContract)) 
                        { 
                            dataContract = new JsonCollectionDataContract((CollectionDataContract) traditionalDataContract);
                        } 
                        else if (traditionalDataContractType == typeof(XmlDataContract))
                        {
                            dataContract = new JsonXmlDataContract((XmlDataContract) traditionalDataContract);
                        } 
                        else
                        { 
                            throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("traditionalDataContract", 
                                SR2.GetString(SR2.JsonTypeNotSupportedByDataContractJsonSerializer, traditionalDataContract.UnderlyingType));
                        } 
                    }
                    return dataContract;
                }
            } 

            void AddCollectionItemContractsToKnownDataContracts() 
            { 
                if (traditionalDataContract.KnownDataContracts != null)
                { 
                    foreach (KeyValuePair knownDataContract in traditionalDataContract.KnownDataContracts)
                    {
                        if (!object.ReferenceEquals(knownDataContract, null))
                        { 
                            CollectionDataContract collectionDataContract = knownDataContract.Value as CollectionDataContract;
                            while (collectionDataContract != null) 
                            { 
                                DataContract itemContract = collectionDataContract.ItemContract;
                                if (knownDataContracts == null) 
                                {
                                    knownDataContracts = new Dictionary();
                                }
 
                                if (!knownDataContracts.ContainsKey(itemContract.StableName))
                                { 
                                    knownDataContracts.Add(itemContract.StableName, itemContract); 
                                }
 
                                if (collectionDataContract.ItemType.IsGenericType
                                    && collectionDataContract.ItemType.GetGenericTypeDefinition() == typeof(KeyValue <, > ))
                                {
                                    DataContract itemDataContract = DataContract.GetDataContract(Globals.TypeOfKeyValuePair.MakeGenericType(collectionDataContract.ItemType.GetGenericArguments())); 
                                    if (!knownDataContracts.ContainsKey(itemDataContract.StableName))
                                    { 
                                        knownDataContracts.Add(itemDataContract.StableName, itemDataContract); 
                                    }
                                } 

                                if (!(itemContract is CollectionDataContract))
                                {
                                    break; 
                                }
                                collectionDataContract = itemContract as CollectionDataContract; 
                            } 
                        }
                    } 
                }
            }
        }
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK