JsonEnumDataContract.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 / JsonEnumDataContract.cs / 1 / JsonEnumDataContract.cs

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

namespace System.Runtime.Serialization.Json 
{
    using System.Xml; 
    using System.Security; 

    class JsonEnumDataContract : JsonDataContract 
    {
        // 
        // 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] 
        JsonEnumDataContractCriticalHelper helper;
 
        // 
        // Critical - initializes SecurityCritical field 'helper'
        // Safe - doesn't leak anything
        //  
        [SecurityCritical, SecurityTreatAsSafe]
        public JsonEnumDataContract(EnumDataContract traditionalDataContract) : base(new JsonEnumDataContractCriticalHelper(traditionalDataContract)) 
        { 
            this.helper = base.Helper as JsonEnumDataContractCriticalHelper;
        } 

        public bool IsULong
        {
            //  
            // Critical - fetches the critical IsULong property
            // Safe - IsULong only needs to be protected for write 
            //  
            [SecurityCritical, SecurityTreatAsSafe]
            get { return this.helper.IsULong; } 
        }

        public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
        { 
            object enumValue;
            if (IsULong) 
            { 
                enumValue = Enum.ToObject(TraditionalDataContract.UnderlyingType, jsonReader.ReadElementContentAsUnsignedLong());
            } 
            else
            {
                enumValue = Enum.ToObject(TraditionalDataContract.UnderlyingType, jsonReader.ReadElementContentAsLong());
            } 

            if (context != null) 
            { 
                context.AddNewObject(enumValue);
            } 
            return enumValue;
        }

        public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) 
        {
            if (IsULong) 
            { 
                jsonWriter.WriteUnsignedLong(((IConvertible) obj).ToUInt64(null));
            } 
            else
            {
                jsonWriter.WriteLong(((IConvertible) obj).ToInt64(null));
            } 
        }
 
        //  
        // Critical - holds all state used for (de)serializing types.
        //            since the data is cached statically, we lock down access to it. 
        // 
        [SecurityCritical(SecurityCriticalScope.Everything)]
        class JsonEnumDataContractCriticalHelper : JsonDataContractCriticalHelper
        { 
            bool isULong;
 
            public JsonEnumDataContractCriticalHelper(EnumDataContract traditionalEnumDataContract) : base(traditionalEnumDataContract) 
            {
                isULong = traditionalEnumDataContract.IsULong; 
            }

            public bool IsULong
            { 
                get { return this.isULong; }
            } 
        } 
    }
} 

// 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