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
- BasicHttpMessageSecurityElement.cs
- WebPartEditorApplyVerb.cs
- HtmlInputReset.cs
- FamilyCollection.cs
- wgx_render.cs
- XmlSerializerImportOptions.cs
- MessagingDescriptionAttribute.cs
- SmiEventSink_DeferedProcessing.cs
- DesignTimeParseData.cs
- TypeNameParser.cs
- DispatcherHooks.cs
- SafeBuffer.cs
- TriggerActionCollection.cs
- BmpBitmapEncoder.cs
- sqlcontext.cs
- ValueChangedEventManager.cs
- SchemaElement.cs
- PointAnimationUsingKeyFrames.cs
- DrawingState.cs
- GeneralTransform3DGroup.cs
- SqlCacheDependencyDatabaseCollection.cs
- SystemIPAddressInformation.cs
- DataStorage.cs
- RecommendedAsConfigurableAttribute.cs
- RtfControlWordInfo.cs
- ActivitiesCollection.cs
- ListItemCollection.cs
- sqlinternaltransaction.cs
- DictionaryBase.cs
- SHA512.cs
- GuidelineSet.cs
- StringCollectionEditor.cs
- ProfileGroupSettingsCollection.cs
- WebHttpBindingElement.cs
- HtmlInputReset.cs
- MLangCodePageEncoding.cs
- AuthenticationServiceManager.cs
- OptimalBreakSession.cs
- TypeHelpers.cs
- WriterOutput.cs
- ValueHandle.cs
- ResourceReferenceExpression.cs
- XPathCompileException.cs
- GifBitmapDecoder.cs
- Int64.cs
- UInt32Storage.cs
- _ChunkParse.cs
- ServicePointManagerElement.cs
- IssuedSecurityTokenProvider.cs
- XmlILStorageConverter.cs
- OrderByQueryOptionExpression.cs
- KeyboardEventArgs.cs
- UnitControl.cs
- TypeElement.cs
- Security.cs
- ProcessModule.cs
- ConfigurationSectionCollection.cs
- HiddenFieldPageStatePersister.cs
- TextBox.cs
- Brush.cs
- CatalogZone.cs
- SqlException.cs
- TextProperties.cs
- SpellerHighlightLayer.cs
- SingleAnimationUsingKeyFrames.cs
- SingleResultAttribute.cs
- HttpCacheVary.cs
- Menu.cs
- CustomErrorsSection.cs
- Error.cs
- _StreamFramer.cs
- HttpModulesSection.cs
- SqlInternalConnectionSmi.cs
- ComplexPropertyEntry.cs
- XmlDocumentType.cs
- SelectionEditingBehavior.cs
- XPathItem.cs
- ResetableIterator.cs
- GridViewEditEventArgs.cs
- Mapping.cs
- SQLConvert.cs
- ECDiffieHellman.cs
- DockPattern.cs
- TrustLevel.cs
- SystemSounds.cs
- InheritedPropertyChangedEventArgs.cs
- GetRecipientRequest.cs
- DoubleKeyFrameCollection.cs
- InputProcessorProfiles.cs
- RowUpdatedEventArgs.cs
- OleDbStruct.cs
- ConfigXmlSignificantWhitespace.cs
- ColumnMapCopier.cs
- DynamicMethod.cs
- XpsInterleavingPolicy.cs
- JsonWriter.cs
- AnnotationStore.cs
- SafeFindHandle.cs
- METAHEADER.cs
- _Events.cs