private static void VerifyBinaryReader <T>(T expectedDeserializedValue)
        {
            string stringValue           = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue));
            ReadOnlyMemory <byte> result = JsonTestUtils.ConvertTextToBinary(stringValue);

            NewtonsoftInteropTests.VerifyReader <T>(result, expectedDeserializedValue);
        }
        public void LargeArrayBinaryJsonTest()
        {
            StringBuilder builder = new StringBuilder((1 << 24) + 50);

            builder.Append('[');
            for (int x = 1 << 24; x < (1 << 24) + 3355450; ++x)
            {
                builder.Append(x);
                builder.Append(',');
            }
            builder.Append("\"string_one\"");
            builder.Append(',');
            builder.Append("\"string_two\"");
            builder.Append(',');
            builder.Append("\"string_two\"");
            builder.Append(']');

            string json = builder.ToString();

            byte[]             binaryJson  = JsonTestUtils.ConvertTextToBinary(json);
            IJsonNavigator     navigator   = JsonNavigator.Create(binaryJson);
            int                count       = navigator.GetArrayItemCount(navigator.GetRootNode());
            IJsonNavigatorNode node        = navigator.GetArrayItemAt(navigator.GetRootNode(), count - 1);
            string             stringValue = navigator.GetStringValue(node);

            Assert.AreEqual("string_two", stringValue);
        }
        private static void VerifyWriter <T>(JsonSerializationFormat jsonSerializationFormat, T expectedDeserializedValue)
        {
            using (CosmosDBToNewtonsoftWriter writer = new CosmosDBToNewtonsoftWriter(jsonSerializationFormat))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(writer, expectedDeserializedValue);

                ReadOnlyMemory <byte> result = writer.GetResult();
                string actualSerializedValue;
                if (jsonSerializationFormat == JsonSerializationFormat.Binary)
                {
                    actualSerializedValue = JsonTestUtils.ConvertBinaryToText(result);
                }
                else
                {
                    actualSerializedValue = Utf8Span.UnsafeFromUtf8BytesNoValidation(result.Span).ToString();
                }

                actualSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(actualSerializedValue);
                string expectedSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(
                    JsonConvert.SerializeObject(
                        expectedDeserializedValue));
                Assert.AreEqual(expectedSerializedValue, actualSerializedValue);
            }
        }
Exemple #4
0
        private static void VerifyNavigator(
            string input,
            bool performExtraChecks = true)
        {
            CultureInfo defaultCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            CultureInfo[] cultureInfoList = new CultureInfo[]
            {
                defaultCultureInfo,
                System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
            };

            try
            {
                foreach (CultureInfo cultureInfo in cultureInfoList)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

                    IJsonReader jsonReader       = JsonReader.Create(Encoding.UTF8.GetBytes(input));
                    JsonToken[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Test text
                    IJsonNavigator     textNavigator           = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
                    IJsonNavigatorNode textRootNode            = textNavigator.GetRootNode();
                    JsonToken[]        tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

                    // Test binary
                    byte[]             binaryInput               = JsonTestUtils.ConvertTextToBinary(input);
                    IJsonNavigator     binaryNavigator           = JsonNavigator.Create(binaryInput);
                    IJsonNavigatorNode binaryRootNode            = binaryNavigator.GetRootNode();
                    JsonToken[]        tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));

                    // Test binary + user string encoding
                    JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);
                    byte[] binaryWithUserStringEncodingInput  = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
                    if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out _))
                    {
                        Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
                    }

                    IJsonNavigator     binaryNavigatorWithUserStringEncoding           = JsonNavigator.Create(binaryInput, jsonStringDictionary);
                    IJsonNavigatorNode binaryRootNodeWithUserStringEncoding            = binaryNavigatorWithUserStringEncoding.GetRootNode();
                    JsonToken[]        tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNodeWithUserStringEncoding, binaryNavigatorWithUserStringEncoding, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }
        // Checks to see if we can go from a JsonReader to a NewtonsoftWriter and get back the original document and visa versa
        private void RoundTripTestCuratedJson(string path)
        {
            path = string.Format("TestJsons/{0}", path);
            string json = TextFileConcatenation.ReadMultipartFile(path);

#if true
            json = JsonTestUtils.RandomSampleJson(json, seed: 42, maxNumberOfItems: 100);
#endif
            JsonRoundTripsTests.MultiSerializationRoundTrip(json);
        }
