Ejemplo n.º 1
0
 private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract)
 {
     lock (createDataContractLock)
     {
         JsonDataContract contract = dataContractCache[id];
         if (contract == null)
         {
             Type type = traditionalDataContract.GetType();
             if (type == typeof(ObjectDataContract))
             {
                 contract = new JsonObjectDataContract(traditionalDataContract);
             }
             else if (type == typeof(StringDataContract))
             {
                 contract = new JsonStringDataContract((StringDataContract)traditionalDataContract);
             }
             else if (type == typeof(UriDataContract))
             {
                 contract = new JsonUriDataContract((UriDataContract)traditionalDataContract);
             }
             else if (type == typeof(QNameDataContract))
             {
                 contract = new JsonQNameDataContract((QNameDataContract)traditionalDataContract);
             }
             else if (type == typeof(ByteArrayDataContract))
             {
                 contract = new JsonByteArrayDataContract((ByteArrayDataContract)traditionalDataContract);
             }
             else if (traditionalDataContract.IsPrimitive || (traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName))
             {
                 contract = new JsonDataContract(traditionalDataContract);
             }
             else if (type == typeof(ClassDataContract))
             {
                 contract = new JsonClassDataContract((ClassDataContract)traditionalDataContract);
             }
             else if (type == typeof(EnumDataContract))
             {
                 contract = new JsonEnumDataContract((EnumDataContract)traditionalDataContract);
             }
             else if ((type == typeof(GenericParameterDataContract)) || (type == typeof(SpecialTypeDataContract)))
             {
                 contract = new JsonDataContract(traditionalDataContract);
             }
             else if (type == typeof(CollectionDataContract))
             {
                 contract = new JsonCollectionDataContract((CollectionDataContract)traditionalDataContract);
             }
             else
             {
                 if (type != typeof(XmlDataContract))
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("traditionalDataContract", System.Runtime.Serialization.SR.GetString("JsonTypeNotSupportedByDataContractJsonSerializer", new object[] { traditionalDataContract.UnderlyingType }));
                 }
                 contract = new JsonXmlDataContract((XmlDataContract)traditionalDataContract);
             }
         }
         return(contract);
     }
 }
        private IDataNode ReadNumericalPrimitiveExtensionDataValue(XmlReaderDelegator xmlReader)
        {
            TypeCode type;
            object   numericalValue = JsonObjectDataContract.ParseJsonNumber(xmlReader.ReadContentAsString(), out type);

            switch (type)
            {
            case TypeCode.Byte:
                return(new DataNode <byte>((byte)numericalValue));

            case TypeCode.SByte:
                return(new DataNode <sbyte>((sbyte)numericalValue));

            case TypeCode.Int16:
                return(new DataNode <short>((short)numericalValue));

            case TypeCode.Int32:
                return(new DataNode <int>((int)numericalValue));

            case TypeCode.Int64:
                return(new DataNode <long>((long)numericalValue));

            case TypeCode.UInt16:
                return(new DataNode <ushort>((ushort)numericalValue));

            case TypeCode.UInt32:
                return(new DataNode <uint>((uint)numericalValue));

            case TypeCode.UInt64:
                return(new DataNode <ulong>((ulong)numericalValue));

            case TypeCode.Single:
                return(new DataNode <float>((float)numericalValue));

            case TypeCode.Double:
                return(new DataNode <double>((double)numericalValue));

            case TypeCode.Decimal:
                return(new DataNode <decimal>((decimal)numericalValue));

            default:
                throw new InvalidOperationException(SR.ParseJsonNumberReturnInvalidNumber);
            }
        }
