예제 #1
0
        private static void VerifyBinaryReader <T>(T expectedDeserializedValue)
        {
            string stringValue = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue));

            byte[] result = JsonPerfMeasurement.ConvertTextToBinary(stringValue);
            NewtonsoftInteropTests.VerifyReader <T>(result, expectedDeserializedValue);
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the JsonNewtonsoftCosmosDBBinaryReader class.
 /// </summary>
 /// <param name="jsonText">The json text.</param>
 public JsonNewtonsoftCosmosDBBinaryReader(string jsonText)
     : base(new JsonCosmosDBReader(new MemoryStream(JsonPerfMeasurement.ConvertTextToBinary(jsonText))))
 {
 }
        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());
                        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");
                }
            }
        }