Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / Json / JsonObjectDataContract.cs / 1305376 / JsonObjectDataContract.cs
//---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Runtime.Serialization.Json { using System.Xml; using System.ServiceModel; using System.Runtime.Serialization; using System.Globalization; class JsonObjectDataContract : JsonDataContract { public JsonObjectDataContract(DataContract traditionalDataContract) : base(traditionalDataContract) { } public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) { object obj; string contentMode = jsonReader.GetAttribute(JsonGlobals.typeString); switch (contentMode) { case JsonGlobals.nullString: jsonReader.Skip(); obj = null; break; case JsonGlobals.booleanString: obj = jsonReader.ReadElementContentAsBoolean(); break; case JsonGlobals.stringString: case null: obj = jsonReader.ReadElementContentAsString(); break; case JsonGlobals.numberString: obj = ParseJsonNumber(jsonReader.ReadElementContentAsString()); break; case JsonGlobals.objectString: jsonReader.Skip(); obj = new object(); break; case JsonGlobals.arrayString: // Read as object array return DataContractJsonSerializer.ReadJsonValue(DataContract.GetDataContract(Globals.TypeOfObjectArray), jsonReader, context); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.JsonUnexpectedAttributeValue, contentMode))); } if (context != null) { context.AddNewObject(obj); } return obj; } public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) { jsonWriter.WriteAttributeString(null, JsonGlobals.typeString, null, JsonGlobals.objectString); } internal static object ParseJsonNumber(string value, out TypeCode objectTypeCode) { if (value == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.XmlInvalidConversion, value, Globals.TypeOfInt))); } if (value.IndexOfAny(JsonGlobals.floatingPointCharacters) == -1) { int intValue; if (Int32.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out intValue)) { objectTypeCode = TypeCode.Int32; return intValue; } long longValue; if (Int64.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out longValue)) { objectTypeCode = TypeCode.Int64; return longValue; } } decimal decimalValue; if (Decimal.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out decimalValue)) { objectTypeCode = TypeCode.Decimal; //check for decimal underflow if (decimalValue == Decimal.Zero) { double doubleValue = XmlConverter.ToDouble(value); if (doubleValue != 0.0) { objectTypeCode = TypeCode.Double; return doubleValue; } } return decimalValue; } objectTypeCode = TypeCode.Double; return XmlConverter.ToDouble(value); } static object ParseJsonNumber(string value) { TypeCode unusedTypeCode; return ParseJsonNumber(value, out unusedTypeCode); } } } // 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.ServiceModel; using System.Runtime.Serialization; using System.Globalization; class JsonObjectDataContract : JsonDataContract { public JsonObjectDataContract(DataContract traditionalDataContract) : base(traditionalDataContract) { } public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) { object obj; string contentMode = jsonReader.GetAttribute(JsonGlobals.typeString); switch (contentMode) { case JsonGlobals.nullString: jsonReader.Skip(); obj = null; break; case JsonGlobals.booleanString: obj = jsonReader.ReadElementContentAsBoolean(); break; case JsonGlobals.stringString: case null: obj = jsonReader.ReadElementContentAsString(); break; case JsonGlobals.numberString: obj = ParseJsonNumber(jsonReader.ReadElementContentAsString()); break; case JsonGlobals.objectString: jsonReader.Skip(); obj = new object(); break; case JsonGlobals.arrayString: // Read as object array return DataContractJsonSerializer.ReadJsonValue(DataContract.GetDataContract(Globals.TypeOfObjectArray), jsonReader, context); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.JsonUnexpectedAttributeValue, contentMode))); } if (context != null) { context.AddNewObject(obj); } return obj; } public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) { jsonWriter.WriteAttributeString(null, JsonGlobals.typeString, null, JsonGlobals.objectString); } internal static object ParseJsonNumber(string value, out TypeCode objectTypeCode) { if (value == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.XmlInvalidConversion, value, Globals.TypeOfInt))); } if (value.IndexOfAny(JsonGlobals.floatingPointCharacters) == -1) { int intValue; if (Int32.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out intValue)) { objectTypeCode = TypeCode.Int32; return intValue; } long longValue; if (Int64.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out longValue)) { objectTypeCode = TypeCode.Int64; return longValue; } } decimal decimalValue; if (Decimal.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out decimalValue)) { objectTypeCode = TypeCode.Decimal; //check for decimal underflow if (decimalValue == Decimal.Zero) { double doubleValue = XmlConverter.ToDouble(value); if (doubleValue != 0.0) { objectTypeCode = TypeCode.Double; return doubleValue; } } return decimalValue; } objectTypeCode = TypeCode.Double; return XmlConverter.ToDouble(value); } static object ParseJsonNumber(string value) { TypeCode unusedTypeCode; return ParseJsonNumber(value, out unusedTypeCode); } } } // 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
- ServiceEndpointCollection.cs
- SelectionManager.cs
- PersianCalendar.cs
- XmlAttribute.cs
- SchemaCompiler.cs
- AspProxy.cs
- PropertyPathConverter.cs
- PathData.cs
- Range.cs
- ConfigurationStrings.cs
- TypeUsageBuilder.cs
- MenuItem.cs
- HandlerBase.cs
- PopupRootAutomationPeer.cs
- MultipleViewPatternIdentifiers.cs
- XmlAutoDetectWriter.cs
- String.cs
- Encoder.cs
- InputLanguage.cs
- Constants.cs
- XsdBuildProvider.cs
- WebReferenceOptions.cs
- CacheAxisQuery.cs
- RegexFCD.cs
- DocumentViewer.cs
- TextTreeTextElementNode.cs
- ConfigXmlDocument.cs
- HtmlControlAdapter.cs
- SecurityCredentialsManager.cs
- SelectedCellsChangedEventArgs.cs
- DllNotFoundException.cs
- BinaryObjectReader.cs
- PropertyGridEditorPart.cs
- TypeHelpers.cs
- LineUtil.cs
- KeyNotFoundException.cs
- Light.cs
- PrintSchema.cs
- RsaEndpointIdentity.cs
- EventBuilder.cs
- ViewStateAttachedPropertyFeature.cs
- DelayedRegex.cs
- TextElementCollectionHelper.cs
- HtmlInputFile.cs
- ToolStripDropDown.cs
- ToolStripCollectionEditor.cs
- MarginCollapsingState.cs
- UnsafeNativeMethods.cs
- ObjectStorage.cs
- UrlRoutingHandler.cs
- HMACSHA256.cs
- StorageScalarPropertyMapping.cs
- FontWeightConverter.cs
- DocumentPropertiesDialog.cs
- ReadOnlyDataSource.cs
- StringAnimationUsingKeyFrames.cs
- UpdateManifestForBrowserApplication.cs
- ListViewHitTestInfo.cs
- SurrogateSelector.cs
- PageContentAsyncResult.cs
- SspiWrapper.cs
- CodeCommentStatement.cs
- ContentTextAutomationPeer.cs
- DiscoveryOperationContext.cs
- Stacktrace.cs
- SHA384Managed.cs
- GZipStream.cs
- ReachFixedDocumentSerializer.cs
- HandleCollector.cs
- PolicyException.cs
- SByteConverter.cs
- PropertyInfoSet.cs
- SimpleRecyclingCache.cs
- controlskin.cs
- PrintingPermission.cs
- DoubleLinkListEnumerator.cs
- versioninfo.cs
- SignedXml.cs
- DocumentApplicationJournalEntry.cs
- Instrumentation.cs
- InstanceCreationEditor.cs
- EntityContainerAssociationSet.cs
- ControlBuilderAttribute.cs
- RecordConverter.cs
- XmlWhitespace.cs
- Localizer.cs
- AvTrace.cs
- EncryptedXml.cs
- StrokeNodeOperations.cs
- MemoryStream.cs
- WebPartTransformer.cs
- ArraySortHelper.cs
- EntityDataSourceDataSelectionPanel.cs
- HtmlInputHidden.cs
- DispatcherHookEventArgs.cs
- VectorValueSerializer.cs
- XmlSerializationWriter.cs
- columnmapfactory.cs
- ObsoleteAttribute.cs
- StringValidatorAttribute.cs