Ejemplo n.º 3
0
        IDataNode ReadNumericalPrimitiveExtensionDataValue(XmlReaderDelegator xmlReader)
        {
            TypeCode type;
            object   numericalValue = JsonObjectDataContract.ParseJsonNumber(xmlReader.ReadContentAsString(), out type);

            switch (type)
            {
            case TypeCode.Byte:
                return(new DataNode <byte>((byte)numericalValue));

            case TypeCode.SByte:
                return(new DataNode <sbyte>((sbyte)numericalValue));

            case TypeCode.Int16:
                return(new DataNode <short>((short)numericalValue));

            case TypeCode.Int32:
                return(new DataNode <int>((int)numericalValue));

            case TypeCode.Int64:
                return(new DataNode <long>((long)numericalValue));

            case TypeCode.UInt16:
                return(new DataNode <ushort>((ushort)numericalValue));

            case TypeCode.UInt32:
                return(new DataNode <uint>((uint)numericalValue));

            case TypeCode.UInt64:
                return(new DataNode <ulong>((ulong)numericalValue));

            case TypeCode.Single:
                return(new DataNode <float>((float)numericalValue));

            case TypeCode.Double:
                return(new DataNode <double>((double)numericalValue));

            case TypeCode.Decimal:
                return(new DataNode <decimal>((decimal)numericalValue));

            default:
                throw Fx.AssertAndThrow("JsonObjectDataContract.ParseJsonNumber shouldn't return a TypeCode that we're not expecting");
            }
        }
        private static IDataNode ReadNumericalPrimitiveExtensionDataValue(XmlReaderDelegator xmlReader)
        {
            TypeCode type;
            object   numericalValue = JsonObjectDataContract.ParseJsonNumber(xmlReader.ReadContentAsString(), out type);

            return(type switch
            {
                TypeCode.Byte => new DataNode <byte>((byte)numericalValue),
                TypeCode.SByte => new DataNode <sbyte>((sbyte)numericalValue),
                TypeCode.Int16 => new DataNode <short>((short)numericalValue),
                TypeCode.Int32 => new DataNode <int>((int)numericalValue),
                TypeCode.Int64 => new DataNode <long>((long)numericalValue),
                TypeCode.UInt16 => new DataNode <ushort>((ushort)numericalValue),
                TypeCode.UInt32 => new DataNode <uint>((uint)numericalValue),
                TypeCode.UInt64 => new DataNode <ulong>((ulong)numericalValue),
                TypeCode.Single => new DataNode <float>((float)numericalValue),
                TypeCode.Double => new DataNode <double>((double)numericalValue),
                TypeCode.Decimal => new DataNode <decimal>((decimal)numericalValue),
                _ => throw new InvalidOperationException(SR.ParseJsonNumberReturnInvalidNumber),
            });
Ejemplo n.º 5
0
 private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract)
 {
     lock (s_createDataContractLock)
     {
         JsonDataContract dataContract = s_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 new ArgumentException(SR.Format(SR.JsonTypeNotSupportedByDataContractJsonSerializer, traditionalDataContract.UnderlyingType), "traditionalDataContract");
             }
         }
         return dataContract;
     }
 }
Ejemplo n.º 6
0
 private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract)
 {
     lock (s_createDataContractLock)
     {
         JsonDataContract dataContract = s_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 new ArgumentException(SR.Format(SR.JsonTypeNotSupportedByDataContractJsonSerializer, traditionalDataContract.UnderlyingType), nameof(traditionalDataContract));
             }
         }
         return(dataContract);
     }
 }
 private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract)
 {
     lock (createDataContractLock)
     {
         JsonDataContract contract = dataContractCache[id];
         if (contract == null)
         {
             Type type = traditionalDataContract.GetType();
             if (type == typeof(ObjectDataContract))
             {
                 contract = new JsonObjectDataContract(traditionalDataContract);
             }
             else if (type == typeof(StringDataContract))
             {
                 contract = new JsonStringDataContract((StringDataContract) traditionalDataContract);
             }
             else if (type == typeof(UriDataContract))
             {
                 contract = new JsonUriDataContract((UriDataContract) traditionalDataContract);
             }
             else if (type == typeof(QNameDataContract))
             {
                 contract = new JsonQNameDataContract((QNameDataContract) traditionalDataContract);
             }
             else if (type == typeof(ByteArrayDataContract))
             {
                 contract = new JsonByteArrayDataContract((ByteArrayDataContract) traditionalDataContract);
             }
             else if (traditionalDataContract.IsPrimitive || (traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName))
             {
                 contract = new JsonDataContract(traditionalDataContract);
             }
             else if (type == typeof(ClassDataContract))
             {
                 contract = new JsonClassDataContract((ClassDataContract) traditionalDataContract);
             }
             else if (type == typeof(EnumDataContract))
             {
                 contract = new JsonEnumDataContract((EnumDataContract) traditionalDataContract);
             }
             else if ((type == typeof(GenericParameterDataContract)) || (type == typeof(SpecialTypeDataContract)))
             {
                 contract = new JsonDataContract(traditionalDataContract);
             }
             else if (type == typeof(CollectionDataContract))
             {
                 contract = new JsonCollectionDataContract((CollectionDataContract) traditionalDataContract);
             }
             else
             {
                 if (type != typeof(XmlDataContract))
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("traditionalDataContract", System.Runtime.Serialization.SR.GetString("JsonTypeNotSupportedByDataContractJsonSerializer", new object[] { traditionalDataContract.UnderlyingType }));
                 }
                 contract = new JsonXmlDataContract((XmlDataContract) traditionalDataContract);
             }
         }
         return contract;
     }
 }