Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / Json / JsonXmlDataContract.cs / 1305376 / JsonXmlDataContract.cs
//---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Runtime.Serialization.Json { using System.Xml; using System.Collections.Generic; using System.IO; using System.Text; class JsonXmlDataContract : JsonDataContract { public JsonXmlDataContract(XmlDataContract traditionalXmlDataContract) : base(traditionalXmlDataContract) { } public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) { string xmlContent = jsonReader.ReadElementContentAsString(); DataContractSerializer dataContractSerializer = new DataContractSerializer(TraditionalDataContract.UnderlyingType, GetKnownTypesFromContext(context, (context == null) ? null : context.SerializerKnownTypeList), 1, false, false, null); // maxItemsInObjectGraph // ignoreExtensionDataObject // preserveObjectReferences // dataContractSurrogate MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlContent)); object xmlValue; XmlDictionaryReaderQuotas quotas = ((JsonReaderDelegator) jsonReader).ReaderQuotas; if (quotas == null) { xmlValue = dataContractSerializer.ReadObject(memoryStream); } else { xmlValue = dataContractSerializer.ReadObject(XmlDictionaryReader.CreateTextReader(memoryStream, quotas)); } if (context != null) { context.AddNewObject(xmlValue); } return xmlValue; } public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) { DataContractSerializer dataContractSerializer = new DataContractSerializer(Type.GetTypeFromHandle(declaredTypeHandle), GetKnownTypesFromContext(context, (context == null) ? null : context.SerializerKnownTypeList), 1, false, false, null); // maxItemsInObjectGraph // ignoreExtensionDataObject // preserveObjectReferences // dataContractSurrogate MemoryStream memoryStream = new MemoryStream(); dataContractSerializer.WriteObject(memoryStream, obj); memoryStream.Position = 0; string serialized = new StreamReader(memoryStream).ReadToEnd(); jsonWriter.WriteString(serialized); } ListGetKnownTypesFromContext(XmlObjectSerializerContext context, IList serializerKnownTypeList) { List knownTypesList = new List (); if (context != null) { List stableNames = new List (); Dictionary [] entries = context.scopedKnownTypes.dataContractDictionaries; if (entries != null) { for (int i = 0; i < entries.Length; i++) { Dictionary entry = entries[i]; if (entry != null) { foreach (KeyValuePair pair in entry) { if (!stableNames.Contains(pair.Key)) { stableNames.Add(pair.Key); knownTypesList.Add(pair.Value.UnderlyingType); } } } } } if (serializerKnownTypeList != null) { knownTypesList.AddRange(serializerKnownTypeList); } } return knownTypesList; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Runtime.Serialization.Json { using System.Xml; using System.Collections.Generic; using System.IO; using System.Text; class JsonXmlDataContract : JsonDataContract { public JsonXmlDataContract(XmlDataContract traditionalXmlDataContract) : base(traditionalXmlDataContract) { } public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) { string xmlContent = jsonReader.ReadElementContentAsString(); DataContractSerializer dataContractSerializer = new DataContractSerializer(TraditionalDataContract.UnderlyingType, GetKnownTypesFromContext(context, (context == null) ? null : context.SerializerKnownTypeList), 1, false, false, null); // maxItemsInObjectGraph // ignoreExtensionDataObject // preserveObjectReferences // dataContractSurrogate MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlContent)); object xmlValue; XmlDictionaryReaderQuotas quotas = ((JsonReaderDelegator) jsonReader).ReaderQuotas; if (quotas == null) { xmlValue = dataContractSerializer.ReadObject(memoryStream); } else { xmlValue = dataContractSerializer.ReadObject(XmlDictionaryReader.CreateTextReader(memoryStream, quotas)); } if (context != null) { context.AddNewObject(xmlValue); } return xmlValue; } public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) { DataContractSerializer dataContractSerializer = new DataContractSerializer(Type.GetTypeFromHandle(declaredTypeHandle), GetKnownTypesFromContext(context, (context == null) ? null : context.SerializerKnownTypeList), 1, false, false, null); // maxItemsInObjectGraph // ignoreExtensionDataObject // preserveObjectReferences // dataContractSurrogate MemoryStream memoryStream = new MemoryStream(); dataContractSerializer.WriteObject(memoryStream, obj); memoryStream.Position = 0; string serialized = new StreamReader(memoryStream).ReadToEnd(); jsonWriter.WriteString(serialized); } List GetKnownTypesFromContext(XmlObjectSerializerContext context, IList serializerKnownTypeList) { List knownTypesList = new List (); if (context != null) { List stableNames = new List (); Dictionary [] entries = context.scopedKnownTypes.dataContractDictionaries; if (entries != null) { for (int i = 0; i < entries.Length; i++) { Dictionary entry = entries[i]; if (entry != null) { foreach (KeyValuePair pair in entry) { if (!stableNames.Contains(pair.Key)) { stableNames.Add(pair.Key); knownTypesList.Add(pair.Value.UnderlyingType); } } } } } if (serializerKnownTypeList != null) { knownTypesList.AddRange(serializerKnownTypeList); } } return knownTypesList; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BitmapSource.cs
- CodeParameterDeclarationExpressionCollection.cs
- ObjectDataSourceMethodEventArgs.cs
- DataColumnCollection.cs
- ExceptionHandlerDesigner.cs
- panel.cs
- ModelFunctionTypeElement.cs
- SettingsSavedEventArgs.cs
- RtfControls.cs
- CodeTypeMemberCollection.cs
- WebBrowserUriTypeConverter.cs
- FunctionImportElement.cs
- OleStrCAMarshaler.cs
- TransactionCache.cs
- PEFileReader.cs
- TemplateControlCodeDomTreeGenerator.cs
- Axis.cs
- RelAssertionDirectKeyIdentifierClause.cs
- ViewCellSlot.cs
- complextypematerializer.cs
- DllNotFoundException.cs
- TypeDependencyAttribute.cs
- FixedSOMTableRow.cs
- ReferencedAssemblyResolver.cs
- sqlinternaltransaction.cs
- QueryStringParameter.cs
- CombinedTcpChannel.cs
- DataGridViewComboBoxEditingControl.cs
- ElementHostPropertyMap.cs
- SourceFilter.cs
- Iis7Helper.cs
- MappingException.cs
- TreeIterator.cs
- Renderer.cs
- BitmapDecoder.cs
- PageStatePersister.cs
- WindowsSlider.cs
- Int32RectValueSerializer.cs
- ComponentConverter.cs
- FileFormatException.cs
- FillBehavior.cs
- PageFunction.cs
- AxDesigner.cs
- Paragraph.cs
- NonVisualControlAttribute.cs
- PerfCounters.cs
- MatrixStack.cs
- SimpleApplicationHost.cs
- SinglePhaseEnlistment.cs
- BulletedListDesigner.cs
- IgnoreFlushAndCloseStream.cs
- Popup.cs
- FormsAuthenticationUser.cs
- XmlILCommand.cs
- SaveFileDialog.cs
- ComponentEditorForm.cs
- MostlySingletonList.cs
- XmlILOptimizerVisitor.cs
- TreeNodeCollection.cs
- CacheVirtualItemsEvent.cs
- DataGridRow.cs
- BindingSource.cs
- RecordsAffectedEventArgs.cs
- MaskedTextBoxTextEditor.cs
- TargetInvocationException.cs
- DragDropHelper.cs
- EntityKeyElement.cs
- SQLByte.cs
- ReadOnlyTernaryTree.cs
- DataGridViewColumnHeaderCell.cs
- MD5CryptoServiceProvider.cs
- DataBinding.cs
- UrlMappingsModule.cs
- DependencyPropertyValueSerializer.cs
- XmlDictionaryString.cs
- PackWebResponse.cs
- EntityContainerRelationshipSet.cs
- DataObject.cs
- DesignSurfaceServiceContainer.cs
- SmiGettersStream.cs
- SmtpDigestAuthenticationModule.cs
- AspNetSynchronizationContext.cs
- SecurityUtils.cs
- SqlHelper.cs
- ExpressionNormalizer.cs
- Atom10FormatterFactory.cs
- CollectionsUtil.cs
- DbMetaDataCollectionNames.cs
- BasePropertyDescriptor.cs
- BookmarkScopeHandle.cs
- _AutoWebProxyScriptWrapper.cs
- PageEventArgs.cs
- DeobfuscatingStream.cs
- XmlSchemaIdentityConstraint.cs
- XPathChildIterator.cs
- IOException.cs
- Typeface.cs
- EntityClientCacheKey.cs
- RequestBringIntoViewEventArgs.cs
- TrustManagerMoreInformation.cs