Exemple #6
0
        private static void VerifyNavigatorWithCurratedDoc(string path, bool performExtraChecks = true)
        {
            path = string.Format("TestJsons/{0}", path);
            string json = TextFileConcatenation.ReadMultipartFile(path);

#if true
            json = JsonTestUtils.RandomSampleJson(json, maxNumberOfItems: 1);
#endif

            JsonNavigatorTests.VerifyNavigator(json, performExtraChecks);
        }
        public void Utf8VsUtf16StringRead()
        {
            void RunPerf(string utf16String, JsonSerializationFormat jsonSerializationFormat, bool useUtf8)
            {
                Stopwatch stopWatch = new Stopwatch();

                byte[] payload = JsonTestUtils.ConvertTextToBinary("\"" + utf16String + "\"");
                for (int i = 0; i < 1000000; i++)
                {
                    IJsonReader reader = JsonReader.Create(payload);
                    reader.Read();
                    stopWatch.Start();
                    if (useUtf8)
                    {
                        reader.TryGetBufferedUtf8StringValue(out ReadOnlyMemory <byte> bufferedUtf8StringValue);
                    }
                    else
                    {
                        string value = reader.GetStringValue();
                    }
                    stopWatch.Stop();
                }

                Console.WriteLine($"UTF {(useUtf8 ? 8 : 16)} {jsonSerializationFormat} reader + string length: {utf16String.Length} = {stopWatch.ElapsedMilliseconds} ms");
            }

            foreach (int stringLength in new int[] { 8, 32, 256, 1024, 4096 })
            {
                foreach (JsonSerializationFormat jsonSerializationFormat in new JsonSerializationFormat[] { JsonSerializationFormat.Text, JsonSerializationFormat.Binary })
                {
                    foreach (bool useUtf8 in new bool[] { false, true })
                    {
                        RunPerf(
                            utf16String: new string('a', stringLength),
                            jsonSerializationFormat: jsonSerializationFormat,
                            useUtf8: useUtf8);
                    }
                }
            }
        }
        private static void VerifyWriter <T>(JsonSerializationFormat jsonSerializationFormat, T expectedDeserializedValue)
        {
            using (CosmosDBToNewtonsoftWriter writer = new CosmosDBToNewtonsoftWriter(jsonSerializationFormat))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(writer, expectedDeserializedValue);

                byte[] result = writer.GetResult().ToArray();
                string actualSerializedValue;
                if (jsonSerializationFormat == JsonSerializationFormat.Binary)
                {
                    actualSerializedValue = JsonTestUtils.ConvertBinaryToText(result);
                }
                else
                {
                    actualSerializedValue = Encoding.UTF8.GetString(result);
                }

                actualSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(actualSerializedValue);
                string expectedSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue));
                Assert.AreEqual(expectedSerializedValue, actualSerializedValue);
            }
        }
        private static void PerformRoundTrip(
            SerializationFormat sourceFormat,
            SerializationFormat destinationFormat,
            string json,
            bool writeAsRootNode)
        {
            IJsonReader reader = sourceFormat switch
            {
                SerializationFormat.Text => JsonReader.Create(Encoding.UTF8.GetBytes(json)),
                SerializationFormat.Binary => JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json)),
                SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBReader.CreateFromString(json),
                _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"),
            };

            IJsonNavigator navigator = sourceFormat switch
            {
                SerializationFormat.Text => JsonNavigator.Create(Encoding.UTF8.GetBytes(json)),
                SerializationFormat.Binary => JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json)),
                SerializationFormat.NewtonsoftText => new JsonNewtonsoftNavigator(json),
                _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"),
            };

            foreach (object source in new object[] { reader, navigator })
            {
                IJsonWriter writer = destinationFormat switch
                {
                    SerializationFormat.Text => JsonWriter.Create(JsonSerializationFormat.Text),
                    SerializationFormat.Binary => JsonWriter.Create(JsonSerializationFormat.Binary),
                    SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBWriter.CreateTextWriter(),
                    _ => throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}"),
                };

                switch (source)
                {
                case IJsonReader sourceReader:
                    sourceReader.WriteAll(writer);
                    break;

                case IJsonNavigator sourceNavigator:
                    if (writeAsRootNode)
                    {
                        sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                    }
                    else
                    {
                        IJsonNavigatorNode rootNode     = sourceNavigator.GetRootNode();
                        JsonNodeType       jsonNodeType = sourceNavigator.GetNodeType(rootNode);
                        switch (jsonNodeType)
                        {
                        case JsonNodeType.Array:
                            writer.WriteArrayStart();

                            foreach (IJsonNavigatorNode arrayItem in sourceNavigator.GetArrayItems(rootNode))
                            {
                                sourceNavigator.WriteNode(arrayItem, writer);
                            }

                            writer.WriteArrayEnd();
                            break;

                        case JsonNodeType.Object:
                            writer.WriteObjectStart();

                            foreach (ObjectProperty objectProperty in sourceNavigator.GetObjectProperties(rootNode))
                            {
                                sourceNavigator.WriteNode(objectProperty.NameNode, writer);
                                sourceNavigator.WriteNode(objectProperty.ValueNode, writer);
                            }

                            writer.WriteObjectEnd();
                            break;

                        default:
                            sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                            break;
                        }
                    }
                    break;

                default:
                    Assert.Fail("Failed to downcast source type.");
                    break;
                }

                string result = writer.SerializationFormat switch
                {
                    JsonSerializationFormat.Text => Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString(),
                    JsonSerializationFormat.Binary => JsonTestUtils.ConvertBinaryToText(writer.GetResult()),
                    _ => throw new ArgumentException(),
                };
                string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result);
                string normalizedJson   = JsonRoundTripsTests.NewtonsoftFormat(json);

                Assert.AreEqual(normalizedJson, normalizedResult);
            }
        }
