//// Raw value is not supported by JsonLight.

        /// <summary>
        /// Creates an <see cref="ODataWriter" /> to write a feed.
        /// </summary>
        /// <param name="entitySet">The entity set we are going to write entities for.</param>
        /// <param name="entityType">The entity type for the entries in the feed to be written (or null if the entity set base type should be used).</param>
        /// <returns>The created writer.</returns>
        private ODataWriter CreateODataFeedWriterImplementation(IEdmEntitySetBase entitySet, IEdmEntityType entityType)
        {
            ODataJsonLightWriter odataJsonWriter = new ODataJsonLightWriter(this, entitySet, entityType, /*writingFeed*/ true);

            this.outputInStreamErrorListener = odataJsonWriter;
            return(odataJsonWriter);
        }
        /// <summary>
        /// Creates an <see cref="ODataWriter" /> to write an entry.
        /// </summary>
        /// <param name="navigationSource">The navigation source we are going to write entities for.</param>
        /// <param name="entityType">The entity type for the entries in the feed to be written (or null if the entity set base type should be used).</param>
        /// <returns>The created writer.</returns>
        private ODataWriter CreateODataEntryWriterImplementation(IEdmNavigationSource navigationSource, IEdmEntityType entityType)
        {
            ODataJsonLightWriter odataJsonWriter = new ODataJsonLightWriter(this, navigationSource, entityType, /*writingFeed*/ false);

            this.outputInStreamErrorListener = odataJsonWriter;
            return(odataJsonWriter);
        }
 private static void WriteEntryAndValidatePayload(ODataEntry entry, MemoryStream stream, ODataJsonLightWriter writer, string expectedPayload)
 {
     writer.WriteStart(entry);
     writer.WriteEnd();
     writer.Flush();
     stream.Seek(0, SeekOrigin.Begin);
     string payload = (new StreamReader(stream)).ReadToEnd();
     payload.Should().Be(expectedPayload);
 }
 private void WriteEntryAndValidatePayloadWithModel(ODataEntry entry, string expectedPayload, bool writingResponse = true, bool setMetadataDocumentUri = true)
 {
     MemoryStream stream = new MemoryStream();
     ODataJsonLightOutputContext outputContext = CreateJsonLightOutputContext(stream, writingResponse, this.userModel, setMetadataDocumentUri ? this.serviceDocumentUri : null);
     entry.SerializationInfo = null;
     ODataJsonLightWriter writer = new ODataJsonLightWriter(outputContext, /*navigationSource*/ singleton, /*entityType*/ webType, /*writingFeed*/ false);
     WriteEntryAndValidatePayload(entry, stream, writer, expectedPayload);
 }
        private void WriteEntryAndValidatePayloadWithoutModel(ODataEntry entry, string expectedPayload, bool writingResponse = true, bool setMetadataDocumentUri = true)
        {
            MemoryStream stream = new MemoryStream();
            ODataJsonLightOutputContext outputContext = CreateJsonLightOutputContext(stream, writingResponse, this.userModel, setMetadataDocumentUri ? this.serviceDocumentUri : null);

            ODataFeedAndEntrySerializationInfo serializationInfo = new ODataFeedAndEntrySerializationInfo
            {
                NavigationSourceName = "MySingleton",
                NavigationSourceEntityTypeName = "NS.Web",
                NavigationSourceKind = EdmNavigationSourceKind.Singleton,
            };

            entry.SerializationInfo = serializationInfo;
            ODataJsonLightWriter writer = new ODataJsonLightWriter(outputContext, /*navigationSource*/ null, /*entityType*/ null, /*writingFeed*/ false);
            WriteEntryAndValidatePayload(entry, stream, writer, expectedPayload);
        }
        //// Raw value is not supported by JsonLight.

        /// <summary>
        /// Creates an <see cref="ODataWriter" /> to write a feed.
        /// </summary>
        /// <param name="entitySet">The entity set we are going to write entities for.</param>
        /// <param name="entityType">The entity type for the entries in the feed to be written (or null if the entity set base type should be used).</param>
        /// <returns>The created writer.</returns>
        private ODataWriter CreateODataFeedWriterImplementation(IEdmEntitySetBase entitySet, IEdmEntityType entityType)
        {
            ODataJsonLightWriter odataJsonWriter = new ODataJsonLightWriter(this, entitySet, entityType, /*writingFeed*/true);
            this.outputInStreamErrorListener = odataJsonWriter;
            return odataJsonWriter;
        }
 /// <summary>
 /// Creates an <see cref="ODataWriter" /> to write an entry.
 /// </summary>
 /// <param name="navigationSource">The navigation source we are going to write entities for.</param>
 /// <param name="entityType">The entity type for the entries in the feed to be written (or null if the entity set base type should be used).</param>
 /// <returns>The created writer.</returns>
 private ODataWriter CreateODataEntryWriterImplementation(IEdmNavigationSource navigationSource, IEdmEntityType entityType)
 {
     ODataJsonLightWriter odataJsonWriter = new ODataJsonLightWriter(this, navigationSource, entityType, /*writingFeed*/false);
     this.outputInStreamErrorListener = odataJsonWriter;
     return odataJsonWriter;
 }
 private void WriteNoMetadataResponseWithoutModelAndValidatePayload(ODataItem[] nestedItemToWrite, string expectedPayload, bool setMetadataDocumentUri = true)
 {
     // without model, write response
     // (when entityType==null: nonemetadata or (serviceDocumentUri==null && writingResponse==false) -> avoid the below exception. pls refer to ODataContextUriBuilder method)
     // "When writing a JSON response, a user model must be specified and the entity set and entity type must be passed to the ODataMessageWriter.CreateEntryWriter method or the ODataFeedAndEntrySerializationInfo must be set on the ODataEntry or ODataFeed that is being writen."
     // so here use nonemetadata:
     ODataMediaType mediaType = new ODataMediaType("application", "json", new KeyValuePair<string, string>(MimeConstants.MimeMetadataParameterName, MimeConstants.MimeMetadataParameterValueNone));
     var stream = new MemoryStream();
     var outputContext = CreateJsonLightOutputContext(stream, mediaType, true, null, setMetadataDocumentUri ? this.serviceDocumentUri : null);
     var writer = new ODataJsonLightWriter(outputContext, null, null, nestedItemToWrite[0] is ODataFeed);
     WriteNestedItems(nestedItemToWrite, writer);
     ValidateWrittenPayload(stream, writer, expectedPayload);
 }
        private static void WriteNestedItems(ODataItem[] nestedItemsToWrite, ODataJsonLightWriter writer)
        {
            foreach (ODataItem itemToWrite in nestedItemsToWrite)
            {
                ODataFeed feedToWrite = itemToWrite as ODataFeed;
                if (feedToWrite != null)
                {
                    writer.WriteStart(feedToWrite);
                }
                else
                {
                    ODataEntry entryToWrite = itemToWrite as ODataEntry;
                    if (entryToWrite != null)
                    {
                        writer.WriteStart(entryToWrite);
                    }
                    else
                    {
                        writer.WriteStart((ODataNavigationLink)itemToWrite);
                    }
                }
            }

            for (int count = 0; count < nestedItemsToWrite.Length; count++)
            {
                writer.WriteEnd();
            }
        }
 private void WriteResponseWithModelAndValidatePayload(ODataMediaType mediaType, ODataItem[] nestedItemToWrite, string expectedPayload, bool setMetadataDocumentUri = true)
 {
     // with model
     // write response
     var stream = new MemoryStream();
     var outputContext = CreateJsonLightOutputContext(stream, mediaType, true, this.userModel, setMetadataDocumentUri ? this.serviceDocumentUri : null);
     var writer = new ODataJsonLightWriter(outputContext, this.entitySet, this.entityType, nestedItemToWrite[0] is ODataFeed);
     WriteNestedItems(nestedItemToWrite, writer);
     ValidateWrittenPayload(stream, writer, expectedPayload);
 }
        private void WriteMinimalMetadataRequestWithoutModelAndValidatePayload(ODataItem[] nestedItemToWrite, string expectedPayload, bool setMetadataDocumentUri = true)
        {
            // without model, write request (JsonLightMetadataLevel.Create method will internally use MinimalMetadata for writing request)
            ODataMediaType mediaType = new ODataMediaType("application", "json", new KeyValuePair<string, string>(MimeConstants.MimeMetadataParameterName, MimeConstants.MimeMetadataParameterValueMinimal));

            // 1. CreateEntityContainerElementContextUri(): no entitySetName --> no context uri is output.
            // 2. but odata.type will be output because of no model. JsonMinimalMetadataTypeNameOracle.GetEntryTypeNameForWriting method: // We only write entity type names in Json Light if it's more derived (different) from the expected type name.
            var stream = new MemoryStream();
            var outputContext = CreateJsonLightOutputContext(stream, mediaType, false, null, setMetadataDocumentUri ? this.serviceDocumentUri : null);
            var writer = new ODataJsonLightWriter(outputContext, null, null, nestedItemToWrite[0] is ODataFeed);
            WriteNestedItems(nestedItemToWrite, writer);
            ValidateWrittenPayload(stream, writer, expectedPayload);
        }
 private void WriteMinimalRequestWithModelAndValidatePayload(ODataMediaType mediaType, ODataItem[] nestedItemToWrite, string expectedPayload, bool setMetadataDocumentUri = true)
 {
     // with model
     // write request (JsonLightMetadataLevel.Create method will internally use MinimalMetadata for writing request)
     var stream = new MemoryStream();
     var outputContext = CreateJsonLightOutputContext(stream, mediaType, false, this.userModel, setMetadataDocumentUri ? this.serviceDocumentUri : null);
     var writer = new ODataJsonLightWriter(outputContext, this.entitySet, this.entityType, nestedItemToWrite[0] is ODataFeed);
     WriteNestedItems(nestedItemToWrite, writer);
     ValidateWrittenPayload(stream, writer, expectedPayload);
 }
 private static void ValidateWrittenPayload(MemoryStream stream, ODataJsonLightWriter writer, string expectedPayload)
 {
     writer.Flush();
     stream.Seek(0, SeekOrigin.Begin);
     string payload = (new StreamReader(stream)).ReadToEnd();
     payload.Should().Be(expectedPayload);
 }
        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 void WriteNestedItemsAndValidatePayload(string entitySetFullName, string derivedEntityTypeFullName, ODataItem[] nestedItemToWrite, string expectedPayload, bool writingResponse = true, bool setMetadataDocumentUri = true)
        {
            MemoryStream stream = new MemoryStream();
            ODataJsonLightOutputContext outputContext = CreateJsonLightOutputContext(stream, writingResponse, EdmCoreModel.Instance, setMetadataDocumentUri ? this.metadataDocumentUri : null);

            ODataItem topLevelItem = nestedItemToWrite[0];
            ODataFeed topLevelFeed = topLevelItem as ODataFeed;

            if (entitySetFullName != null)
            {
                ODataFeedAndEntrySerializationInfo serializationInfo = entitySetFullName == null ? null : new ODataFeedAndEntrySerializationInfo { NavigationSourceName = entitySetFullName, NavigationSourceEntityTypeName = "NS.MyEntityType", ExpectedTypeName = derivedEntityTypeFullName ?? "NS.MyEntityType" };
                if (topLevelFeed != null)
                {
                    topLevelFeed.SetSerializationInfo(serializationInfo);
                }
                else
                {
                    ((ODataEntry)topLevelItem).SetSerializationInfo(serializationInfo);
                }
            }

            ODataJsonLightWriter writer = new ODataJsonLightWriter(outputContext, /*entitySet*/ null, /*entityType*/ null, /*writingFeed*/ topLevelFeed != null);
            WriteNestedItems(nestedItemToWrite, writer);
            ValidateWrittenPayload(stream, writer, expectedPayload);
        }
 private void WriteNestedItemsAndValidatePayload(IEdmEntitySet entitySet, IEdmEntityType entityType, ODataItem[] nestedItemToWrite, string expectedPayload, bool writingResponse = true, bool setMetadataDocumentUri = true)
 {
     MemoryStream stream = new MemoryStream();
     ODataJsonLightOutputContext outputContext = CreateJsonLightOutputContext(stream, writingResponse, this.userModel, setMetadataDocumentUri ? this.metadataDocumentUri : null);
     ODataJsonLightWriter writer = new ODataJsonLightWriter(outputContext, entitySet, entityType, nestedItemToWrite[0] is ODataFeed);
     WriteNestedItems(nestedItemToWrite, writer);
     ValidateWrittenPayload(stream, writer, expectedPayload);
 }