/// <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));
        }
Exemplo n.º 2
0
            public static Payload Create(string name, string jsonText)
            {
                if (jsonText == null)
                {
                    throw new ArgumentNullException(nameof(jsonText));
                }

                CosmosElement element          = CosmosElement.Parse(jsonText);
                IJsonWriter   jsonBinaryWriter = JsonWriter.Create(JsonSerializationFormat.Binary);
                IJsonWriter   jsonTextWriter   = JsonWriter.Create(JsonSerializationFormat.Text);

                element.WriteTo(jsonTextWriter);
                element.WriteTo(jsonBinaryWriter);

                ReadOnlyMemory <byte> text   = jsonTextWriter.GetResult();
                ReadOnlyMemory <byte> binary = jsonBinaryWriter.GetResult();

                return(new Payload(name, text, binary));
            }