private string NewtonsoftFormat(string json)
        {
            JsonNewtonsoftReader newtonsoftReader = new JsonNewtonsoftNewtonsoftTextReader(json);
            JsonNewtonsoftWriter newtonsoftWriter = new JsonNewtonsoftNewtonsoftTextWriter();

            newtonsoftWriter.WriteAll(newtonsoftReader);
            return(Encoding.UTF8.GetString(newtonsoftWriter.GetResult()));
        }
        private void NewtonsoftWrapperRoundTrip(string json)
        {
            // Normalize the json to get rid of any formatting issues
            json = this.NewtonsoftFormat(json);

            foreach (NewtonsoftWrapperFormat sourceFormat in Enum.GetValues(typeof(NewtonsoftWrapperFormat)))
            {
                foreach (NewtonsoftWrapperFormat destinationFormat in Enum.GetValues(typeof(NewtonsoftWrapperFormat)))
                {
                    IJsonReader reader;
                    switch (sourceFormat)
                    {
                    case NewtonsoftWrapperFormat.NewtonsoftText:
                        reader = new JsonNewtonsoftNewtonsoftTextReader(json);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        reader = new JsonNewtonsoftCosmosDBTextReader(json);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        reader = new JsonNewtonsoftCosmosDBBinaryReader(json);
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    IJsonWriter writer;
                    switch (destinationFormat)
                    {
                    case NewtonsoftWrapperFormat.NewtonsoftText:
                        writer = new JsonNewtonsoftNewtonsoftTextWriter();
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        writer = new JsonNewtonsoftCosmosDBTextWriter();
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        writer = new JsonNewtonsoftCosmosDBBinaryWriter();
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    Stopwatch stopwatch = Stopwatch.StartNew();
                    writer.WriteAll(reader);
                    stopwatch.Stop();

                    string result;
                    switch (writer.SerializationFormat)
                    {
                    case JsonSerializationFormat.Text:
                        result = Encoding.UTF8.GetString(writer.GetResult());
                        break;

                    case JsonSerializationFormat.Binary:
                        result = JsonTestUtils.ConvertBinaryToText(writer.GetResult());
                        break;

                    default:
                        throw new ArgumentException();
                    }

                    result = this.NewtonsoftFormat(result);
                    Assert.AreEqual(json, result);

                    Console.WriteLine($"{sourceFormat} Reader to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms");
                }
            }
        }
        private void MultiSerializationRoundTrip(string json)
        {
            // Normalize the json to get rid of any formatting issues
            json = this.NewtonsoftFormat(json);

            foreach (SerializationFormat sourceFormat in Enum.GetValues(typeof(SerializationFormat)))
            {
                foreach (SerializationFormat destinationFormat in Enum.GetValues(typeof(SerializationFormat)))
                {
                    IJsonReader reader;
                    switch (sourceFormat)
                    {
                    case SerializationFormat.Text:
                        reader = JsonReader.Create(Encoding.UTF8.GetBytes(json));
                        break;

                    case SerializationFormat.Binary:
                        reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json));
                        break;

                    case SerializationFormat.NewtonsoftText:
                        reader = new JsonNewtonsoftNewtonsoftTextReader(json);
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    IJsonNavigator navigator;
                    switch (sourceFormat)
                    {
                    case SerializationFormat.Text:
                        navigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(json));
                        break;

                    case SerializationFormat.Binary:
                        navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json));
                        break;

                    case SerializationFormat.NewtonsoftText:
                        navigator = new JsonNewtonsoftNavigator(json);
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    object[] sources = new object[] { reader, navigator };
                    foreach (object source in sources)
                    {
                        IJsonWriter writer;
                        switch (destinationFormat)
                        {
                        case SerializationFormat.Text:
                            writer = JsonWriter.Create(JsonSerializationFormat.Text);
                            break;

                        case SerializationFormat.Binary:
                            writer = JsonWriter.Create(JsonSerializationFormat.Binary);
                            break;

                        case SerializationFormat.NewtonsoftText:
                            writer = new JsonNewtonsoftNewtonsoftTextWriter();
                            break;

                        default:
                            throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}");
                        }

                        Stopwatch stopwatch = Stopwatch.StartNew();
                        if (source is IJsonReader)
                        {
                            IJsonReader sourceReader = source as IJsonReader;
                            writer.WriteAll(sourceReader);
                        }
                        else if (source is IJsonNavigator)
                        {
                            IJsonNavigator sourceNavigator = source as IJsonNavigator;
                            writer.WriteJsonNode(sourceNavigator, sourceNavigator.GetRootNode());
                        }
                        stopwatch.Stop();

                        string result;
                        switch (writer.SerializationFormat)
                        {
                        case JsonSerializationFormat.Text:
                            result = Encoding.UTF8.GetString(writer.GetResult());
                            break;

                        case JsonSerializationFormat.Binary:
                            result = JsonTestUtils.ConvertBinaryToText(writer.GetResult());
                            break;

                        default:
                            throw new ArgumentException();
                        }

                        result = this.NewtonsoftFormat(result);

                        Assert.AreEqual(json, result);
                        string sourceType = (source is IJsonReader) ? "Reader" : "Navigator";
                        Console.WriteLine($"{sourceFormat} {sourceType} to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms");
                    }
                }
            }
        }