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
- UIElement3D.cs
- XmlSchemaSimpleTypeList.cs
- ToolStripSplitStackLayout.cs
- ConfigXmlText.cs
- PlanCompiler.cs
- AddDataControlFieldDialog.cs
- CustomSignedXml.cs
- SharedStatics.cs
- HwndAppCommandInputProvider.cs
- SimpleType.cs
- ServiceInfoCollection.cs
- SqlGatherProducedAliases.cs
- ActivityTrace.cs
- _SSPISessionCache.cs
- BoundsDrawingContextWalker.cs
- HttpProfileBase.cs
- Vector3DConverter.cs
- Helper.cs
- AsnEncodedData.cs
- XomlCompilerParameters.cs
- ViewStateException.cs
- OleDbParameterCollection.cs
- TaskFileService.cs
- PageContentAsyncResult.cs
- XamlSerializationHelper.cs
- DataChangedEventManager.cs
- CustomErrorCollection.cs
- SignalGate.cs
- MaskedTextProvider.cs
- IfAction.cs
- PreloadedPackages.cs
- StatusBarDesigner.cs
- DateTime.cs
- ObjectDataSourceStatusEventArgs.cs
- FormViewModeEventArgs.cs
- MinMaxParagraphWidth.cs
- BuildManagerHost.cs
- X509Chain.cs
- SelectionItemProviderWrapper.cs
- StorageFunctionMapping.cs
- BuildResultCache.cs
- UnsafeNativeMethods.cs
- TypeDescriptionProvider.cs
- PathStreamGeometryContext.cs
- FlowDocumentFormatter.cs
- GACMembershipCondition.cs
- TrustManagerMoreInformation.cs
- CalendarTable.cs
- SerializationException.cs
- InfocardClientCredentials.cs
- DropTarget.cs
- SiteMapNode.cs
- SqlVersion.cs
- bidPrivateBase.cs
- SafeLocalMemHandle.cs
- IndexerNameAttribute.cs
- ObjectContextServiceProvider.cs
- DataSetViewSchema.cs
- X509Chain.cs
- XmlSchemaImport.cs
- OracleTimeSpan.cs
- COM2IVsPerPropertyBrowsingHandler.cs
- MailMessageEventArgs.cs
- StringConcat.cs
- PageBuildProvider.cs
- XmlHelper.cs
- SQLByteStorage.cs
- CasesDictionary.cs
- VariableExpressionConverter.cs
- TrustLevel.cs
- DivideByZeroException.cs
- HttpFormatExtensions.cs
- OleDbConnectionFactory.cs
- XPathArrayIterator.cs
- selecteditemcollection.cs
- ObjectDataSourceFilteringEventArgs.cs
- Configuration.cs
- ImpersonationContext.cs
- GorillaCodec.cs
- OperationAbortedException.cs
- CodeCompileUnit.cs
- TextOnlyOutput.cs
- XmlSchemaAny.cs
- ListContractAdapter.cs
- DynamicField.cs
- nulltextcontainer.cs
- InternalsVisibleToAttribute.cs
- CommandLineParser.cs
- PrintPageEvent.cs
- RemoteWebConfigurationHost.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- ProviderBase.cs
- SeparatorAutomationPeer.cs
- QilXmlReader.cs
- DriveNotFoundException.cs
- RotationValidation.cs
- MemberExpression.cs
- DataGridColumnCollection.cs
- XmlFormatExtensionPrefixAttribute.cs
- RecordBuilder.cs