Ejemplo n.º 1
0
        /// <summary>
        /// Display the result of the Web API call
        /// </summary>
        /// <param name="result">Object to display</param>
        private static void Display(JsonNode result)
        {
            Console.WriteLine("Web Api result: \n");

            JsonArray nodes = result.AsArray();

            foreach (JsonObject aNode in nodes.ToArray())
            {
                foreach (var property in aNode.ToArray())
                {
                    Console.WriteLine($"{property.Key} = {property.Value?.ToString()}");
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// Merges 2 streams of JSON on the property defined by <code>itemName</code>. The property should be a JSON array
    /// </summary>
    /// <param name="left">The first stream.</param>
    /// <param name="right">The second stream.</param>
    /// <param name="itemName">The name of the array property to merge on.</param>
    /// <param name="nextLinkName">The name of the property containing the next link name.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    internal async Task <Stream?> MergeJsonStreamsAsync(Stream?left, Stream?right, string itemName = "value", string nextLinkName = "nextLink", CancellationToken cancellationToken = default)
    {
        if (left?.CanSeek == true)
        {
            left?.Seek(0, SeekOrigin.Begin);
        }
        if (right?.CanSeek == true)
        {
            right?.Seek(0, SeekOrigin.Begin);
        }
        if (left == null || right == null)
        {
            return(left ?? right);
        }

        JsonNode?nodeLeft = JsonNode.Parse(left);

        if (left.CanSeek == true)
        {
            left.Seek(0, SeekOrigin.Begin);
        }
        JsonNode?nodeRight = JsonNode.Parse(right);

        if (right.CanSeek == true)
        {
            right.Seek(0, SeekOrigin.Begin);
        }

        JsonArray?leftArray  = null;
        JsonArray?rightArray = null;

        if (!string.IsNullOrWhiteSpace(itemName))
        {
            if (nodeLeft?[itemName] == null)
            {
                return(right);
            }
            else if (nodeRight?[itemName] == null)
            {
                return(left);
            }

            leftArray  = nodeLeft[itemName]?.AsArray();
            rightArray = nodeRight[itemName]?.AsArray();
        }
        else
        {
            leftArray  = nodeLeft?.AsArray();
            rightArray = nodeRight?.AsArray();
        }


        if (leftArray != null && rightArray != null)
        {
            var elements = rightArray.Where(i => i != null);
            var item     = elements.FirstOrDefault();
            while (item != null)
            {
                rightArray.Remove(item);
                leftArray.Add(item);
                item = elements.FirstOrDefault();
            }
        }
        if (!string.IsNullOrWhiteSpace(itemName) && nodeLeft != null)
        {
            nodeLeft[itemName] = leftArray ?? rightArray;
        }
        else
        {
            nodeLeft = leftArray ?? rightArray;
        }

        // Replace next link with new page's next link
        if (!string.IsNullOrWhiteSpace(nextLinkName))
        {
            var obj1 = nodeLeft as JsonObject;
            if (obj1?[nextLinkName] != null)
            {
                obj1.Remove(nextLinkName);
            }
            if (nodeRight is JsonObject obj2 && obj2?[nextLinkName] != null)
            {
                var nextLink = obj2[nextLinkName];
                obj2.Remove(nextLinkName);
                obj1?.Add(nextLinkName, nextLink);
            }
        }
        var stream = new MemoryStream();

        using var writer = new Utf8JsonWriter(stream);
        nodeLeft?.WriteTo(writer);
        await writer.FlushAsync(cancellationToken);

        stream.Position = 0;

        return(stream);
    }