Exemplo n.º 1
0
        private static void VerifyBinaryReader <T>(T expectedDeserializedValue)
        {
            string stringValue           = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue));
            ReadOnlyMemory <byte> result = JsonPerfMeasurement.ConvertTextToBinary(stringValue);

            NewtonsoftInteropTests.VerifyReader <T>(result, expectedDeserializedValue);
        }
Exemplo n.º 2
0
        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 = NewtonsoftToCosmosDBReader.CreateFromString(json);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        reader = JsonReader.Create(Encoding.UTF8.GetBytes(json));
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        reader = JsonReader.Create(JsonPerfMeasurement.ConvertTextToBinary(json));
                        break;

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

                    IJsonWriter writer;
                    switch (destinationFormat)
                    {
                    case NewtonsoftWrapperFormat.NewtonsoftText:
                        writer = NewtonsoftToCosmosDBWriter.CreateTextWriter();
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        writer = JsonWriter.Create(JsonSerializationFormat.Text);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        writer = JsonWriter.Create(JsonSerializationFormat.Binary);
                        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().ToArray());
                        break;

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

                    default:
                        throw new ArgumentException();
                    }

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

                    Console.WriteLine($"{sourceFormat} Reader to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms");
                }
            }
        }