Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / Runtime / Serialization / Json / JsonXmlDataContract.cs / 1 / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Journaling.cs
- InfoCardTraceRecord.cs
- RecognizedWordUnit.cs
- Maps.cs
- XamlReaderHelper.cs
- SendActivityDesigner.cs
- DataGridViewRowCancelEventArgs.cs
- BatchParser.cs
- DeflateEmulationStream.cs
- ClientTargetSection.cs
- Decorator.cs
- ContractAdapter.cs
- ProxyFragment.cs
- XmlCharCheckingReader.cs
- HtmlWindow.cs
- ProtocolsConfigurationHandler.cs
- PackUriHelper.cs
- StylusCollection.cs
- TransformGroup.cs
- DrawingAttributeSerializer.cs
- XmlCollation.cs
- UriParserTemplates.cs
- FormViewUpdatedEventArgs.cs
- ExpressionPrinter.cs
- RootNamespaceAttribute.cs
- HostElement.cs
- RunInstallerAttribute.cs
- CurrencyWrapper.cs
- DesignerLoader.cs
- Native.cs
- OleDbRowUpdatedEvent.cs
- WorkItem.cs
- BitmapEffectInputData.cs
- ScriptComponentDescriptor.cs
- AsymmetricKeyExchangeDeformatter.cs
- ToolboxCategory.cs
- BuiltInPermissionSets.cs
- ToolStrip.cs
- FamilyCollection.cs
- FillErrorEventArgs.cs
- DefaultTraceListener.cs
- XmlDocument.cs
- RadioButtonPopupAdapter.cs
- SafeHandles.cs
- RelatedCurrencyManager.cs
- WinEventHandler.cs
- ListViewSelectEventArgs.cs
- ConfigXmlCDataSection.cs
- SecurityException.cs
- InputLangChangeRequestEvent.cs
- SizeAnimationBase.cs
- SizeFConverter.cs
- ListDictionary.cs
- Window.cs
- EntityClassGenerator.cs
- ServiceNameElementCollection.cs
- IntranetCredentialPolicy.cs
- ExceptionUtil.cs
- DbProviderConfigurationHandler.cs
- TextBreakpoint.cs
- SecureStringHasher.cs
- MaskedTextBox.cs
- AlignmentXValidation.cs
- NullableFloatSumAggregationOperator.cs
- DecimalAnimation.cs
- TlsSspiNegotiation.cs
- SafeRightsManagementSessionHandle.cs
- SafeBuffer.cs
- XmlSignificantWhitespace.cs
- HebrewCalendar.cs
- PageScaling.cs
- DataTemplate.cs
- Block.cs
- WorkflowHostingResponseContext.cs
- ActivityWithResultConverter.cs
- StreamReader.cs
- TemplateField.cs
- List.cs
- WindowsServiceCredential.cs
- TraceSection.cs
- RewritingSimplifier.cs
- DataIdProcessor.cs
- ConnectionProviderAttribute.cs
- DataGridViewRowsAddedEventArgs.cs
- AsymmetricSecurityProtocol.cs
- HttpResponseWrapper.cs
- CustomAttributeBuilder.cs
- Oid.cs
- XmlSchemaSimpleType.cs
- PerfCounterSection.cs
- SqlServices.cs
- RIPEMD160.cs
- FrameworkElement.cs
- ProfilePropertySettingsCollection.cs
- XPathSingletonIterator.cs
- SubMenuStyleCollection.cs
- FontDialog.cs
- ShapeTypeface.cs
- FactoryGenerator.cs
- SignedXml.cs