private static ODataAtomServiceDocumentSerializer CreateAtomOutputContext(MemoryStream memoryStream, IODataUrlResolver resolver = null) { var model = new EdmModel(); var odataMessageWriterSettings = new ODataMessageWriterSettings(); var atomOutputContext = new ODataAtomOutputContext(ODataFormat.Atom, memoryStream, Encoding.UTF8, odataMessageWriterSettings, false /*writingResponse*/, true /*sync*/, model, resolver); return(new ODataAtomServiceDocumentSerializer(atomOutputContext)); }
private void VerifyComplexTypeRoundtrip(ODataComplexValue value, string typeName) { var typeReference = new EdmComplexTypeReference((IEdmComplexType)model.FindType(typeName), true); MemoryStream stream = new MemoryStream(); using (ODataAtomOutputContext outputContext = new ODataAtomOutputContext( ODataFormat.Atom, new NonDisposingStream(stream), Encoding.UTF8, new ODataMessageWriterSettings() { Version = ODataVersion.V4 }, /*writingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { ODataAtomPropertyAndValueSerializer serializer = new ODataAtomPropertyAndValueSerializer(outputContext); serializer.XmlWriter.WriteStartElement("ValueElement"); serializer.WriteComplexValue( value, typeReference, /*isOpenPropertyType*/ false, /*isWritingCollection*/ false, /*beforeValueAction*/ null, /*afterValueAction*/ null, new DuplicatePropertyNamesChecker(false, false), /*collectionValidator*/ null, /*projectedProperties*/ null); serializer.XmlWriter.WriteEndElement(); } stream.Position = 0; object actualValue; using (ODataAtomInputContext inputContext = new ODataAtomInputContext( ODataFormat.Atom, stream, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { ODataAtomPropertyAndValueDeserializer deserializer = new ODataAtomPropertyAndValueDeserializer(inputContext); deserializer.XmlReader.MoveToContent(); actualValue = deserializer.ReadNonEntityValue( typeReference, /*duplicatePropertyNamesChecker*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true); } TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value); }
private void VerifyTypeCollectionRoundtrip(ODataCollectionValue value, string propertyName, ODataFeedAndEntrySerializationInfo info) { var properties = new[] { new ODataProperty { Name = propertyName, Value = value } }; var entry = new ODataEntry() { TypeName = "NS.Student", Properties = properties, SerializationInfo = info }; MemoryStream stream = new MemoryStream(); using (ODataAtomOutputContext outputContext = new ODataAtomOutputContext( ODataFormat.Atom, new NonDisposingStream(stream), Encoding.UTF8, new ODataMessageWriterSettings() { Version = ODataVersion.V4 }, /*writingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { outputContext.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); var atomWriter = new ODataAtomWriter(outputContext, /*entitySet*/ null, /*entityType*/ null, /*writingFeed*/ false); atomWriter.WriteStart(entry); atomWriter.WriteEnd(); } stream.Position = 0; object actualValue = null; using (ODataAtomInputContext inputContext = new ODataAtomInputContext( ODataFormat.Atom, stream, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { var atomReader = new ODataAtomReader(inputContext, /*entitySet*/ null, /*entityType*/ null, /*writingFeed*/ false); while (atomReader.Read()) { if (atomReader.State == ODataReaderState.EntryEnd) { ODataEntry entryOut = atomReader.Item as ODataEntry; actualValue = entryOut.Properties.Single(p => p.Name == propertyName).ODataValue; } } } TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value); }
private string SerializeProperty(ODataProperty odataProperty) { MemoryStream outputStream = new MemoryStream(); ODataAtomOutputContext atomOutputContext = this.CreateAtomOutputContext(this.model, outputStream); var serializer = new ODataAtomPropertyAndValueSerializer(atomOutputContext); serializer.WriteTopLevelProperty(odataProperty); atomOutputContext.Flush(); outputStream.Position = 0; string result = new StreamReader(outputStream).ReadToEnd(); return(result); }
/// <summary> /// Creates an instance of the output context for the specified format. /// </summary> /// <param name="format">The format to create the context for.</param> /// <param name="message">The message to use.</param> /// <param name="encoding">The encoding to use.</param> /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param> /// <param name="writingResponse">true if writing a response message; otherwise false.</param> /// <param name="model">The model to use.</param> /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param> /// <returns>Task which represents the pending create operation.</returns> internal static Task <ODataOutputContext> CreateOutputContextAsync( ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, IEdmModel model, IODataUrlResolver urlResolver) { DebugUtils.CheckNoExternalCallers(); if (format == ODataFormat.Atom) { return(ODataAtomOutputContext.CreateAsync( format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } if (format == ODataFormat.VerboseJson) { return(ODataJsonOutputContext.CreateAsync( format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } //// Metadata output is only supported in synchronous operation. if (format == ODataFormat.Batch || format == ODataFormat.RawValue) { return(ODataRawOutputContext.CreateAsync( format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataOutputContext_CreateOutputContext_UnrecognizedFormat)); }
internal static Task <ODataOutputContext> CreateOutputContextAsync(ODataFormat format, ODataMessage message, Encoding encoding, ODataMessageWriterSettings messageWriterSettings, bool writingResponse, IEdmModel model, IODataUrlResolver urlResolver) { if (format == ODataFormat.Atom) { return(ODataAtomOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } if (format == ODataFormat.VerboseJson) { return(ODataJsonOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); } if ((format != ODataFormat.Batch) && (format != ODataFormat.RawValue)) { throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataOutputContext_CreateOutputContext_UnrecognizedFormat)); } return(ODataRawOutputContext.CreateAsync(format, message, encoding, messageWriterSettings, writingResponse, model, urlResolver)); }
private string SerializePropertyWithOwningType(EdmModel model, IEdmStructuredType owningType, ODataProperty odataProperty) { MemoryStream outputStream = new MemoryStream(); ODataAtomOutputContext atomOutputContext = this.CreateAtomOutputContext(model, outputStream); var serializer = new ODataAtomPropertyAndValueSerializer(atomOutputContext); serializer.WritePayloadStart(); serializer.WriteProperties( owningType, new[] { odataProperty }, /*isWritingCollection*/ false, null, null, new DuplicatePropertyNamesChecker(allowDuplicateProperties: true, isResponse: true), ProjectedPropertiesAnnotation.AllProjectedPropertiesInstance ); serializer.WritePayloadEnd(); atomOutputContext.Flush(); outputStream.Position = 0; string result = new StreamReader(outputStream).ReadToEnd(); return(result); }
/// <summary> /// Constructor. /// </summary> /// <param name="atomOutputContext">The output context to write to.</param> internal ODataAtomCollectionSerializer(ODataAtomOutputContext atomOutputContext) : base(atomOutputContext) { }
private void VerifyPrimitiveValueRoundtrips(object clrValue, string edmTypeName, ODataVersion version, string description) { IEdmModel model = new EdmModel(); IEdmPrimitiveTypeReference typeReference = new EdmPrimitiveTypeReference((IEdmPrimitiveType)model.FindType(edmTypeName), true); MemoryStream stream = new MemoryStream(); using (ODataAtomOutputContext outputContext = new ODataAtomOutputContext( ODataFormat.Atom, new NonDisposingStream(stream), Encoding.UTF8, new ODataMessageWriterSettings() { Version = version }, /*writingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { ODataAtomPropertyAndValueSerializer serializer = new ODataAtomPropertyAndValueSerializer(outputContext); serializer.XmlWriter.WriteStartElement("ValueElement"); serializer.WritePrimitiveValue( clrValue, /*collectionValidator*/ null, typeReference, /*serializationTypeNameAnnotation*/ null); serializer.XmlWriter.WriteEndElement(); } stream.Position = 0; object actualValue; using (ODataAtomInputContext inputContext = new ODataAtomInputContext( ODataFormat.Atom, stream, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { ODataAtomPropertyAndValueDeserializer deserializer = new ODataAtomPropertyAndValueDeserializer(inputContext); deserializer.XmlReader.MoveToContent(); actualValue = deserializer.ReadNonEntityValue( typeReference, /*duplicatePropertyNamesChecker*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true); } if (clrValue is byte[]) { ((byte[])actualValue).Should().Equal((byte[])clrValue, description); } else { actualValue.Should().Be(clrValue, description); } }
/// <summary> /// Constructor. /// </summary> /// <param name="atomOutputContext">The output context to write to.</param> internal ODataAtomCollectionSerializer(ODataAtomOutputContext atomOutputContext) : base(atomOutputContext) { DebugUtils.CheckNoExternalCallers(); }