private static void VerifyObjectCompatibilityWithInterface(DataContract contract, object graph, Type declaredType) { Type type = contract.GetType(); if ((type == typeof(XmlDataContract)) && !Globals.TypeOfIXmlSerializable.IsAssignableFrom(declaredType)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("XmlObjectAssignedToIncompatibleInterface", new object[] { graph.GetType(), declaredType }))); } if ((type == typeof(CollectionDataContract)) && !CollectionDataContract.IsCollectionInterface(declaredType)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("CollectionAssignedToIncompatibleInterface", new object[] { graph.GetType(), declaredType }))); } }
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; } }
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; } }