/// <summary>
        /// Converts a list of CosmosElements into a memory stream.
        /// </summary>
        /// <param name="cosmosElement">The cosmos elements</param>
        /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param>
        /// <returns>Returns a memory stream of cosmos elements. By default the memory stream will contain JSON.</returns>
        private static MemoryStream ElementToMemoryStream(
            CosmosElement cosmosElement,
            CosmosSerializationFormatOptions cosmosSerializationOptions = null)
        {
            IJsonWriter jsonWriter;

            if (cosmosSerializationOptions != null)
            {
                jsonWriter = cosmosSerializationOptions.CreateCustomWriterCallback();
            }
            else
            {
                jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text);
            }

            cosmosElement.WriteTo(jsonWriter);

            ReadOnlyMemory <byte> result = jsonWriter.GetResult();

            if (!MemoryMarshal.TryGetArray(result, out ArraySegment <byte> resultAsArray))
            {
                resultAsArray = new ArraySegment <byte>(result.ToArray());
            }

            return(new MemoryStream(resultAsArray.Array, resultAsArray.Offset, resultAsArray.Count));
        }
Exemple #2
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            JsonNewtonsoftWriter writerInterop = JsonNewtonsoftWriter.Create(writer);
            CosmosElement        cosmosElement = value as CosmosElement;

            cosmosElement.WriteTo(writerInterop);
        }