Exemple #10
0
        public static void MeasurePerf(string json, string filename, int numberOfIterations = 1)
        {
            byte[] utf8ByteArray = Encoding.UTF8.GetBytes(json);

            // Text
            TimeSpan textReaderTime = JsonPerfMeasurement.MeasureReadPerformance(
                JsonReader.Create(utf8ByteArray),
                numberOfIterations);
            TimeSpan textWriterTime = JsonPerfMeasurement.MeasureWritePerformance(
                JsonWriter.Create(JsonSerializationFormat.Text),
                json,
                numberOfIterations);
            TimeSpan textNavigatorTime = JsonPerfMeasurement.MeasureNavigationPerformance(
                JsonNavigator.Create(utf8ByteArray),
                numberOfIterations);
            JsonExecutionTimes textExecutionTimes = new JsonExecutionTimes(
                textReaderTime,
                textWriterTime,
                textNavigatorTime,
                utf8ByteArray.Length,
                "Text");

            // Newtonsoft
            TimeSpan newtonsoftReaderTime = JsonPerfMeasurement.MeasureReadPerformance(
                NewtonsoftToCosmosDBReader.CreateFromString(json),
                numberOfIterations);
            TimeSpan newtonsoftWriterTime = JsonPerfMeasurement.MeasureWritePerformance(
                NewtonsoftToCosmosDBWriter.CreateTextWriter(),
                json,
                numberOfIterations);
            TimeSpan newtonsoftNavigatorTime = JsonPerfMeasurement.MeasureNavigationPerformance(
                new JsonNewtonsoftNavigator(json),
                numberOfIterations);
            JsonExecutionTimes newtonsoftExecutionTimes = new JsonExecutionTimes(
                newtonsoftReaderTime,
                newtonsoftWriterTime,
                newtonsoftNavigatorTime,
                json.Length,
                "Newtonsoft");

            // Binary
            byte[]   binaryPayload    = JsonTestUtils.ConvertTextToBinary(json);
            TimeSpan binaryReaderTime = JsonPerfMeasurement.MeasureReadPerformance(
                JsonReader.Create(binaryPayload),
                numberOfIterations);
            TimeSpan binarytWriterTime = JsonPerfMeasurement.MeasureWritePerformance(
                JsonWriter.Create(JsonSerializationFormat.Binary),
                json,
                numberOfIterations);
            TimeSpan binaryNavigatorTime = JsonPerfMeasurement.MeasureNavigationPerformance(
                JsonNavigator.Create(binaryPayload),
                numberOfIterations);
            JsonExecutionTimes binaryExecutionTimes = new JsonExecutionTimes(
                binaryReaderTime,
                binarytWriterTime,
                binaryNavigatorTime,
                binaryPayload.Length,
                "Binary");

            JsonPerfMeasurement.PrintStatisticsTable(
                filename,
                textExecutionTimes,
                newtonsoftExecutionTimes,
                binaryExecutionTimes);
        }
        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 = NewtonsoftToCosmosDBReader.CreateFromString(json);
                        break;

                    case SerializationFormat.BinaryWithDictionaryEncoding:
                        JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                        reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json, jsonStringDictionary), jsonStringDictionary);
                        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;

                    case SerializationFormat.BinaryWithDictionaryEncoding:
                        JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                        navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json, jsonStringDictionary), jsonStringDictionary);
                        break;

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

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

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

                        case SerializationFormat.NewtonsoftText:
                            writer = NewtonsoftToCosmosDBWriter.CreateTextWriter();
                            jsonStringDictionary = null;
                            break;

                        case SerializationFormat.BinaryWithDictionaryEncoding:
                            jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                            writer = JsonWriter.Create(JsonSerializationFormat.Binary, jsonStringDictionary);
                            break;

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

                        Stopwatch stopwatch = Stopwatch.StartNew();
                        switch (source)
                        {
                        case IJsonReader sourceReader:
                            writer.WriteAll(sourceReader);
                            break;

                        case IJsonNavigator sourceNavigator:
                            writer.WriteJsonNode(sourceNavigator, sourceNavigator.GetRootNode());
                            break;

                        default:
                            Assert.Fail("Failed to downcast source type.");
                            break;
                        }
                        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(), jsonStringDictionary);
                            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");
                    }
                }
            }
        }
