private void VerifyTimeOfDayValueReader(string payload, string edmTypeName, object expectedResult) { IEdmModel model = new EdmModel(); IEdmPrimitiveTypeReference typeReference = new EdmPrimitiveTypeReference((IEdmPrimitiveType)model.FindType(edmTypeName), true); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)); object actualValue; using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, JsonLightUtils.JsonLightStreamingMediaType, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*duplicatePropertyNamesChecker*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevelPropertyValue*/ true, /*insideComplexValue*/ false, /*propertyName*/ null); } actualValue.Should().Be(expectedResult, "payload ->{0}<- for type '{1}'", payload, edmTypeName); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> protected ODataJsonLightDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { Debug.Assert(jsonLightInputContext != null, "jsonLightInputContext != null"); this.jsonLightInputContext = jsonLightInputContext; }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageStream">The message stream to read from for payload kind detection.</param> /// <param name="message">The message being read.</param> /// <param name="readingResponse">true if reading a response message; otherwise false.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindImplementationAsync( Stream messageStream, ODataMessage message, bool readingResponse, ODataPayloadKindDetectionInfo detectionInfo) { ODataJsonLightInputContext jsonLightInputContext = new ODataJsonLightInputContext( this, messageStream, detectionInfo.ContentType, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings, ODataVersion.V4, // NOTE: we don't rely on the version for payload kind detection; taking the latest. readingResponse, /*synchronous*/ false, detectionInfo.Model, /*urlResolver*/ null, /*payloadKindDetectionState*/ null); return(jsonLightInputContext.DetectPayloadKindAsync(detectionInfo) .FollowAlwaysWith(t => { jsonLightInputContext.Dispose(); })); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The input to read the payload from.</param> /// <param name="operation">The operation import whose parameters are being read.</param> internal ODataJsonLightParameterReader(ODataJsonLightInputContext jsonLightInputContext, IEdmOperation operation) : base(jsonLightInputContext, operation) { Debug.Assert(jsonLightInputContext != null, "jsonLightInputContext != null"); Debug.Assert(jsonLightInputContext.ReadingResponse == false, "jsonLightInputContext.ReadingResponse == false"); Debug.Assert(operation != null, "operationImport != null"); this.jsonLightInputContext = jsonLightInputContext; this.jsonLightParameterDeserializer = new ODataJsonLightParameterDeserializer(this, jsonLightInputContext); Debug.Assert(this.jsonLightInputContext.Model.IsUserModel(), "this.jsonLightInputContext.Model.IsUserModel()"); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The input to read the payload from.</param> /// <param name="expectedItemTypeReference">The expected type for the items in the collection.</param> /// <param name="listener">If not null, the reader will notify the implementer of the interface of relevant state changes in the reader.</param> internal ODataJsonLightCollectionReader( ODataJsonLightInputContext jsonLightInputContext, IEdmTypeReference expectedItemTypeReference, IODataReaderWriterListener listener) : base(jsonLightInputContext, expectedItemTypeReference, listener) { Debug.Assert(jsonLightInputContext != null, "jsonLightInputContext != null"); this.jsonLightInputContext = jsonLightInputContext; this.jsonLightCollectionDeserializer = new ODataJsonLightCollectionDeserializer(jsonLightInputContext); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The input to read the payload from.</param> /// <param name="navigationSource">The navigation source we are going to read entities for.</param> /// <param name="expectedEntityType">The expected entity type for the entry to be read (in case of entry reader) or entries in the feed to be read (in case of feed reader).</param> /// <param name="readingFeed">true if the reader is created for reading a feed; false when it is created for reading an entry.</param> /// <param name="readingParameter">true if the reader is created for reading a parameter; false otherwise.</param> /// <param name="listener">If not null, the Json reader will notify the implementer of the interface of relevant state changes in the Json reader.</param> internal ODataJsonLightReader( ODataJsonLightInputContext jsonLightInputContext, IEdmNavigationSource navigationSource, IEdmEntityType expectedEntityType, bool readingFeed, bool readingParameter = false, IODataReaderWriterListener listener = null) : base(jsonLightInputContext, readingFeed, listener) { Debug.Assert(jsonLightInputContext != null, "jsonLightInputContext != null"); Debug.Assert( expectedEntityType == null || jsonLightInputContext.Model.IsUserModel(), "If the expected type is specified we need model as well. We should have verified that by now."); this.jsonLightInputContext = jsonLightInputContext; this.jsonLightEntryAndFeedDeserializer = new ODataJsonLightEntryAndFeedDeserializer(jsonLightInputContext); this.readingParameter = readingParameter; this.topLevelScope = new JsonLightTopLevelScope(navigationSource, expectedEntityType); this.EnterScope(this.topLevelScope); }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageStream">The message stream to read from for payload kind detection.</param> /// <param name="readingResponse">true if reading a response message; otherwise false.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private IEnumerable <ODataPayloadKind> DetectPayloadKindImplementation( Stream messageStream, bool readingResponse, ODataPayloadKindDetectionInfo detectionInfo) { using (ODataJsonLightInputContext jsonLightInputContext = new ODataJsonLightInputContext( this, messageStream, detectionInfo.ContentType, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings, ODataVersion.V4, // NOTE: we don't rely on the version for payload kind detection; taking the latest. readingResponse, /*synchronous*/ true, detectionInfo.Model, /*urlResolver*/ null, /*payloadKindDetectionState*/ null)) { return(jsonLightInputContext.DetectPayloadKind(detectionInfo)); } }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightCollectionDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { this.duplicatePropertyNamesChecker = this.CreateDuplicatePropertyNamesChecker(); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightErrorDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightServiceDocumentDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightEntityReferenceLinkDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightPropertyAndValueDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightPayloadKindDetectionDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { }
/// <summary> /// Constructor. /// </summary> /// <param name="parameterReader">The JSON Light parameter reader.</param> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightParameterDeserializer(ODataJsonLightParameterReader parameterReader, ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { this.parameterReader = parameterReader; }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageStream">The message stream to read from for payload kind detection.</param> /// <param name="readingResponse">true if reading a response message; otherwise false.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindImplementationAsync( Stream messageStream, bool readingResponse, ODataPayloadKindDetectionInfo detectionInfo) { ODataJsonLightInputContext jsonLightInputContext = new ODataJsonLightInputContext( this, messageStream, detectionInfo.ContentType, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings, readingResponse, /*synchronous*/ false, detectionInfo.Model, /*urlResolver*/ null); return jsonLightInputContext.DetectPayloadKindAsync(detectionInfo) .FollowAlwaysWith(t => { jsonLightInputContext.Dispose(); }); }
private void VerifyNonPrimitiveTypeRoundtrip(object value, string propertyName) { var properties = new[] { new ODataProperty { Name = propertyName, Value = value } }; var entry = new ODataEntry() { TypeName = "NS.Student", Properties = properties }; ODataMessageWriterSettings settings = new ODataMessageWriterSettings { Version = ODataVersion.V4 }; MemoryStream stream = new MemoryStream(); using (ODataJsonLightOutputContext outputContext = new ODataJsonLightOutputContext( ODataFormat.Json, new NonDisposingStream(stream), new ODataMediaType("application", "json"), Encoding.UTF8, settings, /*writingResponse*/ false, /*synchronous*/ true, model, /*urlResolver*/ null)) { var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false); jsonLightWriter.WriteStart(entry); jsonLightWriter.WriteEnd(); } stream.Position = 0; object actualValue = null; using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, JsonLightUtils.JsonLightStreamingMediaType, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ false, /*synchronous*/ true, model, /*urlResolver*/ null)) { var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.EntryEnd) { ODataEntry entryOut = jsonLightReader.Item as ODataEntry; actualValue = entryOut.Properties.Single(p => p.Name == propertyName).ODataValue; } } } TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value as ODataValue); }
private ODataNavigationLink ReadSingletonNavigationLink(string payload) { MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)); ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.UTF8, settings, /*readingResponse*/ true, /*synchronous*/ true, this.userModel, /*urlResolver*/ null)) { var jsonLightReader = new ODataJsonLightReader(inputContext, singleton, webType, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.NavigationLinkEnd) { ODataNavigationLink navigationLink = jsonLightReader.Item as ODataNavigationLink; return navigationLink; } } } return null; }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The JsonLight input context to read from.</param> internal ODataJsonLightEntryAndFeedDeserializer(ODataJsonLightInputContext jsonLightInputContext) : base(jsonLightInputContext) { }
private object WriteThenReadValue(object clrValue, IEdmTypeReference typeReference, ODataVersion version, bool isIeee754Compatible) { MemoryStream stream = new MemoryStream(); ODataMessageWriterSettings settings = new ODataMessageWriterSettings { Version = version }; settings.SetServiceDocumentUri(new Uri("http://odata.org/test/")); ODataMediaType mediaType = isIeee754Compatible ? new ODataMediaType("application", "json", new KeyValuePair<string, string>("IEEE754Compatible", "true")) : new ODataMediaType("application", "json"); using (ODataJsonLightOutputContext outputContext = new ODataJsonLightOutputContext( ODataFormat.Json, new NonDisposingStream(stream), mediaType, Encoding.UTF8, settings, /*writingResponse*/ true, /*synchronous*/ true, this.model, /*urlResolver*/ null)) { ODataJsonLightValueSerializer serializer = new ODataJsonLightValueSerializer(outputContext); serializer.WritePrimitiveValue(clrValue, typeReference); } stream.Position = 0; object actualValue; using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, mediaType, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, this.model, /*urlResolver*/ null)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*duplicatePropertyNamesChecker*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevel*/ true, /*insideComplexValue*/ false, /*propertyName*/ null); } return actualValue; }
/// <summary> /// Converts the given string <paramref name="value"/> to an ODataComplexValue or ODataCollectionValue and returns it. /// Tries in both JSON light and Verbose JSON. /// </summary> /// <remarks>Does not handle primitive values.</remarks> /// <param name="value">Value to be deserialized.</param> /// <param name="model">Model to use for verification.</param> /// <param name="typeReference">Expected type reference from deserialization. If null, verification will be skipped.</param> /// <returns>An ODataComplexValue or ODataCollectionValue that results from the deserialization of <paramref name="value"/>.</returns> internal static object ConvertFromComplexOrCollectionValue(string value, IEdmModel model, IEdmTypeReference typeReference) { ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); using (StringReader reader = new StringReader(value)) { using (ODataJsonLightInputContext context = new ODataJsonLightInputContext( ODataFormat.Json, reader, new ODataMediaType(MimeConstants.MimeApplicationType, MimeConstants.MimeJsonSubType), settings, false /*readingResponse*/, true /*synchronous*/, model, null /*urlResolver*/)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(context); // TODO: The way JSON array literals look in the URI is different that response payload with an array in it. // The fact that we have to manually setup the underlying reader shows this different in the protocol. // There is a discussion on if we should change this or not. deserializer.JsonReader.Read(); // Move to first thing object rawResult = deserializer.ReadNonEntityValue( null /*payloadTypeName*/, typeReference, null /*DuplicatePropertyNameChecker*/, null /*CollectionWithoutExpectedTypeValidator*/, true /*validateNullValue*/, false /*isTopLevelPropertyValue*/, false /*insideComplexValue*/, null /*propertyName*/); deserializer.ReadPayloadEnd(false); Debug.Assert(rawResult is ODataComplexValue || rawResult is ODataCollectionValue, "rawResult is ODataComplexValue || rawResult is ODataCollectionValue"); return rawResult; } } }
private IEnumerable<Tuple<ODataItem, ODataDeltaReaderState>> ReadItem(string payload, IEdmModel model = null, IEdmNavigationSource navigationSource = null, IEdmEntityType entityType = null, bool odataSimplified = false) { MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)); ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); settings.ShouldIncludeAnnotation = s => true; settings.ODataSimplified = odataSimplified; using (var inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.UTF8, settings, /*readingResponse*/ true, /*synchronous*/ true, model ?? new EdmModel(), /*urlResolver*/ null)) { var jsonLightReader = new ODataJsonLightDeltaReader(inputContext, navigationSource, entityType); while (jsonLightReader.Read()) { yield return new Tuple<ODataItem, ODataDeltaReaderState>(jsonLightReader.Item, jsonLightReader.State); } } }
private ODataJsonLightServiceDocumentDeserializer CreateODataJsonServiceDocumentDeserializer(MemoryStream stream, IODataUrlResolver urlResolver = null) { ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext(ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.UTF8, settings, true /*readingResponse*/, true /*sync*/, new EdmModel() /*edmModel*/, urlResolver); return new ODataJsonLightServiceDocumentDeserializer(inputContext); }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageStream">The message stream to read from for payload kind detection.</param> /// <param name="readingResponse">true if reading a response message; otherwise false.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private IEnumerable<ODataPayloadKind> DetectPayloadKindImplementation( Stream messageStream, bool readingResponse, ODataPayloadKindDetectionInfo detectionInfo) { using (ODataJsonLightInputContext jsonLightInputContext = new ODataJsonLightInputContext( this, messageStream, detectionInfo.ContentType, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings, readingResponse, /*synchronous*/ true, detectionInfo.Model, /*urlResolver*/ null)) { return jsonLightInputContext.DetectPayloadKind(detectionInfo); } }