XmlObjectSerializerReadContextComplexJson.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 / XmlObjectSerializerReadContextComplexJson.cs / 3 / XmlObjectSerializerReadContextComplexJson.cs

                            using System; 
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml; 
using System.Security;
using System.ServiceModel; 
using System.Runtime.Serialization.Json; 
using System.ServiceModel.Web;
 
namespace System.Runtime.Serialization.Json
{
#if USE_REFEMIT
    public class XmlObjectSerializerReadContextComplexJson : XmlObjectSerializerReadContextComplex 
#else
    internal class XmlObjectSerializerReadContextComplexJson : XmlObjectSerializerReadContextComplex 
#endif 
    {
        string extensionDataValueType; 

        public XmlObjectSerializerReadContextComplexJson(DataContractJsonSerializer serializer, DataContract rootTypeDataContract)
            : base(serializer, serializer.MaxItemsInObjectGraph,
                new StreamingContext(StreamingContextStates.All), 
                serializer.IgnoreExtensionDataObject)
        { 
            this.rootTypeDataContract = rootTypeDataContract; 
            this.serializerKnownTypeList = serializer.knownTypeList;
            this.serializerKnownDataContracts = serializer.KnownDataContracts; 
            this.dataContractSurrogate = serializer.DataContractSurrogate;
        }

        internal IList SerializerKnownTypeList 
        {
            get 
            { 
                return this.serializerKnownTypeList;
            } 
        }

        protected override void StartReadExtensionDataValue(XmlReaderDelegator xmlReader)
        { 
            extensionDataValueType = xmlReader.GetAttribute(JsonGlobals.typeString);
        } 
 
        protected override IDataNode ReadPrimitiveExtensionDataValue(XmlReaderDelegator xmlReader, string dataContractName, string dataContractNamespace)
        { 
            IDataNode dataNode;

            switch (extensionDataValueType)
            { 
                case null:
                case JsonGlobals.stringString: 
                    dataNode = new DataNode(xmlReader.ReadContentAsString()); 
                    break;
                case JsonGlobals.booleanString: 
                    dataNode = new DataNode(xmlReader.ReadContentAsBoolean());
                    break;
                case JsonGlobals.numberString:
                    dataNode = ReadNumericalPrimitiveExtensionDataValue(xmlReader); 
                    break;
                default: 
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                        XmlObjectSerializer.CreateSerializationException(SR2.GetString(SR2.JsonUnexpectedAttributeValue, extensionDataValueType)));
            } 

            xmlReader.ReadEndElement();
            return dataNode;
        } 

        IDataNode ReadNumericalPrimitiveExtensionDataValue(XmlReaderDelegator xmlReader) 
        { 
            TypeCode type;
            object numericalValue = JsonObjectDataContract.ParseJsonNumber(xmlReader.ReadContentAsString(), out type); 
            switch (type)
            {
                case TypeCode.Byte:
                    return new DataNode((byte)numericalValue); 
                case TypeCode.SByte:
                    return new DataNode((sbyte)numericalValue); 
                case TypeCode.Int16: 
                    return new DataNode((short)numericalValue);
                case TypeCode.Int32: 
                    return new DataNode((int)numericalValue);
                case TypeCode.Int64:
                    return new DataNode((long)numericalValue);
                case TypeCode.UInt16: 
                    return new DataNode((ushort)numericalValue);
                case TypeCode.UInt32: 
                    return new DataNode((uint)numericalValue); 
                case TypeCode.UInt64:
                    return new DataNode((ulong)numericalValue); 
                case TypeCode.Single:
                    return new DataNode((float)numericalValue);
                case TypeCode.Double:
                    return new DataNode((double)numericalValue); 
                case TypeCode.Decimal:
                    return new DataNode((decimal)numericalValue); 
                default: 
                    Fx.Assert(false, "JsonObjectDataContract.ParseJsonNumber shouldn't return a TypeCode that we're not expecting");
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); 
            }
        }

        internal static XmlObjectSerializerReadContextComplexJson CreateContext(DataContractJsonSerializer serializer, DataContract rootTypeDataContract) 
        {
            return new XmlObjectSerializerReadContextComplexJson(serializer, rootTypeDataContract); 
        } 

#if USE_REFEMIT 
        public override int GetArraySize()
#else
        internal override int GetArraySize()
#endif 
        {
            return -1; 
        } 

        protected override object ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader) 
        {
            return DataContractJsonSerializer.ReadJsonValue(dataContract, reader, this);
        }
 
#if USE_REFEMIT
        public override void ReadAttributes(XmlReaderDelegator xmlReader) 
#else 
        internal override void ReadAttributes(XmlReaderDelegator xmlReader)
#endif 
        {
            if (attributes == null)
                attributes = new Attributes();
            attributes.Reset(); 

            if (xmlReader.MoveToAttribute(JsonGlobals.typeString) && xmlReader.Value == JsonGlobals.nullString) 
            { 
                attributes.XsiNil = true;
            } 
            else if (xmlReader.MoveToAttribute(JsonGlobals.serverTypeString))
            {
                XmlQualifiedName qualifiedTypeName = JsonReaderDelegator.ParseQualifiedName(xmlReader.Value);
                attributes.XsiTypeName = qualifiedTypeName.Name; 

                string serverTypeNamespace = qualifiedTypeName.Namespace; 
 
                if (!string.IsNullOrEmpty(serverTypeNamespace))
                { 
                    switch (serverTypeNamespace[0])
                    {
                        case '#':
                            serverTypeNamespace = string.Concat(Globals.DataContractXsdBaseNamespace, serverTypeNamespace.Substring(1)); 
                            break;
                        case '\\': 
                            if (serverTypeNamespace.Length >= 2) 
                            {
                                switch (serverTypeNamespace[1]) 
                                {
                                    case '#':
                                    case '\\':
                                        serverTypeNamespace = serverTypeNamespace.Substring(1); 
                                        break;
                                    default: 
                                        break; 
                                }
                            } 
                            break;
                        default:
                            break;
                    } 
                }
 
                attributes.XsiTypeNamespace = serverTypeNamespace; 
            }
            xmlReader.MoveToElement(); 
        }


#if USE_REFEMIT 
        public int GetJsonMemberIndex(XmlReaderDelegator xmlReader,  XmlDictionaryString[] memberNames, int memberIndex, ExtensionDataObject extensionData)
#else 
        internal int GetJsonMemberIndex(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, int memberIndex, ExtensionDataObject extensionData) 
#endif
        { 
            int length = memberNames.Length;
            if (length != 0)
            {
                for (int i = 0, index = (memberIndex + 1) % length; i < length; i++, index = (index + 1) % length) 
                {
                    if (xmlReader.IsStartElement(memberNames[index], XmlDictionaryString.Empty)) 
                    { 
                        return index;
                    } 
                }
                string name;
                if (TryGetJsonLocalName(xmlReader, out name))
                { 
                    for (int i = 0, index = (memberIndex + 1) % length; i < length; i++, index = (index + 1) % length)
                    { 
                        if (memberNames[index].Value == name) 
                        {
                            return index; 
                        }
                    }
                }
            } 
            HandleMemberNotFound(xmlReader, extensionData, memberIndex);
            return length; 
        } 

        internal static bool TryGetJsonLocalName(XmlReaderDelegator xmlReader, out string name) 
        {
            if (xmlReader.IsStartElement(JsonGlobals.itemDictionaryString, JsonGlobals.itemDictionaryString))
            {
                if (xmlReader.MoveToAttribute(JsonGlobals.itemString)) 
                {
                    name = xmlReader.Value; 
                    return true; 
                }
            } 
            name = null;
            return false;
        }
 
#if USE_REFEMIT
        public static void ThrowMissingRequiredMembers(object obj, XmlDictionaryString[] memberNames, byte[] expectedElements, byte[] requiredElements) 
#else 
        static internal void ThrowMissingRequiredMembers(object obj, XmlDictionaryString[] memberNames, byte[] expectedElements, byte[] requiredElements)
#endif 
        {
            StringBuilder stringBuilder = new StringBuilder();
            int missingMembersCount = 0;
            for (int i = 0; i < memberNames.Length; i++) 
            {
                if (IsBitSet(expectedElements, i) && IsBitSet(requiredElements, i)) 
                { 
                    if (stringBuilder.Length != 0)
                        stringBuilder.Append(", "); 
                    stringBuilder.Append(memberNames[i]);
                    missingMembersCount++;
                }
            } 

            if (missingMembersCount == 1) 
            { 
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR2.GetString(
                 SR2.JsonOneRequiredMemberNotFound, DataContract.GetClrTypeFullName(obj.GetType()), stringBuilder.ToString()))); 
            }
            else
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR2.GetString( 
                    SR2.JsonRequiredMembersNotFound, DataContract.GetClrTypeFullName(obj.GetType()), stringBuilder.ToString())));
            } 
        } 

 

#if USE_REFEMIT
        public static void ThrowDuplicateMemberException(object obj, XmlDictionaryString[] memberNames, int memberIndex)
#else 
        static internal void ThrowDuplicateMemberException(object obj, XmlDictionaryString[] memberNames, int memberIndex)
#endif 
        { 
            throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(
                SR2.GetString(SR2.JsonDuplicateMemberInInput, DataContract.GetClrTypeFullName(obj.GetType()), memberNames[memberIndex]))); 
        }

        /// 
        /// Critical - accesses SecurityCritical helper class 'BitFlagsGenerator' 
        /// Safe - this method is safe to call
        ///  
        [SecurityCritical, SecurityTreatAsSafe] 
        static bool IsBitSet(byte[] bytes, int bitIndex)
        { 
            return BitFlagsGenerator.IsBitSet(bytes, bitIndex);
        }

        protected override bool IsReadingCollectionExtensionData(XmlReaderDelegator xmlReader) 
        {
            return xmlReader.GetAttribute(JsonGlobals.typeString) == JsonGlobals.arrayString; 
        } 

        protected override bool IsReadingClassExtensionData(XmlReaderDelegator xmlReader) 
        {
            return xmlReader.GetAttribute(JsonGlobals.typeString) == JsonGlobals.objectString;
        }
 
        protected override XmlReaderDelegator CreateReaderDelegatorForReader(XmlReader xmlReader)
        { 
            return new JsonReaderDelegator(xmlReader); 
        }
 
        internal override DataContract GetDataContract(RuntimeTypeHandle typeHandle, Type type)
        {
            DataContract dataContract = base.GetDataContract(typeHandle, type);
            DataContractJsonSerializer.CheckIfTypeIsReference(dataContract); 
            return dataContract;
        } 
 
        internal override DataContract GetDataContractSkipValidation(int typeId, RuntimeTypeHandle typeHandle, Type type)
        { 
            DataContract dataContract = base.GetDataContractSkipValidation(typeId, typeHandle, type);
            DataContractJsonSerializer.CheckIfTypeIsReference(dataContract);
            return dataContract;
        } 

        internal override DataContract GetDataContract(int id, RuntimeTypeHandle typeHandle) 
        { 
            DataContract dataContract = base.GetDataContract(id, typeHandle);
            DataContractJsonSerializer.CheckIfTypeIsReference(dataContract); 
            return dataContract;
        }
    }
} 

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