Exemple #12
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");
                }
            }
        }
Exemple #13
0
        private static void VerifyNavigator(
            string input,
            bool performExtraChecks = true)
        {
            CultureInfo defaultCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            CultureInfo[] cultureInfoList = new CultureInfo[]
            {
                defaultCultureInfo,
                System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
            };

            try
            {
                foreach (CultureInfo cultureInfo in cultureInfoList)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

                    IJsonReader jsonReader       = JsonReader.Create(Encoding.UTF8.GetBytes(input));
                    JsonToken[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Text
                    IJsonNavigator textNavigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));

                    // Binary
                    byte[]         binaryInput     = JsonTestUtils.ConvertTextToBinary(input);
                    IJsonNavigator binaryNavigator = JsonNavigator.Create(binaryInput);

                    // Test binary + user string encoding
                    JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);
                    byte[] binaryWithUserStringEncodingInput  = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
                    if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out _))
                    {
                        Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
                    }

                    IJsonNavigator binaryNavigatorWithUserStringEncoding = JsonNavigator.Create(binaryInput, jsonStringDictionary);

                    // Test
                    foreach (IJsonNavigator jsonNavigator in new IJsonNavigator[] { textNavigator, binaryNavigator, binaryNavigatorWithUserStringEncoding })
                    {
                        IJsonNavigatorNode rootNode            = jsonNavigator.GetRootNode();
                        JsonToken[]        tokensFromNavigator = JsonNavigatorTests.GetTokensFromNode(rootNode, jsonNavigator, performExtraChecks);
                        Assert.IsTrue(tokensFromNavigator.SequenceEqual(tokensFromReader));

                        // Test materialize
                        JToken materializedToken = CosmosElement.Dispatch(jsonNavigator, rootNode).Materialize <JToken>();

                        try
                        {
                            if (materializedToken.ToString() != JToken.Parse(input).ToString())
                            {
                                throw new MaterilizationFailedToMatchException();
                            }
                        }
                        catch (Newtonsoft.Json.JsonReaderException)
                        {
                            // If the input is extended type we ignore this check.
                        }
                    }
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }
        private static void PerformRoundTrip(
            SerializationFormat sourceFormat,
            SerializationFormat destinationFormat,
            string json)
        {
            JsonStringDictionary sourceDictionary = null;

            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 = NewtonsoftToCosmosDBReader.CreateFromString(json);
                break;

            case SerializationFormat.BinaryWithDictionaryEncoding:
                sourceDictionary = new JsonStringDictionary(capacity: 128);
                reader           = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json, sourceDictionary), sourceDictionary);
                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;

            case SerializationFormat.BinaryWithDictionaryEncoding:
                sourceDictionary = new JsonStringDictionary(capacity: 128);
                navigator        = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json, sourceDictionary), sourceDictionary);
                break;

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

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

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

                case SerializationFormat.NewtonsoftText:
                    writer = NewtonsoftToCosmosDBWriter.CreateTextWriter();
                    jsonStringDictionary = null;
                    break;

                case SerializationFormat.BinaryWithDictionaryEncoding:
                    jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                    if (sourceFormat == SerializationFormat.BinaryWithDictionaryEncoding)
                    {
                        int index = 0;
                        while (sourceDictionary.TryGetStringAtIndex(index++, out UtfAllString value))
                        {
                            Assert.IsTrue(jsonStringDictionary.TryAddString(value.Utf16String, out _));
                        }
                    }

                    writer = JsonWriter.Create(JsonSerializationFormat.Binary, jsonStringDictionary);
                    break;

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

                switch (source)
                {
                case IJsonReader sourceReader:
                    sourceReader.WriteAll(writer);
                    break;

                case IJsonNavigator sourceNavigator:
                    sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                    break;

                default:
                    Assert.Fail("Failed to downcast source type.");
                    break;
                }

                string result;
                switch (writer.SerializationFormat)
                {
                case JsonSerializationFormat.Text:
                    result = Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString();
                    break;

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

                default:
                    throw new ArgumentException();
                }

                string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result);
                string normalizedJson   = JsonRoundTripsTests.NewtonsoftFormat(json);

                Assert.AreEqual(normalizedJson, normalizedResult);
            }
        }
Exemple #15
0
        private static void VerifyNavigator(
            string input,
            bool performExtraChecks = true)
        {
            CultureInfo defaultCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            CultureInfo[] cultureInfoList = new CultureInfo[]
            {
                defaultCultureInfo,
                System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
            };

            try
            {
                foreach (CultureInfo cultureInfo in cultureInfoList)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

                    IJsonReader jsonReader       = JsonReader.Create(Encoding.UTF8.GetBytes(input));
                    JsonToken[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Text
                    IJsonNavigator textNavigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));

                    // Binary
                    byte[]         binaryInput     = JsonTestUtils.ConvertTextToBinary(input);
                    IJsonNavigator binaryNavigator = JsonNavigator.Create(binaryInput);

                    // Test
                    foreach (IJsonNavigator jsonNavigator in new IJsonNavigator[] { textNavigator, binaryNavigator })
                    {
                        IJsonNavigatorNode rootNode            = jsonNavigator.GetRootNode();
                        JsonToken[]        tokensFromNavigator = JsonNavigatorTests.GetTokensFromNode(rootNode, jsonNavigator, performExtraChecks);
                        Assert.AreEqual(tokensFromNavigator.Length, tokensFromReader.Length);
                        IEnumerable <(JsonToken, JsonToken)> zippedTokens = tokensFromNavigator.Zip(tokensFromReader, (first, second) => (first, second));
                        foreach ((JsonToken tokenFromNavigator, JsonToken tokenFromReader) in zippedTokens)
                        {
                            if (!tokenFromNavigator.Equals(tokenFromReader))
                            {
                                Assert.Fail();
                            }
                        }

                        // Test materialize
                        JToken materializedToken = CosmosElement.Dispatch(jsonNavigator, rootNode).Materialize <JToken>();

                        try
                        {
                            if (materializedToken.ToString() != JToken.Parse(input).ToString())
                            {
                                throw new MaterilizationFailedToMatchException();
                            }
                        }
                        catch (Newtonsoft.Json.JsonReaderException)
                        {
                            // If the input is extended type we ignore this check.
                        }
                    }
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }